python console simple implementation of backgammon

 

# Chessboard
# Lazi
# rules
Import Random
class Chess:
DEF __init __ (Self):
Print ( '# --------------- ------------- board # --- ')
self.grid = []
self.si = []
for I in Range (. 8):
self.grid.append ([' □ '] *. 8)
# Print (self.grid)
# Print ( )
for J in Range (. 8):
Print ( '' the Join (self.grid [J].)) # '' the Join (Grid [J])).

DEF getPos (Self, X, Y):
Print ( '# zi # ---------------- ---------------- ')
self.x = X
self.y Y =
IF self.grid [self.x] [self.y] == ' ○' or self.grid [self.x] [self.y] == '●': # determines whether to repeat the pieces
print ( '\ t \ t \ t pieces duplicate ')
the else:
self.l = 0
self.grid [self.x] [self.y] ='● '
if self.x==7 or self.y==0 :
self.grid[random.randint(self.x-2,self.x-1)][random.randint(self.y-2,self.y-1)] = ' ○ '

elif self.grid[random.randint(self.x - 1, self.x+1)][random.randint(self.y - 1, self.y+1)] == None:
self.grid[random.randint(self.x - 1, self.x + 1)][random.randint(self.y - 1, self.y + 1)] = ' ○ '

else:
self.grid[self.x][self.y] = ' ● '
self.grid[self.x + 1][ self.y + 1] =' ○ '
self.si.append([self.x, self.y])
for i in range(len(self.si)):
if self.si[i-1][0] == self.si[i][0] or self.si[i-1][1] == self.si[i][1] or self.si[i][1]==self.si[i][0] and self.si[i-1][1]==self.si[i-1][0]: # 判断玩家是否排成一条线
self.l += 1
# print(self.l)
for j in range(8):
Print ( '\ n-' the Join (self.grid [J]) +. '') # '' the Join (Grid [J])).

DEF the Rulers (Self):
Recode = []
recode.append ([self.x , self.y])
Print (self.si)

for I in Range (len (Recode)):
for J in Range (len (Recode [I])):
iF self.l ==. 5: # flag, as when the end of the 5:00 game
print ( 'game Over')
print ( 'victory players')
self.t = False
return self.t
elif len (self.si)> 6: # judged a draw, because of the simple conditions, so no computer win situation
print ( 'game Over')
print ( 'draw')
self.t = False
return self.t
the else:
self.t = True
return self.t




Play Chess = ()
t = True
the while t: # cycle to achieve board appears
x, y = input ( "\ t enter the x, y coordinates ',' split: \ n"). split ( ',')
play.getPos(int(x),int(y))
t = play.rulers()

This is just a simple exercise

White one, AI training methods have not yet learned, will continue to back a more

keep it up

Guess you like

Origin www.cnblogs.com/superSmall/p/11503354.html