99WHITE = 2
1010
1111# Define directions to explore in the board
12- DIRECTIONS = [(0 , 1 ), (0 , - 1 ), (1 , 0 ), (- 1 , 0 ), (1 , 1 ), (1 , - 1 ), (- 1 , 1 ), (- 1 , - 1 )]
12+ DIRECTIONS = [(0 , 1 ), (0 , - 1 ), (1 , 0 ), (- 1 , 0 ),
13+ (1 , 1 ), (1 , - 1 ), (- 1 , 1 ), (- 1 , - 1 )]
14+
1315
1416def create_board ():
1517 return [[EMPTY for _ in range (BOARD_SIZE )] for _ in range (BOARD_SIZE )]
1618
19+
1720def print_board (board ):
1821 print (" " + " " .join (str (i ) for i in range (BOARD_SIZE )))
1922 for i in range (BOARD_SIZE ):
2023 print (f"{ i } |" + " " .join (str (board [i ][j ]) for j in range (BOARD_SIZE )))
2124
25+
2226def is_valid_move (board , player , row , col ):
2327 if board [row ][col ] != EMPTY :
2428 return False
@@ -31,6 +35,7 @@ def is_valid_move(board, player, row, col):
3135 return True
3236 return False
3337
38+
3439def make_move (board , player , row , col ):
3540 if not is_valid_move (board , player , row , col ):
3641 return False
@@ -48,6 +53,7 @@ def make_move(board, player, row, col):
4853 break
4954 return True
5055
56+
5157def get_valid_moves (board , player ):
5258 valid_moves = []
5359 for i in range (BOARD_SIZE ):
@@ -56,18 +62,21 @@ def get_valid_moves(board, player):
5662 valid_moves .append ((i , j ))
5763 return valid_moves
5864
65+
5966def count_discs (board ):
6067 black_count = sum (row .count (BLACK ) for row in board )
6168 white_count = sum (row .count (WHITE ) for row in board )
6269 return black_count , white_count
6370
71+
6472def evaluate_board (board , player ):
6573 black_count , white_count = count_discs (board )
6674 if player == BLACK :
6775 return black_count - white_count
6876 else :
6977 return white_count - black_count
7078
79+
7180def minimax (board , depth , player , alpha , beta , maximizing_player ):
7281 if depth == 0 :
7382 return evaluate_board (board , player )
@@ -96,6 +105,7 @@ def minimax(board, depth, player, alpha, beta, maximizing_player):
96105 break
97106 return min_eval
98107
108+
99109def find_best_move (board , player ):
100110 valid_moves = get_valid_moves (board , player )
101111 best_move = None
@@ -104,14 +114,16 @@ def find_best_move(board, player):
104114 for row , col in valid_moves :
105115 new_board = copy .deepcopy (board )
106116 make_move (new_board , player , row , col )
107- move_eval = minimax (new_board , depth = 3 , player = player , alpha = float ('-inf' ), beta = float ('inf' ), maximizing_player = False )
117+ move_eval = minimax (new_board , depth = 3 , player = player , alpha = float (
118+ '-inf' ), beta = float ('inf' ), maximizing_player = False )
108119
109120 if move_eval > best_eval :
110121 best_eval = move_eval
111122 best_move = (row , col )
112123
113124 return best_move
114125
126+
115127def main ():
116128 board = create_board ()
117129 board [3 ][3 ], board [4 ][4 ] = WHITE , WHITE
@@ -150,5 +162,6 @@ def main():
150162
151163 current_player = WHITE if current_player == BLACK else BLACK
152164
165+
153166if __name__ == "__main__" :
154167 main ()
0 commit comments