python Makes Simple Gobang Game

  • 2021-06-28 13:29:01
  • OfStack

This example shares the specific code of python5 chess game for your reference, as follows

#5 Chess
'''
Matrix makes board 16*16 "+"

Print chessboard for for

Is the game over

Start Chess while Game End:
Black and white alternating player=0 p%2==0==1 p+=1
Chess moves 1 but pieces 1
'''

Code


# A program to create a chessboard 
def initBoard():
 global board # Call Global board
 board=[None]*16
 for i in range(len(board)):
 board[i]=["+ "]*16
# Program for Printing Chess Board 
def printBoard():
 global board
 for i in range(len(board)):
 for j in range(len(board[i])):
  print(board[i][j],end=" ")
 print("")
# Procedure to start playing chess 
def startGame():
 global board
 player=0
 while isGameContinue():
 if player%2==0:
  # Black Chess 
  print("==> Black Chess ")
  if not playChess(" <. "):
  continue
 else:
  # White Chess 
  print("==> White Chess ")
  if not playChess(" Zero "):
  continue
 player+=1

def playChess(chess):
 # Get Location 
 x=int(input("==> X="))-1
 y=int(input("==> Y="))-1
 if board[x][y]=="+ ":
 board[x][y]=chess
 printBoard()
 return True # Lao Zi Success 
 else:
 print("==>  Existing chess pieces   Please fall back \a")
 printBoard()
 return False# Fall Child Failure 
def isGameContinue():
 for i in range(len(board)):
 for j in range(len(board[i])):
  if board[i][j]!="+ ":
  # transverse 
  if j<=11:
   if board[i][j]==board[i][j+1]==board[i][j+2]==board[i][j+3]==board[i][j+4]:
   whoWin(i,j)
   return False
  # vertical 
  if i<=11:
   if board[i][j]==board[i+1][j]==board[i+2][j]==board[i+3][j]==board[i+4][j]:
   whoWin(i,j)
   return False
  # Backslant 
  if i<=11 and j<=11:
   if board[i][j]==board[i+1][j+1]==board[i+2][j+2]==board[i+3][j+3]==board[i+4][j+4]:
   whoWin(i,j)
   return False
  # Forward slant 
  if i>=4 and j<=11:
   if board[i][j]==board[i-1][j+1]==board[i-2][j+2]==board[i-3][j+3]==board[i-4][j+4]:
   whoWin(i,j)
   return False
 return True
def whoWin(i,j):
 if board[i][j]==" <. ":
 print(" Black wins! ")
 else:
 print(" White Square Wins! ")
 for i in range(10):
 print("\a")
board=[] 
initBoard()
printBoard()
startGame()

Related articles: