-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkers_ai.py
69 lines (62 loc) · 2.21 KB
/
checkers_ai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from game import init_borders, Board, Piece
from game import make_move
def find_ai_move(curr_state):
"""Returns the best move found from a call to the minimax algorithm"""
borders = init_borders()
best_move = minimax(curr_state, 6, True, borders)[1]
return best_move
def minimax(state, depth, max_player, borders, alpha=float("-inf"), beta=float("inf")):
"""
Returns the best move found from a given state, depth of the search tree,
and player
"""
color = Piece.WHITE if max_player else Piece.BLACK
state_obj = Board(state, borders)
moves = state_obj.find_valid_moves(color)
game_won = not moves
if depth == 0 or game_won:
return evaluate(state), ()
best_move = None
if max_player:
max_eval = float("-inf")
for move in moves:
next_state = make_move(move, state)
curr_eval = minimax(next_state, depth - 1, False, borders)[0]
max_eval = max(max_eval, curr_eval)
alpha = max(alpha, curr_eval)
if beta <= alpha:
break
if max_eval == curr_eval:
best_move = move
return max_eval, best_move
else:
min_eval = float("inf")
for move in moves:
next_state = make_move(move, state)
curr_eval = minimax(next_state, depth - 1, True, borders)[0]
min_eval = min(min_eval, curr_eval)
beta = min(beta, curr_eval)
if beta <= alpha:
break
if min_eval == curr_eval:
best_move = move
return min_eval, best_move
def evaluate(state):
"""
heuristic function that evaluates the game state based on the
number of captured pieces and the number of king pieces.
"""
count_white = 0
count_black = 0
count_white_kings = 0
count_black_kings = 0
for _, piece in state:
if piece == Piece.WHITE:
count_white += 1
if piece == Piece.BLACK:
count_black += 1
if piece == Piece.BLACK_KING:
count_black_kings += 1
if piece == Piece.WHITE_KING:
count_white_kings += 1
return (count_white - count_black) + (.5 * count_white_kings - .5 * count_black_kings)