点击上方蓝色“程序猿DD”,选择“设为星标”

回复“资源”获取独家整理的学习资料!

来源 |  blog.csdn.net/gaosanjin/article/details/108244164

「羊毛+福利」撸一波超便宜的云服务,完成任务DD另外送奖励!

这是实验室2018年底招新时的考核题目,使用Python编写一个能够完成基本对战的五子棋游戏。面向新手。

程序主要包括两个部分,图形创建与逻辑编写两部分。

程序的运行结果:

样式创建

老规矩,先把用到的包导入进来。

'''
@Auther : gaoxin
@Date : 2019.01.01
@Version : 1.0
'''from tkinter import *
import math
12345678

然后建立一个样式的类,类名称chessBoard。这里加了很多注释,避免新手看不懂函数的作用,说实话我觉得挺别扭的。

#定义棋盘类
class chessBoard() :def __init__(self) :#创建一个tk对象,即窗口self.window = Tk()#窗口命名self.window.title("五子棋游戏")#定义窗口大小self.window.geometry("660x470")#定义窗口不可放缩self.window.resizable(0,0)#定义窗口里的画布self.canvas=Canvas(self.window , bg="#EEE8AC" , width=470, height=470)#画出画布内容self.paint_board()#定义画布所在的网格self.canvas.grid(row = 0 , column = 0)def paint_board(self) :#画横线for row in range(0,15) :if row == 0 or row == 14 :self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 2)else :self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 1)#画竖线for column in range(0,15) :if column == 0 or column == 14 :self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 ,width = 2)else :self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 , width = 1)#画圆self.canvas.create_oval(112, 112, 118, 118, fill="black")self.canvas.create_oval(352, 112, 358, 118, fill="black")self.canvas.create_oval(112, 352, 118, 358, fill="black")self.canvas.create_oval(232, 232, 238, 238, fill="black")self.canvas.create_oval(352, 352, 358, 358, fill="black")1234567891011121314151617181920212223242526272829303132333435363738394041

逻辑编写

这里的主要看每个函数的功能就好了。

#定义五子棋游戏类
#0为黑子 , 1为白子 , 2为空位
class Gobang() :#初始化def __init__(self) :self.board = chessBoard()self.game_print = StringVar()self.game_print.set("")#16*16的二维列表,保证不会out of indexself.db = [([2] * 16) for i in range(16)]#悔棋用的顺序列表self.order = []#棋子颜色self.color_count = 0self.color = 'black'#清空与赢的初始化,已赢为1,已清空为1self.flag_win = 1self.flag_empty = 1self.options()#黑白互换def change_color(self) :self.color_count = (self.color_count + 1 ) % 2if self.color_count == 0 :self.color = "black"elif self.color_count ==1 :self.color = "white"#落子def chess_moving(self ,event) :#不点击“开始”与“清空”无法再次开始落子if self.flag_win ==1 or self.flag_empty ==0  :return#坐标转化为下标x,y = event.x-25 , event.y-25x = round(x/30)y = round(y/30)#点击位置没用落子,且没有在棋盘线外,可以落子while self.db[y][x] == 2 and self.limit_boarder(y,x):self.db[y][x] = self.color_countself.order.append(x+15*y)self.board.canvas.create_oval(25+30*x-12 , 25+30*y-12 , 25+30*x+12 , 25+30*y+12 , fill = self.color,tags = "chessman")if self.game_win(y,x,self.color_count) :print(self.color,"获胜")self.game_print.set(self.color+"获胜")else :self.change_color()self.game_print.set("请"+self.color+"落子")#保证棋子落在棋盘上def limit_boarder(self , y , x) :if x<0 or x>14 or y<0 or y>14 :return Falseelse :return True#计算连子的数目,并返回最大连子数目def chessman_count(self , y , x , color_count ) :count1,count2,count3,count4 = 1,1,1,1#横计算for i in range(-1 , -5 , -1) :if self.db[y][x+i] == color_count  :count1 += 1else:breakfor i in  range(1 , 5 ,1 ) :if self.db[y][x+i] == color_count  :count1 += 1else:break#竖计算for i in range(-1 , -5 , -1) :if self.db[y+i][x] == color_count  :count2 += 1else:breakfor i in  range(1 , 5 ,1 ) :if self.db[y+i][x] == color_count  :count2 += 1else:break#/计算for i in range(-1 , -5 , -1) :if self.db[y+i][x+i] == color_count  :count3 += 1else:breakfor i in  range(1 , 5 ,1 ) :if self.db[y+i][x+i] == color_count  :count3 += 1else:break#\计算for i in range(-1 , -5 , -1) :if self.db[y+i][x-i] == color_count :count4 += 1else:breakfor i in  range(1 , 5 ,1 ) :if self.db[y+i][x-i] == color_count :count4 += 1else:breakreturn max(count1 , count2 , count3 , count4)#判断输赢def game_win(self , y , x , color_count ) :if self.chessman_count(y,x,color_count) >= 5 :self.flag_win = 1self.flag_empty = 0return Trueelse :return False#悔棋,清空棋盘,再画剩下的n-1个棋子def withdraw(self ) :if len(self.order)==0 or self.flag_win == 1:returnself.board.canvas.delete("chessman")z = self.order.pop()x = z%15y = z//15self.db[y][x] = 2self.color_count = 1for i in self.order :ix = i%15iy = i//15self.change_color()self.board.canvas.create_oval(25+30*ix-12 , 25+30*iy-12 , 25+30*ix+12 , 25+30*iy+12 , fill = self.color,tags = "chessman")self.change_color()self.game_print.set("请"+self.color+"落子")#清空def empty_all(self) :self.board.canvas.delete("chessman")#还原初始化self.db = [([2] * 16) for i in range(16)]self.order = []self.color_count = 0self.color = 'black'self.flag_win = 1self.flag_empty = 1self.game_print.set("")#将self.flag_win置0才能在棋盘上落子def game_start(self) :#没有清空棋子不能置0开始if self.flag_empty == 0:returnself.flag_win = 0self.game_print.set("请"+self.color+"落子")def options(self) :self.board.canvas.bind("<Button-1>",self.chess_moving)Label(self.board.window , textvariable = self.game_print , font = ("Arial", 20) ).place(relx = 0, rely = 0 ,x = 495 , y = 200)Button(self.board.window , text= "开始游戏" ,command = self.game_start,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=15)Button(self.board.window , text= "我要悔棋" ,command = self.withdraw,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=60)Button(self.board.window , text= "清空棋局" ,command = self.empty_all,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=105)Button(self.board.window , text= "结束游戏" ,command = self.board.window.destroy,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=420)self.board.window.mainloop()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171

