电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

python實現(xiàn)單機五子棋

瀏覽:5日期:2022-07-13 08:01:58

簡介

這是實驗室2018年底招新時的考核題目,使用Python編寫一個能夠完成基本對戰(zhàn)的五子棋游戲。面向新手。

程序主要包括兩個部分,圖形創(chuàng)建與邏輯編寫兩部分。

程序的運行結(jié)果:

python實現(xiàn)單機五子棋

樣式創(chuàng)建

老規(guī)矩,先把用到的包導(dǎo)入進來。

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

然后建立一個樣式的類,類名稱chessBoard。這里加了很多注釋,避免新手看不懂函數(shù)的作用,說實話我覺得挺別扭的。

#定義棋盤類class chessBoard() : def __init__(self) : #創(chuàng)建一個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) #畫出畫布內(nèi)容 self.paint_board() #定義畫布所在的網(wǎng)格 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')

邏輯編寫

這里的主要看每個函數(shù)的功能就好了。

#定義五子棋游戲類#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 #坐標轉(zhuǎn)化為下標 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 #計算連子的數(shù)目,并返回最大連子數(shù)目 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= '結(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ù)制粘貼,即為完整的程序了,可以運行。

最后來一個完整程序,一個一個復(fù)制粘貼簡直不要太麻煩。

’’’@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 #坐標轉(zhuǎn)化為下標 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 #計算連子的數(shù)目,并返回最大連子數(shù)目 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= '結(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)專題,分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

javascript經(jīng)典小游戲匯總

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 盘煤仪,盘料仪,盘点仪,堆料测量仪,便携式激光盘煤仪-中科航宇(北京)自动化工程技术有限公司 | 国产频谱分析仪-国产网络分析仪-上海坚融实业有限公司 | 电销卡_北京电销卡_包月电话卡-豪付网络 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 天命文免费算命堂_自助算命_自由算命系统_长文周易 | 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | 全自动不干胶贴标机_套标机-上海今昂贴标机生产厂家 | 水平垂直燃烧试验仪-灼热丝试验仪-漏电起痕试验仪-针焰试验仪-塑料材料燃烧检测设备-IP防水试验机 | 真空干燥烘箱_鼓风干燥箱 _高低温恒温恒湿试验箱_光照二氧化碳恒温培养箱-上海航佩仪器 | 济南轻型钢结构/济南铁艺护栏/济南铁艺大门-济南燕翔铁艺制品有限公司 | 青岛侦探_青岛侦探事务所_青岛劝退小三_青岛调查出轨取证公司_青岛婚外情取证-青岛探真调查事务所 | 水厂自动化-水厂控制系统-泵站自动化|控制系统-闸门自动化控制-济南华通中控科技有限公司 | 科昊仪器超纯水机系统-可成气相液氮罐-美菱超低温冰箱-西安昊兴生物科技有限公司 | 深圳宣传片制作_产品视频制作_深圳3D动画制作公司_深圳短视频拍摄-深圳市西典映画传媒有限公司 | 清洁设备_洗地机/扫地机厂家_全自动洗地机_橙犀清洁设备官网 | 大数据营销公司_舆情监测软件_上海SEO公司-文军营销官网 | 刺绳_刀片刺网_刺丝滚笼_不锈钢刺绳生产厂家_安平县浩荣金属丝网制品有限公司-安平县浩荣金属丝网制品有限公司 | 高效复合碳源-多核碳源生产厂家-污水处理反硝化菌种一长隆科技库巴鲁 | 亮化工程,亮化设计,城市亮化工程,亮化资质合作,长沙亮化照明,杰奥思【官网】 | 火锅加盟_四川成都火锅店加盟_中国火锅连锁品牌十强_朝天门火锅【官网】 | 电采暖锅炉_超低温空气源热泵_空气源热水器-鑫鲁禹电锅炉空气能热泵厂家 | 商秀—企业短视频代运营_抖音企业号托管 | 动力配电箱-不锈钢配电箱-高压开关柜-重庆宇轩机电设备有限公司 聚天冬氨酸,亚氨基二琥珀酸四钠,PASP,IDS - 远联化工 | 立式壁挂广告机厂家-红外电容触摸一体机价格-华邦瀛 | 中国在职研究生招生信息网| 钣金加工厂家-钣金加工-佛山钣金厂-月汇好 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 全自动贴标机-套标机-工业热风机-不干胶贴标机-上海厚冉机械 | 小型铜米机-干式铜米机-杂线全自动铜米机-河南鑫世昌机械制造有限公司 | 清水-铝合金-建筑模板厂家-木模板价格-铝模板生产「五棵松」品牌 | 黑龙江「京科脑康」医院-哈尔滨失眠医院_哈尔滨治疗抑郁症医院_哈尔滨精神心理医院 | 长沙广告公司|长沙广告制作设计|长沙led灯箱招牌制作找望城湖南锦蓝广告装饰工程有限公司 | 深圳货架厂_仓库货架公司_重型仓储货架_线棒货架批发-深圳市诺普泰仓储设备有限公司 | 合肥风管加工厂-安徽螺旋/不锈钢风管-通风管道加工厂家-安徽风之范 | 昆山PCB加工_SMT贴片_PCB抄板_线路板焊接加工-昆山腾宸电子科技有限公司 | 上海租奔驰_上海租商务车_上海租车网-矢昂汽车服务公司 | 仓储笼_仓储货架_南京货架_仓储货架厂家_南京货架价格低-南京一品仓储设备制造公司 | 济南轻型钢结构/济南铁艺护栏/济南铁艺大门-济南燕翔铁艺制品有限公司 | 车牌识别道闸_停车场收费系统_人脸识别考勤机_速通门闸机_充电桩厂家_中全清茂官网 | 工程管道/塑料管材/pvc排水管/ppr给水管/pe双壁波纹管等品牌管材批发厂家-河南洁尔康建材 | 浙江美尔凯特智能厨卫股份有限公司 |