python实现五子棋小程序

æ¬æå®ä¾ä¸ºå¤§å®¶å享äºpythonå®ç°äºå­æ£å°ç¨åºçå·ä½ä»£ç ï¼ä¾å¤§å®¶åèï¼å·ä½å容å¦ä¸

ä¸ãç»å书ä¸ä¾å­ï¼åä¸æ®µç¼åï¼

wuziqi.py

#coding:utf-8
from win_notwin import *
from show_qipan import *
 
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
while True:
  t=input("请è¾å¥æ£å­çä½ç½®ï¼xï¼yï¼,ç°å¨ç±"+("O" if who else "X")+"æ¹ä¸å­ï¼").split(",")
  #è¾å¥åæ 
  if len(t)==2:
    x=int(t[0])
    y=int(t[1])
    if qipan[x][y]==0:
      qipan[x][y]=1 if who else 2
      who=not who
 
      sqipan(qipan) #æ¾ç¤ºå½åæ£ç
 
      win_not(qipan,x,y) #å¤æ­æ没æ人赢
    else:
      print("å½åä½ç½®å·²ææ£å­ï¼è¯·éæ°ä¸å­")
  else :
    print("è¾å¥ä½ç½®æ误ï¼è¯·è¾å¥è¦ä¸çä½ç½®ï¼å¦1ï¼1")

show_qipan.py

def sqipan(qipan):
  maxx=10
  maxy=10
  print("  O ä¸ äº ä¸ å  äº å­ ä¸ å« ä¹")
  for i in range(maxx):
    print(i, " ", end="")
    for j in range(maxy):
      if qipan[i][j] == 0:
        print("+", " ", end="") # æ æ£å­
      elif qipan[i][j] == 1:
        print("O", " ", end="") # ç½è²
      elif qipan[i][j] == 2:
        print("X", " ", end="") # é»è²
    print("\n")

win_notwin.py

def win_not(qipan,x,y):
  maxx=10
  maxy=10
  flag=qipan[x][y]
  xPoint = x
  yPoint = y
  # 横å
  count = 0
  # x=xPoint
  # y=yPoint
  while (x >= 0 and flag == qipan[x][y]): # åå·¦ç»è®¡è¿ç»­æ£å­æ°
    count += 1
    x -= 1
  x = xPoint
  y = yPoint
  while (x >= 0 and flag == qipan[x][y]):
    count += 1
    x += 1
  if (count > 5): print("纵åäºå­æ£ç¸è¿ï¼èå©ï¼")
 
  count = 0
  x = xPoint
  y = yPoint
  while (y >= 0 and flag == qipan[x][y]):
    count += 1
    y -= 1
  y = yPoint
  while (y <= maxy and flag == qipan[x][y]):
    count += 1
    y += 1
  if (count > 5): print("横åäºå­ç¸è¿ï¼èå©")
 
  # æå
  # å·¦ä¸
  count = 0
  x = xPoint
  y = yPoint
  while (x >= 0 and y < maxy and flag == qipan[x][y]):
    count += 1
    x -= 1
    y += 1
  x = xPoint
  y = yPoint
  # å³ä¸
  while (x < maxx and y >= 0 and flag == qipan[x][y]):
    count += 1
    x += 1
    y -= 1
  if (count > 5): print("æåäºå­æ£ç¸è¿ï¼èå©")
 
  # æä¸
  count = 0
  x = xPoint
  y = yPoint
  # å·¦ä¸
  while (x >= 0 and y >= 0 and flag == qipan[x][y]):
    count += 1
    x -= 1
    y -= 1
  x = xPoint
  y = yPoint
  while (x < maxx and y < maxy and flag == qipan[x][y]):
    count += 1
    x += 1
    y += 1
  if (count > 5):
    print("æåäºå­ç¸è¿ï¼èå©")

äºãä¸é¢ç代ç æ²¡æéªè¯

#coding:utf-8
import os
import pdb
import pickle
 
class Player(object):
  number=0
  def __init__(self,name=''):
    """
     ç©å®¶ç±»æé æ¹æ³
     """
    if not name:
      Player.number+=1
      name='Player%d'% Player.number
    self.name=name
  def play(self):
    """
     ç©å®¶è¾å¥ä¸ä¸æ­¥è½å­ä½ç½®
     """
    t=input('Please input(x,y),now is'+self.name+':')
   
    return t
 
 