最后,main函数

if __name__ == "__main__":game = Gobang()1234

将以上的所有程序复制粘贴,即为完整的程序了,可以运行。最后来一个完整程序,一个一个复制粘贴简直不要太麻烦。

「羊毛+福利」撸一波超便宜的云服务,完成任务DD另外送奖励!

'''
@Auther : gaoxin
@Date : 2019.01.01
@Version : 1.0'''from tkinter import *
import math#定义棋盘类
class chessBoard() :def __init__(self) :self.window = Tk()self.window.title("五子棋游戏")self.window.geometry("660x470")self.window.resizable(0,0)self.canvas=Canvas(self.window , bg="#EEE8AC" , width=470, height=470)self.paint_board()self.canvas.grid(row = 0 , column = 0)def paint_board(self) :for row in range(0,15) :if row == 0 or row == 14 :self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 2)else :self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 1)for column in range(0,15) :if column == 0 or column == 14 :self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 ,width = 2)else :self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 , width = 1)self.canvas.create_oval(112, 112, 118, 118, fill="black")self.canvas.create_oval(352, 112, 358, 118, fill="black")self.canvas.create_oval(112, 352, 118, 358, fill="black")self.canvas.create_oval(232, 232, 238, 238, fill="black")self.canvas.create_oval(352, 352, 358, 358, fill="black")#定义五子棋游戏类
#0为黑子 , 1为白子 , 2为空位
class Gobang() :#初始化def __init__(self) :self.board = chessBoard()self.game_print = StringVar()self.game_print.set("")#16*16的二维列表,保证不会out of indexself.db = [([2] * 16) for i in range(16)]#悔棋用的顺序列表self.order = []#棋子颜色self.color_count = 0self.color = 'black'#清空与赢的初始化,已赢为1,已清空为1self.flag_win = 1self.flag_empty = 1self.options()#黑白互换def change_color(self) :self.color_count = (self.color_count + 1 ) % 2if self.color_count == 0 :self.color = "black"elif self.color_count ==1 :self.color = "white"#落子def chess_moving(self ,event) :#不点击“开始”与“清空”无法再次开始落子if self.flag_win ==1 or self.flag_empty ==0  :return#坐标转化为下标x,y = event.x-25 , event.y-25x = round(x/30)y = round(y/30)#点击位置没用落子,且没有在棋盘线外,可以落子while self.db[y][x] == 2 and self.limit_boarder(y,x):self.db[y][x] = self.color_countself.order.append(x+15*y)self.board.canvas.create_oval(25+30*x-12 , 25+30*y-12 , 25+30*x+12 , 25+30*y+12 , fill = self.color,tags = "chessman")if self.game_win(y,x,self.color_count) :print(self.color,"获胜")self.game_print.set(self.color+"获胜")else :self.change_color()self.game_print.set("请"+self.color+"落子")#保证棋子落在棋盘上def limit_boarder(self , y , x) :if x<0 or x>14 or y<0 or y>14 :return Falseelse :return True#计算连子的数目,并返回最大连子数目def chessman_count(self , y , x , color_count ) :count1,count2,count3,count4 = 1,1,1,1#横计算for i in range(-1 , -5 , -1) :if self.db[y][x+i] == color_count  :count1 += 1else:breakfor i in  range(1 , 5 ,1 ) :if self.db[y][x+i] == color_count  :count1 += 1else:break#竖计算for i in range(-1 , -5 , -1) :if self.db[y+i][x] == color_count  :count2 += 1else:breakfor i in  range(1 , 5 ,1 ) :if self.db[y+i][x] == color_count  :count2 += 1else:break#/计算for i in range(-1 , -5 , -1) :if self.db[y+i][x+i] == color_count  :count3 += 1else:breakfor i in  range(1 , 5 ,1 ) :if self.db[y+i][x+i] == color_count  :count3 += 1else:break#\计算for i in range(-1 , -5 , -1) :if self.db[y+i][x-i] == color_count :count4 += 1else:breakfor i in  range(1 , 5 ,1 ) :if self.db[y+i][x-i] == color_count :count4 += 1else:breakreturn max(count1 , count2 , count3 , count4)#判断输赢def game_win(self , y , x , color_count ) :if self.chessman_count(y,x,color_count) >= 5 :self.flag_win = 1self.flag_empty = 0return Trueelse :return False#悔棋,清空棋盘,再画剩下的n-1个棋子def withdraw(self ) :if len(self.order)==0 or self.flag_win == 1:returnself.board.canvas.delete("chessman")z = self.order.pop()x = z%15y = z//15self.db[y][x] = 2self.color_count = 1for i in self.order :ix = i%15iy = i//15self.change_color()self.board.canvas.create_oval(25+30*ix-12 , 25+30*iy-12 , 25+30*ix+12 , 25+30*iy+12 , fill = self.color,tags = "chessman")self.change_color()self.game_print.set("请"+self.color+"落子")#清空def empty_all(self) :self.board.canvas.delete("chessman")#还原初始化self.db = [([2] * 16) for i in range(16)]self.order = []self.color_count = 0self.color = 'black'self.flag_win = 1self.flag_empty = 1self.game_print.set("")#将self.flag_win置0才能在棋盘上落子def game_start(self) :#没有清空棋子不能置0开始if self.flag_empty == 0:returnself.flag_win = 0self.game_print.set("请"+self.color+"落子")def options(self) :self.board.canvas.bind("<Button-1>",self.chess_moving)Label(self.board.window , textvariable = self.game_print , font = ("Arial", 20) ).place(relx = 0, rely = 0 ,x = 495 , y = 200)Button(self.board.window , text= "开始游戏" ,command = self.game_start,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=15)Button(self.board.window , text= "我要悔棋" ,command = self.withdraw,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=60)Button(self.board.window , text= "清空棋局" ,command = self.empty_all,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=105)Button(self.board.window , text= "结束游戏" ,command = self.board.window.destroy,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=420)self.board.window.mainloop()if __name__ == "__main__":game = Gobang()


往期推荐

离职半年了,最近又开始被吐槽输出不够...

赠书:支付平台架构业务、规划、设计与实现

「羊毛+福利」撸一波超便宜的云服务,完成任务DD另外送奖励!

用 gson 替换 fastjson 引发的线上问题分析

靠,上班打游戏!不,我只是在Minecraft里管理Kubernetes...

超牛逼的 Feed 流系统设计!

扫一扫,关注我

一起学习,一起进步

每周赠书,福利不断

深度内容

推荐加入

最近热门内容回顾   #社会人系列

实现单机五子棋,难吗?相关推荐

  1. 人与人之间的单机五子棋 —— C语言实现

    人与人之间的单机五子棋 每博一文案 作家苏曾说,到了现在这个年纪,谁都不想再取悦了,跟谁在一起舒服就和 谁在一起,包括朋友也是,累了,就躲远一点.人活着总是离不开圈子二字. 为了扩大自己的社交圈,参见 ...

  2. 用python做双人五子棋_基于python的socket实现单机五子棋到双人对战

    基于python的socket实现单机五子棋到双人对战,供大家参考,具体内容如下 本次实验使用python语言.通过socket进行不同机器见的通信,具体可以分为以下四步:1.创建ServerSock ...

  3. python五子棋游戏from tkinter import_Python tkinter制作单机五子棋游戏

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 以下文章来源于Python家庭,作者Python家庭 实战项目:使用Python ...

  4. python 单机程序_Python tkinter制作单机五子棋游戏

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 以下文章来源于Python家庭,作者Python家庭 实战项目:使用Python ...

  5. Android实训案例(八)——单机五子棋游戏,自定义棋盘,线条,棋子,游戏逻辑,游戏状态存储,再来一局

    Android实训案例(八)--单机五子棋游戏,自定义棋盘,线条,棋子,游戏逻辑,游戏状态存储,再来一局 阿法狗让围棋突然就被热议了,鸿洋大神也顺势出了篇五子棋单机游戏的视频,我看到了就像膜拜膜拜,就 ...

  6. 基于python的socket实现单机五子棋到双人对战

    基于python的socket实现单机五子棋到双人对战 本次实验使用python语言.通过socket进行不同机器见的通信,具体可以分为以下四步:1.创建ServerSocket和Socket:2.打 ...

  7. python单机五子棋详解(tkinter)

    python单机五子棋详解(tkinter) 简介 样式创建 逻辑编写 简介 这是实验室2018年底招新时的考核题目,使用Python编写一个能够完成基本对战的五子棋游戏.面向新手. 程序主要包括两个 ...

  8. Java实现单机五子棋,含完整代码

    文章目录 五子棋!! 实现功能 改进方向 主体思路 遇到的困难 完整代码 五子棋!! 实现功能 基本的棋盘绘制,重绘,输赢判断,悔棋,重新开始 改进方向 添加背景音乐,背景图片美化,用棋子图片代替原棋 ...

  9. python五子棋单机版源代码_python实现单机五子棋

    简介 这是实验室2018年底招新时的考核题目,使用Python编写一个能够完成基本对战的五子棋游戏.面向新手. 程序主要包括两个部分,图形创建与逻辑编写两部分. 程序的运行结果: 样式创建 老规矩,先 ...

