SoFunction
Updated on 2024-11-16

python implementation of backgammon applet

In this article, we share the example of python to achieve the specific code of the five pieces of chess applet for your reference, the details are as follows

I. Prepared in three paragraphs with examples from the book:

#coding:utf-8
from win_notwin import *
from show_qipan import *
 
maxx=10 #10 rows and 10 columns
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) # Show initialized board
 
 
who=True
while True:
 t=input("Please enter the position of the piece (x, y),now by "+("O" if who else "X")+"Square down:").split(",")
 # Input coordinates
 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) # Show current board
 
  win_not(qipan,x,y) # To determine if anyone is winning
 else:
  print("There are already pieces in the current position. Please make a new move.")
 else :
 print("There is an error in entering the position, please enter the position you want to go down, e.g. 1, 1")

show_qipan.py

def sqipan(qipan):
 maxx=10
 maxy=10
 print("O, 1, 2, 3, 4, 5, 6, 7, 8, 9.")
 for i in range(maxx):
 print(i, " ", end="")
 for j in range(maxy):
  if qipan[i][j] == 0:
  print("+", " ", end="") # No pawns
  elif qipan[i][j] == 1:
  print("O", " ", end="") # White
  elif qipan[i][j] == 2:
  print("X", " ", end="") # Black
 print("\n")

win_notwin.py

def win_not(qipan,x,y):
 maxx=10
 maxy=10
 flag=qipan[x][y]
 xPoint = x
 yPoint = y
 # Horizontal
 count = 0
 # x=xPoint
 # y=yPoint
 while (x >= 0 and flag == qipan[x][y]): # Count the number of consecutive pieces to the left
 count += 1
 x -= 1
 x = xPoint
 y = yPoint
 while (x >= 0 and flag == qipan[x][y]):
 count += 1
 x += 1
 if (count > 5): print("Vertical pentominoes connected, victory!")
 
 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("Horizontal five-on-five. Victory.")
 
 # Inclined
 # Bottom left
 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
 # Upper right
 while (x < maxx and y >= 0 and flag == qipan[x][y]):
 count += 1
 x += 1
 y -= 1
 if (count > 5): print("Diagonal pentominoes connected. Victory.")
 
 # Slanting up
 count = 0
 x = xPoint
 y = yPoint
 # Upper left
 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("Oblique five-piece connection. Victory.")

II. The following code does not validate

#coding:utf-8
import os
import pdb
import pickle
 
class Player(object):
 number=0
 def __init__(self,name=''):
 """
  Player class constructor
  """
 if not name:
  +=1
  name='Player%d'% 
 =name
 def play(self):
 """
  Players enter their next drop position
  """
 t=input('Please input(x,y),now is'++':')
 
 return t
 
 
#Chessboard
class Board(object):
 class Status(object):
 """
  State quantities that provide transitive constants
  """
 NONE=0
 WHITE=1
 BLACK=2
 
 def __init__(self,maxx=10,maxy=10):
 """
  Chessboard Class Constructor Methods Determine dimensions, and create Chessboard Member Objects
  """
 ,=maxx,maxy
 =[[0]*maxy for i in range(maxx)]
 
 def hasChaessman(self,xPoint,yPoint):
 """
  Determining if a piece exists
  """
 return [xPoint][yPoint]!=
 def downPawn(self,xPoint,yPoint,who):
 """
  A player lands in a certain position
  """
 if (xPoint,yPoint):
  return False
 else:
  [xPoint][yPoint]= if who else 
  return True
 
 def inRange(self,xPoint,yPoint):
 """
 A player lands in a certain position
 """
 return xPoint< and\
  yPoint< and\
  xPoint>=0 and\
  yPoint>=0
 
 def checkFiveInRow(self,xPoint,yPoint,xDir,yDir):
 """
 determine whether or not to(xpoint,ypoiny)point (in space or time)(xDir,yDir)Whether or not the direction is five in a row
 """
 count = 0
 t=[xPoint][yPoint]
 x,y=xPoint,yPoint
 while ((x,y) and t==[x][y]):
  count += 1
  x+=yDir
  y+=yDir
 x, y = xPoint, yPoint
 while ((x, y) and t == [x][y]):
  count += 1
  x -= yDir
  y -= yDir
 return count>5
 
 def isWin(self,xPoint,yPoint):
 """
 Judge the pentominoes in each of the four directions centered on the (xpoint,ypoiny) point
 """
 pdb.set_trace()#####################
 return (xPoint,yPoint,1,0) or \
  (xPoint, yPoint, 0,1) or \
  (xPoint, yPoint, 1,1) or \
  (xPoint, yPoint, 1,-1)
 
 def printQp(self):
 """
 Printing Chess Boards
 """
 qiType=["Ten.","O","X"]
 print(' O one, two, three, four, five, six, seven, eight, nine.')
 for i in range():
  print(i," ",end='')
  print(' '.join(qiType[x] for x in [i]))
 
# Documents in the archive class
class FileStatus(object):
 def save(self):
 """
 Archiving Methods
 """
 fpath=input("Please enter the path to the holding file:")
 file=open(fpath,'w')
 (self,file)
 ()
 
 def load(self):
 """
 How to read the file
 """
 pass
 
 
#Games
class GoBang(FileStatus):
 
 def __init__(self,qipan,white,black):
 """
 Game Class Constructor Methods
 Creating member variables
 """
 =qipan
 =white
 =black
 =True
 
 def start(self):
 """
 The Mainstream Approach to Gaming
 """
 ('cls')
 ()
 while True:
  t=( if  else ).play()
  if t=='S':
  ()
  continue
  if t=='L':
  ()
  continue
  (',')
  if len(t)==2:
  x,y=int(t[0]).int(t[1])
  if (x,y,):
   ('cls')
   ()
   if (x,y):# Determine if the game is over
   print( if\
     else )+'Win'
   break
   =not  #Switching game characters
 ('pause')
 
 def load(self):
 """
 Rewrite read file method
 """
 fpath=input("Please enter the path to read the file.")
 file=open(fpath,'r')
 status=(file)
 ()
 #Read the file. Copy.
 =
 =
 =
 =
 ('cls')
 ()
 
 def printQp(self):
 """
 Print
 """
 ()
 print("Press L to read, S to save.")
 
if __name__=='__main__':
 t=GoBang(Board(),Player(),Player())
 ()

This is the whole content of this article.