python实现五子棋小程序
本文实例为大家分享了python实现五子棋小程序的具体代码,供大家参考,具体内容如下
一、结合书上例子,分三段编写:
wuziqi.py
#coding:utf-8 fromwin_notwinimport* fromshow_qipanimport* maxx=10#10行10列 maxy=10 qipan=[[0,0,0,0,1,0,0,2,0,0],[0,1,2,1,1,0,2,0,0,0],[0,0,0,0,1,1,0,2,0,0],[0,0,0,0,2,0,0,1,0,0], [0,0,0,1,1,1,2,0,0,0],[0,0,0,2,0,0,0,2,0,0],[0,0,1,2,0,2,2,0,1,0],[0,0,0,2,0,0,0,1,0,0],[0,0,0,0,0,0,1,1,0,0], [0,0,0,0,0,0,0,0,0,0]] sqipan(qipan)#显示初始化的棋盘 who=True whileTrue: t=input("请输入棋子的位置(x,y),现在由"+("O"ifwhoelse"X")+"方下子:").split(",") #输入坐标 iflen(t)==2: x=int(t[0]) y=int(t[1]) ifqipan[x][y]==0: qipan[x][y]=1ifwhoelse2 who=notwho sqipan(qipan)#显示当前棋盘 win_not(qipan,x,y)#判断有没有人赢 else: print("当前位置已有棋子,请重新下子") else: print("输入位置有误,请输入要下的位置,如1,1")
show_qipan.py
defsqipan(qipan): maxx=10 maxy=10 print("O一二三四五六七八九") foriinrange(maxx): print(i,"",end="") forjinrange(maxy): ifqipan[i][j]==0: print("+","",end="")#无棋子 elifqipan[i][j]==1: print("O","",end="")#白色 elifqipan[i][j]==2: print("X","",end="")#黑色 print("\n")
win_notwin.py
defwin_not(qipan,x,y): maxx=10 maxy=10 flag=qipan[x][y] xPoint=x yPoint=y #横向 count=0 #x=xPoint #y=yPoint while(x>=0andflag==qipan[x][y]):#向左统计连续棋子数 count+=1 x-=1 x=xPoint y=yPoint while(x>=0andflag==qipan[x][y]): count+=1 x+=1 if(count>5):print("纵向五子棋相连,胜利!") count=0 x=xPoint y=yPoint while(y>=0andflag==qipan[x][y]): count+=1 y-=1 y=yPoint while(y<=maxyandflag==qipan[x][y]): count+=1 y+=1 if(count>5):print("横向五子相连,胜利") #斜向 #左下 count=0 x=xPoint y=yPoint while(x>=0andy=0andflag==qipan[x][y]): count+=1 x+=1 y-=1 if(count>5):print("斜向五子棋相连,胜利") #斜上 count=0 x=xPoint y=yPoint #左上 while(x>=0andy>=0andflag==qipan[x][y]): count+=1 x-=1 y-=1 x=xPoint y=yPoint while(x 5): print("斜向五子相连,胜利")
二、下面的代码没有验证
#coding:utf-8 importos importpdb importpickle classPlayer(object): number=0 def__init__(self,name=''): """ 玩家类构造方法 """ ifnotname: Player.number+=1 name='Player%d'%Player.number self.name=name defplay(self): """ 玩家输入下一步落子位置 """ t=input('Pleaseinput(x,y),nowis'+self.name+':') returnt #棋盘类 classBoard(object): classStatus(object): """ 状态量,提供转态常量 """ NONE=0 WHITE=1 BLACK=2 def__init__(self,maxx=10,maxy=10): """ 棋盘类构造方法确定尺寸,以及创建棋盘成员对象 """ self.maxx,self.maxy=maxx,maxy self.qipan=[[0]*maxyforiinrange(maxx)] defhasChaessman(self,xPoint,yPoint): """ 判断是否有棋子存在 """ returnself.qipan[xPoint][yPoint]!=Board.Status.NONE defdownPawn(self,xPoint,yPoint,who): """ 玩家在某个位置落子 """ ifself.hasChaessman(xPoint,yPoint): returnFalse else: self.qipan[xPoint][yPoint]=Board.Status.WHITEifwhoelseBoard.Status.BLACK returnTrue definRange(self,xPoint,yPoint): """ 玩家在某个位置落子 """ returnxPoint=0and\ yPoint>=0 defcheckFiveInRow(self,xPoint,yPoint,xDir,yDir): """ 判断以(xpoint,ypoiny)点(xDir,yDir)方向上是否五子连珠 """ count=0 t=self.qipan[xPoint][yPoint] x,y=xPoint,yPoint while(self.inRange(x,y)andt==self.qipan[x][y]): count+=1 x+=yDir y+=yDir x,y=xPoint,yPoint while(self.inRange(x,y)andt==self.qipan[x][y]): count+=1 x-=yDir y-=yDir returncount>5 defisWin(self,xPoint,yPoint): """ 以(xpoint,ypoiny)点为中心在四个方向分别判断五子连珠 """ pdb.set_trace()##################### returnself.checkFiveInRow(xPoint,yPoint,1,0)or\ self.checkFiveInRow(xPoint,yPoint,0,1)or\ self.checkFiveInRow(xPoint,yPoint,1,1)or\ self.checkFiveInRow(xPoint,yPoint,1,-1) defprintQp(self): """ 打印棋盘 """ qiType=["十","O","X"] print('O一二三四五六七八九') foriinrange(self.maxx): print(i,"",end='') print(''.join(qiType[x]forxinself.qipan[i])) #文件存读档类 classFileStatus(object): defsave(self): """ 存档方法 """ fpath=input("请输入保持文件的路径:") file=open(fpath,'w') pickle.dump(self,file) file.close() defload(self): """ 读档方法 """ pass #游戏类 classGoBang(FileStatus): def__init__(self,qipan,white,black): """ 游戏类构造方法 创建成员变量 """ self.qipan=qipan self.white=white self.black=black self.who=True defstart(self): """ 游戏主流方法 """ os.system('cls') self.printQp() whileTrue: t=(self.whiteifself.whoelseself.black).play() ift=='S': self.save() continue ift=='L': self.load() continue t.split(',') iflen(t)==2: x,y=int(t[0]).int(t[1]) ifself.qipan.downPawn(x,y,self.who): os.system('cls') self.printQp() ifself.qipan.isWin(x,y):#判断游戏是否结束 print(self.white.nameif\ self.whoelseself.black.name)+'Win' break self.who=notself.who#切换游戏角色 os.system('pause') defload(self): """ 重写读档方法 """ fpath=input("请输入读取文件的路径") file=open(fpath,'r') status=pickle.load(file) file.close() #读档拷贝 self.qipan=status.qipan self.white=status.white self.black=status.black self.who=status.who os.system('cls') self.printQp() defprintQp(self): """ 打印 """ self.qipan.printQp() print("按L读取,S保存") if__name__=='__main__': t=GoBang(Board(),Player(),Player()) t.start()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。