-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphabeta_player.py
78 lines (65 loc) · 2.54 KB
/
alphabeta_player.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
70
71
72
73
74
75
76
77
78
from player_interface import PlayerInterface
from constants import SKIP
"""A minimax player who maximizes their move assuming others are adversarial
players, with alpha beta pruning. Depth of 2 by default.
"""
class AlphaBetaPlayer(PlayerInterface):
def __init__(self, depth=2):
super().__init__()
self.DEPTH = depth
def get_move(self, board):
"""
Returns the maximal move for this AI player.
Board -> Posn
"""
tree = self.game_tree
return self.max_value(tree, self.DEPTH, -float("inf"), float("inf"))[0]
def max_value(self, tree, depth, alpha, beta):
"""
Maximizes the value for this AI player with alpha beta pruning,
and assuming the opponent will minimize the score. Does alpha beta pruning.
GameTree Nat int int -> (Tupleof Posn Nat)
"""
# the game has ended
if tree.is_game_over():
return (None, tree.get_score(self.color))
moves = tree.get_actions()
# Skip if there are no possible moves
if (len(moves) == 0):
return (SKIP, tree.get_score(self.color))
best_val = -float("inf")
for action in moves:
val_action = self.min_value(tree.children[action], depth, alpha, beta)[1]
if val_action > best_val:
best_action = action
best_val = val_action
alpha = max(alpha, best_val)
if beta < alpha:
break
return (best_action, best_val)
def min_value(self, tree, depth, alpha, beta):
"""
When it is the opponent player's turn, they will minimize this player's score.
Does alpha beta pruning.
GameTree Nat int int -> (Tupleof Posn Nat)
"""
# the game has ended
if tree.is_game_over():
return (None, tree.get_score(self.color))
moves = tree.get_actions()
# Skip if there are no possible moves
if (len(moves) == 0):
return (SKIP, tree.get_score(self.color))
best_val = float("inf")
for action in moves:
if depth == 1:
val_action = tree.children[action].get_score(self.color)
else:
val_action = self.max_value(tree.children[action], depth - 1, alpha, beta)[1]
if val_action < best_val:
best_val = val_action
best_action = action
beta = min(beta, best_val)
if beta < alpha:
break
return (best_action, best_val)