python实现单机五子棋


Posted in Python onAugust 28, 2020

简介

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

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

程序的运行结果:

python实现单机五子棋

样式创建

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

'''
@Auther : gaoxin
@Date : 2019.01.01
@Version : 1.0
'''

from tkinter import *
import math

然后建立一个样式的类,类名称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")

逻辑编写

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

#定义五子棋游戏类
#0为黑子 , 1为白子 , 2为空位
class Gobang() :
 #初始化
 def __init__(self) :
  self.board = chessBoard()
  self.game_print = StringVar()
  self.game_print.set("")
  #16*16的二维列表,保证不会out of index
  self.db = [([2] * 16) for i in range(16)]
  #悔棋用的顺序列表
  self.order = []
  #棋子颜色
  self.color_count = 0 
  self.color = 'black'
  #清空与赢的初始化,已赢为1,已清空为1
  self.flag_win = 1
  self.flag_empty = 1
  self.options()
  
  
 #黑白互换
 def change_color(self) :
  self.color_count = (self.color_count + 1 ) % 2
  if 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-25
  x = 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_count
   self.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 False
  else :
   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 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y][x+i] == color_count :
    count1 += 1
   else:
    break 
  #竖计算
  for i in range(-1 , -5 , -1) :
   if self.db[y+i][x] == color_count :
    count2 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y+i][x] == color_count :
    count2 += 1
   else:
    break 
  #/计算
  for i in range(-1 , -5 , -1) :
   if self.db[y+i][x+i] == color_count :
    count3 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y+i][x+i] == color_count :
    count3 += 1
   else:
    break 
  #\计算
  for i in range(-1 , -5 , -1) :
   if self.db[y+i][x-i] == color_count :
    count4 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y+i][x-i] == color_count :
    count4 += 1
   else:
    break 
   
  return 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 = 1
   self.flag_empty = 0
   return True
  else :
   return False
  

 #悔棋,清空棋盘,再画剩下的n-1个棋子
 def withdraw(self ) :
  if len(self.order)==0 or self.flag_win == 1:
   return
  self.board.canvas.delete("chessman")
  z = self.order.pop()
  x = z%15
  y = z//15
  self.db[y][x] = 2
  self.color_count = 1
  for i in self.order :
   ix = i%15
   iy = i//15
   self.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 = 0 
  self.color = 'black'
  self.flag_win = 1
  self.flag_empty = 1
  self.game_print.set("")


 #将self.flag_win置0才能在棋盘上落子
 def game_start(self) :
  #没有清空棋子不能置0开始
  if self.flag_empty == 0:
   return
  self.flag_win = 0
  self.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()

最后,main函数

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

将以上的所有程序复制粘贴,即为完整的程序了,可以运行。

最后来一个完整程序,一个一个复制粘贴简直不要太麻烦。