最新文章

  1. 深入掌握Java技术 EJB调用原理分析
  2. python怎么发送邮件_python中是如何借助smtp协议发送邮件的?
  3. 相同的字符串哈希值一样吗_关于哈希,来看这里!
  4. 加减法叫做什么运算_小学四则运算基础知识,赶快给孩子存下吧!
  5. arm处理器的历史及现状
  6. jquery标题左右移动动画
  7. 面试真题------hashmap与hashset
  8. 《Effective Debugging:软件和系统调试的66个有效方法》——导读
  9. SQL极限函数limit()详解分页必备
  10. php strcmp bypass漏洞
  11. 2022中山大学计算机技术专硕考研初试、复试经验帖
  12. Spring----pom.xml报错Missing artifact org.aspectj:aspectjweaver:jar:1.8.0.M1
  13. matlab二极管伏安特性,基于Matlab对Spice二极管特性受温度影响的研究
  14. linux双机热备软件 mysql,Linux Mysql 双机热备安装详解
  15. When Work Becomes a Game
  16. Godaddy绑定手机遗失,成功申诉取消手机两步验证全过程
  17. 将腾讯云对象存储挂载到云服务器
  18. 搭建各类游戏如何选择合适的服务器
  19. git免密pull指定ssh密钥文件
  20. ipad已有2周未备份。ipad插入电源、被锁定且接入Wi-Fi时会进行备份

热门文章

  1. Atlas Samples Suse Linux 10.1
  2. hadoop完全分布式集群安装
  3. java 反序列化利用工具 marshalsec 使用简介
  4. linux 查看 占用内存最多 占用cpu最多 程序
  5. C发展史:KR C/C89/C99/C11 C++发展史: C++98/C++03/C++11
  6. uclinux与linux的区别
  7. VS中编译64位程序以及遇到的问题(E0000235)
  8. VFS文件系统结构分析 与socket
  9. 监控mysql的shell脚本_监控MySQL主从状态的shell脚本
  10. c语言map作为参数传递,C++中map和vector作形参时如何给定默认参数?