python實(shí)現(xiàn)單機(jī)五子棋
簡(jiǎn)介
這是實(shí)驗(yàn)室2018年底招新時(shí)的考核題目,使用Python編寫(xiě)一個(gè)能夠完成基本對(duì)戰(zhàn)的五子棋游戲。面向新手。
程序主要包括兩個(gè)部分,圖形創(chuàng)建與邏輯編寫(xiě)兩部分。
程序的運(yùn)行結(jié)果:
樣式創(chuàng)建
老規(guī)矩,先把用到的包導(dǎo)入進(jìn)來(lái)。
''' @Auther : gaoxin @Date : 2019.01.01 @Version : 1.0 ''' from tkinter import * import math
然后建立一個(gè)樣式的類(lèi),類(lèi)名稱(chēng)chessBoard。這里加了很多注釋?zhuān)苊庑率挚床欢瘮?shù)的作用,說(shuō)實(shí)話我覺(jué)得挺別扭的。
#定義棋盤(pán)類(lèi) class chessBoard() : def __init__(self) : #創(chuàng)建一個(gè)tk對(duì)象,即窗口 self.window = Tk() #窗口命名 self.window.title("五子棋游戲") #定義窗口大小 self.window.geometry("660x470") #定義窗口不可放縮 self.window.resizable(0,0) #定義窗口里的畫(huà)布 self.canvas=Canvas(self.window , bg="#EEE8AC" , width=470, height=470) #畫(huà)出畫(huà)布內(nèi)容 self.paint_board() #定義畫(huà)布所在的網(wǎng)格 self.canvas.grid(row = 0 , column = 0) def paint_board(self) : #畫(huà)橫線 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) #畫(huà)豎線 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) #畫(huà)圓 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")
邏輯編寫(xiě)
這里的主要看每個(gè)函數(shù)的功能就好了。
#定義五子棋游戲類(lèi) #0為黑子 , 1為白子 , 2為空位 class Gobang() : #初始化 def __init__(self) : self.board = chessBoard() self.game_print = StringVar() self.game_print.set("") #16*16的二維列表,保證不會(huì)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) : #不點(diǎn)擊“開(kāi)始”與“清空”無(wú)法再次開(kāi)始落子 if self.flag_win ==1 or self.flag_empty ==0 : return #坐標(biāo)轉(zhuǎn)化為下標(biāo) x,y = event.x-25 , event.y-25 x = round(x/30) y = round(y/30) #點(diǎn)擊位置沒(méi)用落子,且沒(méi)有在棋盤(pán)線外,可以落子 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("請(qǐng)"+self.color+"落子") #保證棋子落在棋盤(pán)上 def limit_boarder(self , y , x) : if x<0 or x>14 or y<0 or y>14 : return False else : return True #計(jì)算連子的數(shù)目,并返回最大連子數(shù)目 def chessman_count(self , y , x , color_count ) : count1,count2,count3,count4 = 1,1,1,1 #橫計(jì)算 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 #豎計(jì)算 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 #/計(jì)算 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 #\計(jì)算 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 #悔棋,清空棋盤(pán),再畫(huà)剩下的n-1個(gè)棋子 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("請(qǐng)"+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才能在棋盤(pán)上落子 def game_start(self) : #沒(méi)有清空棋子不能置0開(kāi)始 if self.flag_empty == 0: return self.flag_win = 0 self.game_print.set("請(qǐng)"+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= "開(kāi)始游戲" ,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= "結(jié)束游戲" ,command = self.board.window.destroy,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=420) self.board.window.mainloop()
最后,main函數(shù)
if __name__ == "__main__": game = Gobang()
將以上的所有程序復(fù)制粘貼,即為完整的程序了,可以運(yùn)行。
最后來(lái)一個(gè)完整程序,一個(gè)一個(gè)復(fù)制粘貼簡(jiǎn)直不要太麻煩。
''' @Auther : gaoxin @Date : 2019.01.01 @Version : 1.0 ''' from tkinter import * import math #定義棋盤(pán)類(lèi) 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") #定義五子棋游戲類(lèi) #0為黑子 , 1為白子 , 2為空位 class Gobang() : #初始化 def __init__(self) : self.board = chessBoard() self.game_print = StringVar() self.game_print.set("") #16*16的二維列表,保證不會(huì)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) : #不點(diǎn)擊“開(kāi)始”與“清空”無(wú)法再次開(kāi)始落子 if self.flag_win ==1 or self.flag_empty ==0 : return #坐標(biāo)轉(zhuǎn)化為下標(biāo) x,y = event.x-25 , event.y-25 x = round(x/30) y = round(y/30) #點(diǎn)擊位置沒(méi)用落子,且沒(méi)有在棋盤(pán)線外,可以落子 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("請(qǐng)"+self.color+"落子") #保證棋子落在棋盤(pán)上 def limit_boarder(self , y , x) : if x<0 or x>14 or y<0 or y>14 : return False else : return True #計(jì)算連子的數(shù)目,并返回最大連子數(shù)目 def chessman_count(self , y , x , color_count ) : count1,count2,count3,count4 = 1,1,1,1 #橫計(jì)算 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 #豎計(jì)算 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 #/計(jì)算 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 #\計(jì)算 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 #悔棋,清空棋盤(pán),再畫(huà)剩下的n-1個(gè)棋子 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("請(qǐng)"+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才能在棋盤(pán)上落子 def game_start(self) : #沒(méi)有清空棋子不能置0開(kāi)始 if self.flag_empty == 0: return self.flag_win = 0 self.game_print.set("請(qǐng)"+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= "開(kāi)始游戲" ,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= "結(jié)束游戲" ,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()
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專(zhuān)題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PyTorch中的squeeze()和unsqueeze()解析與應(yīng)用案例
這篇文章主要介紹了PyTorch中的squeeze()和unsqueeze()解析與應(yīng)用案例,文章內(nèi)容介紹詳細(xì),需要的小伙伴可以參考一下,希望對(duì)你有所幫助2022-03-03解決Jupyter Notebook使用parser.parse_args出現(xiàn)錯(cuò)誤問(wèn)題
這篇文章主要介紹了解決Jupyter Notebook使用parser.parse_args出現(xiàn)錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04python利用OpenCV2實(shí)現(xiàn)人臉檢測(cè)
這篇文章主要為大家詳細(xì)介紹了python利用OpenCV2實(shí)現(xiàn)人臉檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12python3 常見(jiàn)解密加密算法實(shí)例分析【base64、MD5等】
這篇文章主要介紹了python3 常見(jiàn)解密加密算法,結(jié)合實(shí)例形式分析了Python的base64模塊加密,以及基于pycrypto模塊的MD5加密等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12python中創(chuàng)建以及刪除虛擬環(huán)境的幾種方法總結(jié)
在Python?中創(chuàng)建虛擬環(huán)境非常容易,但是刪除虛擬環(huán)境可能會(huì)有一些挑戰(zhàn),這篇文章主要給大家介紹了關(guān)于python中創(chuàng)建以及刪除虛擬環(huán)境的幾種方法,需要的朋友可以參考下2024-03-03python實(shí)現(xiàn)每次處理一個(gè)字符的三種方法
這篇文章主要介紹了python實(shí)現(xiàn)每次處理一個(gè)字符的三種方法,是非常實(shí)用的字符串操作技巧,需要的朋友可以參考下2014-10-10python 遞歸深度優(yōu)先搜索與廣度優(yōu)先搜索算法模擬實(shí)現(xiàn)
這篇文章主要介紹了python 遞歸深度優(yōu)先搜索與廣度優(yōu)先搜索算法模擬實(shí)現(xiàn) ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10