'''
@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 index
  self.db = [([2] * 16) for i in range(16)]
  #悔棋用的顺序列表
  self.order = []
  #棋子颜色
  self.color_count = 0 
  self.color = 'black'
  #清空与赢的初始化,已赢为1,已清空为1
  self.flag_win = 1
  self.flag_empty = 1
  self.options()
  
  
 #黑白互换
 def change_color(self) :
  self.color_count = (self.color_count + 1 ) % 2
  if 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-25
  x = 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_count
   self.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 False
  else :
   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 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y][x+i] == color_count :
    count1 += 1
   else:
    break 
  #竖计算
  for i in range(-1 , -5 , -1) :
   if self.db[y+i][x] == color_count :
    count2 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y+i][x] == color_count :
    count2 += 1
   else:
    break 
  #/计算
  for i in range(-1 , -5 , -1) :
   if self.db[y+i][x+i] == color_count :
    count3 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y+i][x+i] == color_count :
    count3 += 1
   else:
    break 
  #\计算
  for i in range(-1 , -5 , -1) :
   if self.db[y+i][x-i] == color_count :
    count4 += 1
   else:
    break 
  for i in range(1 , 5 ,1 ) :
   if self.db[y+i][x-i] == color_count :
    count4 += 1
   else:
    break 
   
  return 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 = 1
   self.flag_empty = 0
   return True
  else :
   return False
  

 #悔棋,清空棋盘,再画剩下的n-1个棋子
 def withdraw(self ) :
  if len(self.order)==0 or self.flag_win == 1:
   return
  self.board.canvas.delete("chessman")
  z = self.order.pop()
  x = z%15
  y = z//15
  self.db[y][x] = 2
  self.color_count = 1
  for i in self.order :
   ix = i%15
   iy = i//15
   self.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 = 0 
  self.color = 'black'
  self.flag_win = 1
  self.flag_empty = 1
  self.game_print.set("")


 #将self.flag_win置0才能在棋盘上落子
 def game_start(self) :
  #没有清空棋子不能置0开始
  if self.flag_empty == 0:
   return
  self.flag_win = 0
  self.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()

更多有趣的经典小游戏实现专题,分享给大家:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
书单|人生苦短,你还不用python!
Dec 29 Python
python实现简单遗传算法
Mar 19 Python
Python爬虫实现全国失信被执行人名单查询功能示例
May 03 Python
Python chardet库识别编码原理解析
Feb 18 Python
python修改linux中文件(文件夹)的权限属性操作
Mar 05 Python
三步解决python PermissionError: [WinError 5]拒绝访问的情况
Apr 22 Python
Django框架安装及项目创建过程解析
Sep 14 Python
python判断字符串以什么结尾的实例方法
Sep 18 Python
Opencv python 图片生成视频的方法示例
Nov 18 Python
Django自带的用户验证系统实现
Dec 18 Python
通用的Django注册功能模块实现方法
Feb 05 Python
Python实战之OpenCV实现猫脸检测
Jun 26 Python
Python3+selenium配置常见报错解决方案
Aug 28 #Python
Python 中如何写注释
Aug 28 #Python
Python操作Word批量生成合同的实现示例
Aug 28 #Python
Python接口自动化测试的实现
Aug 28 #Python
解决python和pycharm安装gmpy2 出现ERROR的问题
Aug 28 #Python
Python自动登录QQ的实现示例
Aug 28 #Python
python opencv pytesseract 验证码识别的实现
Aug 28 #Python
You might like
一些操作和快捷键的理解和讨论
2020/03/04 星际争霸
比特率,大家看看这个就不用收音机音质去比MP3音质了
2021/03/01 无线电
PHP中return 和 exit 、break和contiue 区别与用法
2012/04/09 PHP
解析:php调用MsSQL存储过程使用内置RETVAL获取过程中的return值
2013/07/03 PHP
PHP使用strrev翻转中文乱码问题的解决方法
2017/01/13 PHP
Aster vs KG BO3 第二场2.18
2021/03/10 DOTA
如何实现iframe(嵌入式帧)的自适应高度
2006/07/26 Javascript
javascript中的一些注意事项 更新中
2010/12/06 Javascript
jquery怎样实现ajax联动框(一)
2013/03/08 Javascript
javascript中var的重要性分析
2015/02/11 Javascript
JS+CSS实现下拉列表框美化效果(3款)
2015/08/15 Javascript
原生js实现图片层叠轮播切换效果
2016/02/02 Javascript
微信小程序登录session的使用
2019/03/17 Javascript
原生JS实现列表内容自动向上滚动效果
2019/05/22 Javascript
为vue项目自动设置请求状态的配置方法
2019/06/09 Javascript
详解钉钉小程序组件之自定义模态框(弹窗封装实现)
2020/03/07 Javascript
JS如何实现封装列表右滑动删除收藏按钮
2020/07/23 Javascript
js实现随机点名
2021/01/19 Javascript
[01:07:13]TNC vs Pain 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
python实现多张图片拼接成大图
2019/01/15 Python
pyqt5 lineEdit设置密码隐藏,删除lineEdit已输入的内容等属性方法
2019/06/24 Python
python获取array中指定元素的示例
2019/11/26 Python
Python selenium的基本使用方法分析
2019/12/21 Python
520使用Python实现“我爱你”表白
2020/05/20 Python
Python unittest基本使用方法代码实例
2020/06/29 Python
windows+vscode安装paddleOCR运行环境的步骤
2020/11/11 Python
Html5内唤醒百度、高德APP的实现示例
2019/05/20 HTML / CSS
英国比较机场停车场网站:Airport Parking Essentials
2019/12/01 全球购物
电大自我鉴定范文
2013/10/01 职场文书
护士岗位求职应聘自荐书范文
2014/02/12 职场文书
幼儿园教师节活动总结
2015/03/23 职场文书
英文慰问信范文
2015/03/24 职场文书
毕业论文答辩开场白和结束语
2015/05/27 职场文书
地心历险记观后感
2015/06/15 职场文书
工作表现证明
2015/06/15 职场文书
2017年寒假社区服务活动总结
2016/04/06 职场文书