用Python编写的五子棋程序1.0版
作为一个Python初学者,
在学习了相关知识后,
又参考了一些代码,
自己也编了个五子棋的程序,
目前只能实现人人对战,
即双方轮流下,
执黑先走,鼠标左键走棋,右键悔棋。
请多指教!
代码如下:
""" 判断输赢函数 """ def checkWin(x,y): flag = False count = 1 #保存共有相同颜色多少棋子相连 color =map[x][y] #横向判断 i =1 while color == map[x + i][y]: #向右判断 count += 1 i += 1 i=1 while color == map[x - i][y]: #向左判断 count += 1 i += 1 if count >= 5: flag = True #纵向判断 i2 = 1 count2 = 1 while color == map[x][y + i2]: # 向上判断 count2 += 1 i2 += 1 i2 = 1 while color == map[x][y - i2]: # 向下判断 count2 += 1 i2 += 1 if count2 >= 5: flag = True #右上和左下判断 i3 = 1 count3 = 1 while color == map[x + i3][y + i3]: # 右上判断 count3 += 1 i3 += 1 i3 = 1 while color == map[x - i3][y - i3]: # 左下判断 count3 += 1 i3 += 1 if count3 >= 5: flag = True #右下和左上判断 i4 = 1 count4 = 1 while color == map[x + i4][y - i4]: # 右下判断 count4 += 1 i4 += 1 i3 = 1 while color == map[x - i4][y + i4]: # 左上判断 count4 += 1 i4 += 1 if count4 >= 5: flag = True return flag """ 走棋函数 """ def callback(event): global turn x = (event.x)//40 y = (event.y)//40 print("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 = 1 else: turn = 0 """ 悔棋函数 """ def huiqi(event): global turn if len(back) == 0: showinfo(title= "提示",message = "已没有任何棋子!") return m = back.pop() id = m[0] x = m[1] y = m[2] map[x][y] = '' cv.delete(id) # 删除棋子 #换上一方走棋 if turn == 0: turn = 1 else: turn = 0 """ 绘制棋盘函数 """ 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') """ 主函数 """ 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_studypracticeblacK.gif'), PhotoImage(file = 'F:Python_studypracticewhite.gif')] #调取棋子图片 格式为gif root.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()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155棋子图片:
程序运行如下:
在主函数调取棋子图片时,
老是出错,
后来在论坛求助后,
得到网友“陈年椰子”的提醒和指教,
完美解决问题,
如下:求助贴
本程序也得以完成,
在此再次感谢。
相关知识
Python编写玫瑰花
Python的魅力
用python画花瓣
python 玫瑰花程序
Python的简单介绍(一)
Python文字花
python毕设网上购物商城系统36x49.程序+论文
用 Python 做一个情人节表白神器
(开题)flask框架《花间故里》(程序+论文+python)
玫瑰花数程序编写
网址: 用Python编写的五子棋程序1.0版 https://www.huajiangbk.com/newsview1619064.html
上一篇: 现有工厂丢弃的废铜屑(铜屑表面呈 |
下一篇: 绿色、智能,进博会汽车展区描绘未 |
推荐分享

- 1君子兰什么品种最名贵 十大名 4012
- 2世界上最名贵的10种兰花图片 3364
- 3花圈挽联怎么写? 3286
- 4迷信说家里不能放假花 家里摆 1878
- 5香山红叶什么时候红 1493
- 6花的意思,花的解释,花的拼音 1210
- 7教师节送什么花最合适 1167
- 8勿忘我花图片 1103
- 9橄榄枝的象征意义 1093
- 10洛阳的市花 1039