#æ£çç±»
class Board(object):
  class Status(object):
    """
     ç¶æéï¼æä¾è½¬æ常é
     """
    NONE=0
    WHITE=1
    BLACK=2
 
  def __init__(self,maxx=10,maxy=10):
    """
     æ£çç±»æé æ¹æ³ ç¡®å®å°ºå¯¸ï¼ä»¥åå建æ£çæå对象
     """
    self.maxx,self.maxy=maxx,maxy
    self.qipan=[[0]*maxy for i in range(maxx)]
 
  def hasChaessman(self,xPoint,yPoint):
    """
     å¤æ­æ¯å¦ææ£å­å­å¨
     """
    return self.qipan[xPoint][yPoint]!=Board.Status.NONE
  def downPawn(self,xPoint,yPoint,who):
    """
     ç©å®¶å¨æ个ä½ç½®è½å­
     """
    if self.hasChaessman(xPoint,yPoint):
      return False
    else:
      self.qipan[xPoint][yPoint]=Board.Status.WHITE if who else Board.Status.BLACK
      return True
 
  def inRange(self,xPoint,yPoint):
    """
    ç©å®¶å¨æ个ä½ç½®è½å­
    """
    return xPoint<self.maxx and\
        yPoint<self.maxy and\
        xPoint>=0 and\
        yPoint>=0
 
  def checkFiveInRow(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) and t==self.qipan[x][y]):
      count += 1
      x+=yDir
      y+=yDir
    x, y = xPoint, yPoint
    while (self.inRange(x, y) and t == self.qipan[x][y]):
      count += 1
      x -= yDir
      y -= yDir
    return count>5
 
  def isWin(self,xPoint,yPoint):
    """
    以ï¼xpoint,ypoinyï¼ç¹ä¸ºä¸­å¿å¨å个æ¹ååå«å¤æ­äºå­è¿ç 
    """
    pdb.set_trace()#####################
    return self.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)
 
  def printQp(self):
    """
    æå°æ£ç
    """
    qiType=["å","O","X"]
    print('  O ä¸ äº ä¸ å  äº å­ ä¸ å« ä¹')
    for i in range(self.maxx):
      print(i," ",end='')
      print(' '.join(qiType[x] for x in self.qipan[i]))
 
#æ件å­è¯»æ¡£ç±»
class FileStatus(object):
  def save(self):
    """
    å­æ¡£æ¹æ³
    """
    fpath=input("请è¾å¥ä¿ææ件çè·¯å¾ï¼")
    file=open(fpath,'w')
    pickle.dump(self,file)
    file.close()
 
  def load(self):
    """
    读档æ¹æ³
    """
    pass
 
 
#游æç±»
class GoBang(FileStatus):
 
  def __init__(self,qipan,white,black):
    """
    游æç±»æé æ¹æ³
    å建æååé
    """
    self.qipan=qipan
    self.white=white
    self.black=black
    self.who=True
 
  def start(self):
    """
    游æ主æµæ¹æ³
    """
    os.system('cls')
    self.printQp()
    while True:
      t=(self.white if self.who else self.black).play()
      if t=='S':
        self.save()
        continue
      if t=='L':
        self.load()
        continue
      t.split(',')
      if len(t)==2:
        x,y=int(t[0]).int(t[1])
        if self.qipan.downPawn(x,y,self.who):
          os.system('cls')
          self.printQp()
          if self.qipan.isWin(x,y):#å¤æ­æ¸¸ææ¯å¦ç»æ
            print(self.white.name if\
               self.who else self.black.name)+'Win'
            break
          self.who=not self.who #åæ¢æ¸¸æè§è²
    os.system('pause')
 
  def load(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()
 
  def printQp(self):
    """
    æå°
    """
    self.qipan.printQp()
    print("æL读åï¼Sä¿å­")
 
if __name__=='__main__':
  t=GoBang(Board(),Player(),Player())
  t.start()

以ä¸å°±æ¯æ¬æçå¨é¨å容ï¼å¸æ对大家ç学习ææ帮å©ï¼ä¹å¸æ大家å¤å¤æ¯æèæ¬ä¹å®¶ã

相关推荐