python实现单机五子棋
作者:东流去也 发布时间:2021-04-26 05:34:35
标签:python,五子棋
简介
这是实验室2018年底招新时的考核题目,使用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()
更多有趣的经典小游戏实现专题,分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏 玩不停
javascript经典小游戏汇总
来源:https://blog.csdn.net/gaosanjin/article/details/108244164
0
投稿
猜你喜欢
- 数据库和操作系统一样,是一个多用户使用的共享资源。当多个用户并发地存取数据 时,在数据库中就会产生多个事务同时存取同一数据的情况。若对并发操
- mysql分组统计并求出百分比1、mysql 分组统计并列出百分比SELECT point_id,&nb
- 这篇文章主要介绍了简单了解为什么python函数后有多个括号,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- Python简介及入门python为什么是python选择自己喜欢的语言,这往往不容易,更多的是根据需求话说,之前是java,大学用了三年+
- 1、安装MSSQL时使用混合模式,当然SA密码最好不能为空,在SQL2005中,可以对SA这个超级用户名进行修改或删除。use master
- 现在大部分网站都使用asp+access构建,这样的话通过下载access数据库简单就可以对网站进行破坏! 而很多的网站都不太重
- 本文实例讲述了Django框架文件上传与自定义图片上传路径、上传文件名操作。分享给大家供大家参考,具体如下:文件上传1、创建上传文件夹在st
- 什么是JSONP首先提一下JSON这个概念,JSON是一种轻量级的数据传输格式,被广泛应用于当前Web应用中。JSON格式数据的编码和解析基
- 传染源: 野生动物,可能为中华菊头蝠病毒: 新型冠状病毒 2019-nCoV传播途径: 经呼吸道飞沫传播,亦可
- 判断文件是否存在os.IsExists()函数和os.IsNotExists(),他们的函数的原形是func IsExist(err err
- 如果在session级保存一个dictionary对象会降低系统的性能,而在application级保存一个dictionary对象会导致w
- 一、前言MySQL 的锁按照范围可以分为全局锁、表锁、行锁,其中行锁是由数据库引擎实现的,并不是所有的引擎都提供行锁,MyISAM 就不支持
- post, put请求的参数有两种形式一种是把参数拼接在url中 对应postman第二种是把参数放在body中 对应postman在Pyt
- Python 超简单的聊天程序客户端:import socket, syshost = '10.248.27.23'# ho
- 在使用数据库的时候,难免要在使用过程中进行删除的操作,如果是使用int类型的字段,令其自增长,这是个最简单的办法,但是后果会有些不是你想要的
- 本文实例讲述了Python类的用法。分享给大家供大家参考。具体如下:先看一段代码:#!/usr/bin/env pythonclass Te
- 本文介绍以下内容:1. 使用transformers框架做预训练的bert-base模型;2. 开发平台使用Google的Colab平台,白
- 说明1、在使用之前需要在settings中打开。2、pipeline在settings中键表示位置(即pipeline在项目中的位置可以自定
- 下面把代码写出来,希望大家批评指正. 首先domain对象.在这里使用的注解的方式,都是比较新的版本. User.java package
- 1.安装MySql目前MySQL有两种形式的文件,一个是msi格式,一个是zip格式的。msi格式的直接点击setup.exe就好,按照步骤