-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathai.py
90 lines (65 loc) · 2.23 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from math import inf
INFINITY = inf
def best_move(config, player, max_depth, prune):
"Finds the best move starting from a given configuration."
next_configs = config.next_configurations(player)
minimax_fn = alpha_beta if prune else minimax
opposite_player = player.opposite()
scores = {}
for next_config in next_configs:
# for every possible move, compute a score
if next_config.capture_chain:
next_player = player
else:
next_player = opposite_player
score = minimax_fn(next_config, next_player, max_depth)
scores[next_config] = score
if player.is_maximizing():
optimum_func = max
else:
optimum_func = min
return optimum_func(next_configs, key=lambda cfg: scores[cfg])
def minimax(config, player, depth):
if depth == 0:
return config.score()
next_configs = config.next_configurations(player)
opposite_player = player.opposite()
if player.is_maximizing():
score = -INFINITY
else:
score = +INFINITY
for next_config in next_configs:
if next_config.capture_chain:
next_player = player
else:
next_player = opposite_player
next_score = minimax(next_config, next_player, depth - 1)
if player.is_maximizing():
score = max(score, next_score)
else:
score = min(score, next_score)
return score
def alpha_beta(config, player, depth, alpha=-INFINITY, beta=+INFINITY):
if depth == 0:
return config.score()
next_configs = config.next_configurations(player)
opposite_player = player.opposite()
if player.is_maximizing():
score = -INFINITY
else:
score = +INFINITY
for next_config in next_configs:
if next_config.capture_chain:
next_player = player
else:
next_player = opposite_player
next_score = alpha_beta(next_config, next_player, depth - 1, alpha, beta)
if player.is_maximizing():
score = max(score, next_score)
alpha = max(alpha, score)
else:
score = min(score, next_score)
beta = min(beta, score)
if alpha >= beta:
break
return score