-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiescence_minimax.py
More file actions
111 lines (95 loc) · 3.87 KB
/
quiescence_minimax.py
File metadata and controls
111 lines (95 loc) · 3.87 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import globals
from move_logic import gen_legal_moves, make_move
from globals import save_global_state, restore_global_state, switch_player_turn
from PST_evaluation import evaluate
from move_ordering import order_moves
leaf_node_count = 0
leaf_node_evaluations_retrieved_from_transposition_table = 0
def alpha_beta_quiescence_minimax(depth, maximizing_player, alpha, beta):
global leaf_node_count
if depth == 0:
return quiescence_search(alpha, beta, maximizing_player, depth, 5), None
best_move = None
if maximizing_player:
checks, captures, non_captures = gen_legal_moves()
ordered_captures = order_moves(captures)
max_eval = float('-inf')
for move in checks + ordered_captures + non_captures:
piece, start_index, end_index = move
saved_state = save_global_state()
make_move(piece, start_index, end_index)
switch_player_turn()
eval, _ = alpha_beta_quiescence_minimax(depth - 1, False, alpha, beta)
if eval > max_eval:
max_eval = eval
best_move = move
restore_global_state(saved_state)
alpha = max(alpha, eval)
if beta <= alpha:
break
return max_eval, best_move
else:
checks, captures, non_captures = gen_legal_moves()
print(f'checks: {checks}, captures: {captures}, non_captures: {non_captures}')
ordered_captures = order_moves(captures)
min_eval = float('inf')
for move in checks + ordered_captures + non_captures:
piece, start_index, end_index = move
saved_state = save_global_state()
make_move(piece, start_index, end_index)
switch_player_turn()
eval, _ = alpha_beta_quiescence_minimax(depth - 1, True, alpha, beta)
if eval < min_eval:
min_eval = eval
best_move = move
restore_global_state(saved_state)
beta = min(beta, eval)
if beta <= alpha:
break
return min_eval, best_move
def quiescence_search(alpha, beta, maximizing_player, depth, max_depth):
global leaf_node_count
global leaf_node_evaluations_retrieved_from_transposition_table
stand_pat = evaluate(globals.piece_bitboards)
leaf_node_count += 1
# If the current depth is greater than or equal to max depth, return the stand-pat evaluation
if depth >= max_depth:
return stand_pat
if maximizing_player:
if stand_pat >= beta:
return beta
if alpha < stand_pat:
alpha = stand_pat
checks, captures, _ = gen_legal_moves()
ordered_captures = order_moves(captures)
for move in checks + ordered_captures:
piece, start_index, end_index = move
saved_state = save_global_state()
make_move(piece, start_index, end_index)
switch_player_turn()
score = quiescence_search(alpha, beta, False, depth + 1, max_depth)
restore_global_state(saved_state)
if score >= beta:
return beta
if score > alpha:
alpha = score
return alpha
else:
if stand_pat <= alpha:
return alpha
if beta > stand_pat:
beta = stand_pat
checks, captures, _ = gen_legal_moves()
ordered_captures = order_moves(captures)
for move in checks + ordered_captures:
piece, start_index, end_index = move
saved_state = save_global_state()
make_move(piece, start_index, end_index)
switch_player_turn()
score = quiescence_search(alpha, beta, True, depth + 1, max_depth)
restore_global_state(saved_state)
if score <= alpha:
return alpha
if score < beta:
beta = score
return beta