1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python开发 实现单机五子棋!

python开发 实现单机五子棋!

时间:2022-11-20 02:26:36

相关推荐

python开发 实现单机五子棋!

这篇文章主要为大家详细介绍了python实现单机五子棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

简介

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

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

程序的运行结果:

样式创建

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

"""

@Auther : gaoxin

@Date : .01.01

@Version : 1.0

"""

fromtkinterimport*

importmath

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

#定义棋盘类

classchessBoard():

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)

defpaint_board(self):

#画横线

forrowinrange(0,15):

ifrow==0orrow==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)

#画竖线

forcolumninrange(0,15):

ifcolumn==0orcolumn==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为空位

classGobang():

#初始化

def__init__(self):

self.board=chessBoard()

self.game_print=StringVar()

self.game_print.set("")

#16*16的二维列表,保证不会out of index

self.db=[([2]*16)foriinrange(16)]

#悔棋用的顺序列表

self.order=[]

#棋子颜色

self.color_count=0

self.color="black"

#清空与赢的初始化,已赢为1,已清空为1

self.flag_win=1

self.flag_empty=1

self.options()

#黑白互换

defchange_color(self):

self.color_count=(self.color_count+1)%2

ifself.color_count==0:

self.color="black"

elifself.color_count==1:

self.color="white"

#落子

defchess_moving(self,event):

#不点击“开始”与“清空”无法再次开始落子

ifself.flag_win==1orself.flag_empty==0:

return

#坐标转化为下标

x,y=event.x-25,event.y-25

x=round(x/30)

y=round(y/30)

#点击位置没用落子,且没有在棋盘线外,可以落子

whileself.db[y][x]==2andself.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")

ifself.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+"落子")

#保证棋子落在棋盘上

deflimit_boarder(self,y,x):

ifx<0orx>14ory<0ory>14:

returnFalse

else:

returnTrue

#计算连子的数目,并返回最大连子数目

defchessman_count(self,y,x,color_count):

count1,count2,count3,count4=1,1,1,1

#横计算

foriinrange(-1,-5,-1):

ifself.db[y][x+i]==color_count:

count1+=1

else:

break

foriinrange(1,5,1):

ifself.db[y][x+i]==color_count:

count1+=1

else:

break

#竖计算

foriinrange(-1,-5,-1):

ifself.db[y+i][x]==color_count:

count2+=1

else:

break

foriinrange(1,5,1):

ifself.db[y+i][x]==color_count:

count2+=1

else:

break

#/计算

foriinrange(-1,-5,-1):

ifself.db[y+i][x+i]==color_count:

count3+=1

else:

break

foriinrange(1,5,1):

ifself.db[y+i][x+i]==color_count:

count3+=1

else:

break

#\计算

foriinrange(-1,-5,-1):

ifself.db[y+i][x-i]==color_count:

count4+=1

else:

break

foriinrange(1,5,1):

ifself.db[y+i][x-i]==color_count:

count4+=1

else:

break

returnmax(count1,count2,count3,count4)

#判断输赢

defgame_win(self,y,x,color_count):

ifself.chessman_count(y,x,color_count)>=5:

self.flag_win=1

self.flag_empty=0

returnTrue

else:

returnFalse

#悔棋,清空棋盘,再画剩下的n-1个棋子

defwithdraw(self):

iflen(self.order)==0orself.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

foriinself.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+"落子")

#清空

defempty_all(self):

self.board.canvas.delete("chessman")

#还原初始化

self.db=[([2]*16)foriinrange(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才能在棋盘上落子

defgame_start(self):

#没有清空棋子不能置0开始

ifself.flag_empty==0:

return

self.flag_win=0

self.game_print.set("请"+self.color+"落子")

defoptions(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 : .01.01

@Version : 1.0

"""

fromtkinterimport*

importmath

#定义棋盘类

classchessBoard():

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)

defpaint_board(self):

forrowinrange(0,15):

ifrow==0orrow==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)

forcolumninrange(0,15):

ifcolumn==0orcolumn==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为空位

classGobang():

#初始化

def__init__(self):

self.board=chessBoard()

self.game_print=StringVar()

self.game_print.set("")

#16*16的二维列表,保证不会out of index

self.db=[([2]*16)foriinrange(16)]

#悔棋用的顺序列表

self.order=[]

#棋子颜色

self.color_count=0

self.color="black"

#清空与赢的初始化,已赢为1,已清空为1

self.flag_win=1

self.flag_empty=1

self.options()

#黑白互换

defchange_color(self):

self.color_count=(self.color_count+1)%2

ifself.color_count==0:

self.color="black"

elifself.color_count==1:

self.color="white"

#落子

defchess_moving(self,event):

#不点击“开始”与“清空”无法再次开始落子

ifself.flag_win==1orself.flag_empty==0:

return

#坐标转化为下标

x,y=event.x-25,event.y-25

x=round(x/30)

y=round(y/30)

#点击位置没用落子,且没有在棋盘线外,可以落子

whileself.db[y][x]==2andself.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")

ifself.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+"落子")

#保证棋子落在棋盘上

deflimit_boarder(self,y,x):

ifx<0orx>14ory<0ory>14:

returnFalse

else:

returnTrue

#计算连子的数目,并返回最大连子数目

defchessman_count(self,y,x,color_count):

count1,count2,count3,count4=1,1,1,1

#横计算

foriinrange(-1,-5,-1):

ifself.db[y][x+i]==color_count:

count1+=1

else:

break

foriinrange(1,5,1):

ifself.db[y][x+i]==color_count:

count1+=1

else:

break

#竖计算

foriinrange(-1,-5,-1):

ifself.db[y+i][x]==color_count:

count2+=1

else:

break

foriinrange(1,5,1):

ifself.db[y+i][x]==color_count:

count2+=1

else:

break

#/计算

foriinrange(-1,-5,-1):

ifself.db[y+i][x+i]==color_count:

count3+=1

else:

break

foriinrange(1,5,1):

ifself.db[y+i][x+i]==color_count:

count3+=1

else:

break

#\计算

foriinrange(-1,-5,-1):

ifself.db[y+i][x-i]==color_count:

count4+=1

else:

break

foriinrange(1,5,1):

ifself.db[y+i][x-i]==color_count:

count4+=1

else:

break

returnmax(count1,count2,count3,count4)

#判断输赢

defgame_win(self,y,x,color_count):

ifself.chessman_count(y,x,color_count)>=5:

self.flag_win=1

self.flag_empty=0

returnTrue

else:

returnFalse

#悔棋,清空棋盘,再画剩下的n-1个棋子

defwithdraw(self):

iflen(self.order)==0orself.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

foriinself.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+"落子")

#清空

defempty_all(self):

self.board.canvas.delete("chessman")

#还原初始化

self.db=[([2]*16)foriinrange(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才能在棋盘上落子

defgame_start(self):

#没有清空棋子不能置0开始

ifself.flag_empty==0:

return

self.flag_win=0

self.game_print.set("请"+self.color+"落子")

defoptions(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工作五年月薪23K,记录一下我的学习经历建议

-------------------End-------------------

欢迎大家点赞,留言,转发,转载,感谢大家的相伴与支持

万水千山总是情,点个【在看】行不行

*声明:本文于网络整理,版权归原作者所有,如来源信息有误或侵犯权益,请联系我们删除或授权事宜。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。