9
9
WHITE = 2
10
10
11
11
# 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
+
13
15
14
16
def create_board ():
15
17
return [[EMPTY for _ in range (BOARD_SIZE )] for _ in range (BOARD_SIZE )]
16
18
19
+
17
20
def print_board (board ):
18
21
print (" " + " " .join (str (i ) for i in range (BOARD_SIZE )))
19
22
for i in range (BOARD_SIZE ):
20
23
print (f"{ i } |" + " " .join (str (board [i ][j ]) for j in range (BOARD_SIZE )))
21
24
25
+
22
26
def is_valid_move (board , player , row , col ):
23
27
if board [row ][col ] != EMPTY :
24
28
return False
@@ -31,6 +35,7 @@ def is_valid_move(board, player, row, col):
31
35
return True
32
36
return False
33
37
38
+
34
39
def make_move (board , player , row , col ):
35
40
if not is_valid_move (board , player , row , col ):
36
41
return False
@@ -48,6 +53,7 @@ def make_move(board, player, row, col):
48
53
break
49
54
return True
50
55
56
+
51
57
def get_valid_moves (board , player ):
52
58
valid_moves = []
53
59
for i in range (BOARD_SIZE ):
@@ -56,18 +62,21 @@ def get_valid_moves(board, player):
56
62
valid_moves .append ((i , j ))
57
63
return valid_moves
58
64
65
+
59
66
def count_discs (board ):
60
67
black_count = sum (row .count (BLACK ) for row in board )
61
68
white_count = sum (row .count (WHITE ) for row in board )
62
69
return black_count , white_count
63
70
71
+
64
72
def evaluate_board (board , player ):
65
73
black_count , white_count = count_discs (board )
66
74
if player == BLACK :
67
75
return black_count - white_count
68
76
else :
69
77
return white_count - black_count
70
78
79
+
71
80
def minimax (board , depth , player , alpha , beta , maximizing_player ):
72
81
if depth == 0 :
73
82
return evaluate_board (board , player )
@@ -96,6 +105,7 @@ def minimax(board, depth, player, alpha, beta, maximizing_player):
96
105
break
97
106
return min_eval
98
107
108
+
99
109
def find_best_move (board , player ):
100
110
valid_moves = get_valid_moves (board , player )
101
111
best_move = None
@@ -104,14 +114,16 @@ def find_best_move(board, player):
104
114
for row , col in valid_moves :
105
115
new_board = copy .deepcopy (board )
106
116
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 )
108
119
109
120
if move_eval > best_eval :
110
121
best_eval = move_eval
111
122
best_move = (row , col )
112
123
113
124
return best_move
114
125
126
+
115
127
def main ():
116
128
board = create_board ()
117
129
board [3 ][3 ], board [4 ][4 ] = WHITE , WHITE
@@ -150,5 +162,6 @@ def main():
150
162
151
163
current_player = WHITE if current_player == BLACK else BLACK
152
164
165
+
153
166
if __name__ == "__main__" :
154
167
main ()
0 commit comments