33import random
44import AI
55
6- generation = 861 ## Gerneation to play against
7- n = 8 # board size 8,6,4,2(needs to be even)
8- white = "☺" # white tile ascii
9- black = "☻" # black tile ascii
10- bg = "•" # bg tile ascii
6+ generation = 2500 # Generation to play against
7+ n = 8 # board size 8,6,4,2(needs to be even)
8+ white = "☺" # white tile ascii
9+ black = "☻" # black tile ascii
10+ bg = "•" # bg tile ascii
1111
12- w = - 1 # white tile ID
13- b = 1 # black tile ID
12+ w = - 1 # white tile ID
13+ b = 1 # black tile ID
1414
15- board = [[0 for _ in range (n )] for _ in range (n )] # the board
15+ board = [[0 for _ in range (n )] for _ in range (n )] # the board
1616
1717
18- def playerASCII (): # defining tile ID
18+ def playerASCII (): # defining tile ID
1919 return white if turn == w else black
2020
21- def countChess (turn ): # count how many pieces are on the board
21+
22+ def countChess (turn ): # count how many pieces are on the board
2223 return sum (board [i ].count (turn ) for i in range (n ))
2324
24- def printBoard (): # print the board
25+
26+ def printBoard (): # print the board
2527 print ()
2628 print (" " , end = "" )
2729 for i in range (n ):
2830 print (i , end = " " )
2931 print ()
30- for i in range (n ): # making a 2-D array
32+ for i in range (n ): # making a 2-D array
3133 print (i , end = " " )
3234 for j in range (n ):
3335 print (white if board [j ][i ] == w else \
@@ -37,51 +39,56 @@ def printBoard(): # print the board
3739 print (white + ":" + str (countChess (w )), end = "" )
3840 print ()
3941
40- def initBoard (): # prepare board
42+
43+ def initBoard (): # prepare board
4144 board [n // 2 - 1 ][n // 2 - 1 ] = w
4245 board [n // 2 ][n // 2 ] = w
4346 board [n // 2 ][n // 2 - 1 ] = b
4447 board [n // 2 - 1 ][n // 2 ] = b
4548
46- ## custom board for testing
47- #global board
48- #board = [[b,b,b,b,b,b,b,b],
49- # [b,b,b,b,b,b,b,b],
50- # [b,b,b,w,b,b,b,b],
51- # [b,w,b,b,b,w,b,b],
52- # [b,b,b,b,w,b,b,b],
53- # [b,b,b,0,w,b,b,b],
54- # [b,b,0,0,w,w,0,0],
55- # [b,0,0,0,w,w,w,0]]
56-
57- def outOfBound (x , y ): # if is out of board/bounds
49+ # custom board for testing
50+ #global board
51+ #board = [[b,b,b,b,b,b,b,b],
52+ # [b,b,b,b,b,b,b,b],
53+ # [b,b,b,w,b,b,b,b],
54+ # [b,w,b,b,b,w,b,b],
55+ # [b,b,b,b,w,b,b,b],
56+ # [b,b,b,0,w,b,b,b],
57+ # [b,b,0,0,w,w,0,0],
58+ # [b,0,0,0,w,w,w,0]]
59+
60+
61+ def outOfBound (x , y ): # if is out of board/bounds
5862 return x < 0 or x >= n or y < 0 or y >= n
5963
64+
6065def neighbourIsOpposite (x , y , dx , dy ):
6166 if outOfBound (x + dx , y + dy ):
6267 return False
6368 return board [x + dx ][y + dy ] == opposite (turn )
6469
70+
6571def checkSandwich (x , y , dx , dy , turn ):
6672 if dx == 0 and dy == 0 :
6773 return False
68- if outOfBound (x + dx , y + dy ): # out of bound
74+ if outOfBound (x + dx , y + dy ): # out of bound
6975 return False
70- if board [x + dx ][y + dy ] == turn : # sandwiched
76+ if board [x + dx ][y + dy ] == turn : # sandwiched
7177 return True
7278 elif board [x + dx ][y + dy ] == opposite (turn ):
7379 return checkSandwich (x + dx , y + dy , dx , dy , turn )
7480 else : # space is unoccupied
7581 return False
7682
77- def opposite (turn ): # change turn
83+
84+ def opposite (turn ): # change turn
7885 return w if turn == b else b
7986
8087
8188def isLegal (x , y , turn ):
82- if outOfBound (x , y ): # out of bound
89+ if outOfBound (x , y ): # out of bound
8390 return False
84- if board [x ][y ] != 0 : # space is occupied
91+ if board [x ][y ] != 0 : # space is occupied
8592 return False
8693 for i in range (- 1 , 2 ):
8794 for j in range (- 1 , 2 ):
@@ -90,20 +97,23 @@ def isLegal(x, y, turn):
9097 return True
9198 return False
9299
100+
93101def replaceNeighbours (x , y , dx , dy , turn ):
94102 if checkSandwich (x , y , dx , dy , turn ):
95103 board [x + dx ][y + dy ] = turn
96104 replaceNeighbours (x + dx , y + dy , dx , dy , turn )
97105
106+
98107def generateValidMovesList ():
99108 return [(i ,j ) for i in range (n ) for j in range (n ) \
100109 if isLegal (i , j , turn )]
101110
111+
102112initBoard ()
103113
104- turn = b # turn indicator b or w(Black Or White), black goes first
105- whiteCanPlay = True # if white is not stuck
106- blackCanPlay = True # if black is not stuck
114+ turn = b # turn indicator b or w(Black Or White), black goes first
115+ whiteCanPlay = True # if white is not stuck
116+ blackCanPlay = True # if black is not stuck
107117
108118# legal check
109119playerTile = input ("Choose your chess color. Type 'b' for black and white otherwise. Black goes first: " )
@@ -147,7 +157,7 @@ def generateValidMovesList():
147157 print ("Invalid Format. " , end = "" )
148158 else :
149159 # computer's turn
150- #x, y = random.choice(validMoves)
160+ # x, y = random.choice(validMoves)
151161 x , y = AI .chooseMove (board , AI .structure , bestNetwork , turn )
152162 print (playerASCII () + " Computer's move:" , x , y )
153163
0 commit comments