日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

用Python编写的五子棋程序1.0版

發(fā)布時(shí)間:2023/12/15 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用Python编写的五子棋程序1.0版 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

作為一個(gè)Python初學(xué)者,
在學(xué)習(xí)了相關(guān)知識(shí)后,
又參考了一些代碼,
自己也編了個(gè)五子棋的程序,
目前只能實(shí)現(xiàn)人人對(duì)戰(zhàn),
即雙方輪流下,
執(zhí)黑先走,鼠標(biāo)左鍵走棋,右鍵悔棋。
請(qǐng)多指教!

代碼如下:

"""判斷輸贏函數(shù) """ def checkWin(x,y):flag = Falsecount = 1 #保存共有相同顏色多少棋子相連color =map[x][y]#橫向判斷i =1while color == map[x + i][y]: #向右判斷count += 1i += 1i=1while color == map[x - i][y]: #向左判斷count += 1i += 1if count >= 5:flag = True#縱向判斷i2 = 1count2 = 1while color == map[x][y + i2]: # 向上判斷count2 += 1i2 += 1i2 = 1while color == map[x][y - i2]: # 向下判斷count2 += 1i2 += 1if count2 >= 5:flag = True#右上和左下判斷i3 = 1count3 = 1while color == map[x + i3][y + i3]: # 右上判斷count3 += 1i3 += 1i3 = 1while color == map[x - i3][y - i3]: # 左下判斷count3 += 1i3 += 1if count3 >= 5:flag = True#右下和左上判斷i4 = 1count4 = 1while color == map[x + i4][y - i4]: # 右下判斷count4 += 1i4 += 1i3 = 1while color == map[x - i4][y + i4]: # 左上判斷count4 += 1i4 += 1if count4 >= 5:flag = Truereturn flag"""走棋函數(shù) """ def callback(event):global turnx = (event.x)//40y = (event.y)//40print("click at",x,y,turn)if map[x][y] != "":showinfo(title= "提示",message = "已有棋子!")else:img1 = pics[turn]id = cv.create_image((x * 40 + 20, y * 40 + 20),image = img1)back.append((id,x,y))cv.pack()map[x][y] = str(turn)print_map()if checkWin(x,y):if turn == 0:showinfo(title= "提示",message = "黑方贏了!")else:showinfo(title= "提示",message = "白方贏了!")# 換下一方走棋if turn == 0:turn = 1else:turn = 0"""悔棋函數(shù) """ def huiqi(event):global turnif len(back) == 0:showinfo(title= "提示",message = "已沒有任何棋子!")returnm = back.pop()id = m[0]x = m[1]y = m[2]map[x][y] = ''cv.delete(id) # 刪除棋子#換上一方走棋if turn == 0:turn = 1else:turn = 0"""繪制棋盤函數(shù) """ def drawQipan():for i in range(0,15):cv.create_line(20,20+40*i,580,20+40*i,width = 2)for i in range(0,15):cv.create_line(20+40*i,20,20+40*i,580,width = 2)cv.pack"""輸出走棋信息 """ def print_map():for i in range(0, 15):for j in range(0, 15):print(map[i][j],end='')print('w')"""主函數(shù) """ from tkinter import * from tkinter.messagebox import *turn = 0 map = [[""for y in range(15)]for x in range(15)] root = Tk()pics = [PhotoImage(file = 'F:\\Python_study\\practice\\blacK.gif'),PhotoImage(file = 'F:\\Python_study\\practice\\white.gif')] #調(diào)取棋子圖片 格式為gifroot.title("五子棋v1.0") back = [] cv = Canvas(root,bg = 'green',width = 610,height = 610) #棋盤 drawQipan() cv.bind("<Button-1>",callback) #走棋 cv.bind("<Button-3>",huiqi) #悔棋 cv.pack() root.mainloop()

棋子圖片:


程序運(yùn)行如下:

在主函數(shù)調(diào)取棋子圖片時(shí),
老是出錯(cuò),
后來在論壇求助后,
得到網(wǎng)友“陳年椰子”的提醒和指教,
完美解決問題,
如下:求助貼
本程序也得以完成,
在此再次感謝。

總結(jié)

以上是生活随笔為你收集整理的用Python编写的五子棋程序1.0版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。