-
Notifications
You must be signed in to change notification settings - Fork 0
/
contests.py
215 lines (178 loc) · 9.12 KB
/
contests.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import time
from core.utils import Board
from core.zuggenerator import game_ending, check_king_hill, check_for_remi, check_for_checkmate, gen_all_masks
from core.dtm import dynamic_time_management
from core.aitech.alpha_beta.alpha_beta import iterative_deepening_alpha_beta_search
from core.aitech.alpha_beta.alpha_beta_tt import iterative_deepening_alpha_beta_TT
from core.aitech.alpha_beta.alpha_beta_sorted import iterative_deepening_alpha_beta_sorted
from core.aitech.pvs.pvs import pvs
from core.aitech.mcts import mcts
from core.aitech.pvs.pvs_sorted import pvs_sort
from core.aitech.pvs.pvs_sorted_multi import pvs_multi_sort
from core.aitech.pvs.pvs_sorted_multi_qs import pvs_multi_sort_qs as pvs_multi_sort_qs
from core.aitech.pvs.pvs_sorted_qs import pvs_sort_qs as pvs_sort_qs
START_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 0"
def play_clock(player_1_function, player_2_function, chess_clock_time=300):
with open("contest_results.dat", "a") as file:
file.write("---- NEW GAME ----\n")
file.write(f"time per player: {chess_clock_time}\n")
file.write(f"player 1: {player_1_function.__name__}\n")
file.write(f"player 2: {player_2_function.__name__}\n")
board = Board(START_FEN)
p1_time = chess_clock_time
p2_time = chess_clock_time
for i in range(200):
tl = dynamic_time_management(board=board, time=p1_time)
t1 = time.time()
move = player_1_function(board=board, time_limit=tl)
print(move)
t2 = time.time()
p1_time -= (t2 - t1)
board.do_move(move)
if game_ending(board):
with open("contest_results.dat", "a") as file:
file.write("Game Ended. Winner: Player 1\n")
if check_for_remi(board):
file.write("Reason: Remi\n")
elif check_for_checkmate(board):
file.write("Reason: Checkmate\n")
elif check_king_hill(board):
file.write("Reason: King Hill\n")
file.write(f"Time Left. Player 1: {p1_time}, Player 2: {p2_time}\n")
return
if p1_time <= 0:
with open("contest_results.dat", "a") as file:
file.write(" Game Ended. Winner: Player 2\n")
file.write("Reason: No time left for Player 1\n")
file.write(f"Time Left. Player 1: {p1_time}, Player 2: {p2_time}\n")
return
tl = dynamic_time_management(board=board, time=p2_time)
t1 = time.time()
move = player_2_function(board=board, time_limit=tl)
print(move)
t2 = time.time()
p2_time -= (t2 - t1)
board.do_move(move)
if game_ending(board):
with open("contest_results.dat", "a") as file:
file.write("Game Ended. Winner: Player 2\n")
if check_for_remi(board):
file.write("Reason: Remi\n")
elif check_for_checkmate(board):
file.write("Reason: Checkmate\n")
elif check_king_hill(board):
file.write("Reason: King Hill\n")
file.write(f"Time Left. Player 1: {p1_time}, Player 2: {p2_time}\n")
return
if p1_time <= 0:
with open("contest_results.dat", "a") as file:
file.write(" Game Ended. Winner: Player 1\n")
file.write("Reason: No time left for Player 2\n")
file.write(f"Time Left. Player 1: {p1_time}, Player 2: {p2_time}\n")
return
with open("contest_results.dat", "a") as file:
file.write("Game did not End after 200 Moves. Abort.\n")
return
def play(player_1_function, player_2_function, chess_clock_time=30):
with open("contest_results.dat", "a") as file:
file.write("---- NEW GAME FIXED TIME ----\n")
file.write(f"player 1: {player_1_function.__name__}\n")
file.write(f"player 2: {player_2_function.__name__}\n")
REMI_D = {}
board = Board(START_FEN)
for i in range(200):
move = player_1_function(board=board, time_limit=chess_clock_time)
board.do_move(move)
if game_ending(board):
with open("contest_results.dat", "a") as file:
file.write("Game Ended. Winner: Player 1\n")
if check_for_remi(board):
file.write("Reason: Remi\n")
elif check_for_checkmate(board):
file.write("Reason: Checkmate\n")
elif check_king_hill(board):
file.write("Reason: King Hill\n")
return
if move.capture:
REMI_D = {}
else:
if hash(board) in REMI_D:
REMI_D[hash(board)] = REMI_D[hash(board)] + 1
if REMI_D[hash(board)] == 3:
with open("contest_results.dat", "a") as file:
file.write("Game Ended. No Winner")
file.write("Reason: Real Remi --\n")
return
else:
REMI_D[hash(board)]= 1
move = player_2_function(board=board, time_limit=chess_clock_time)
board.do_move(move)
if game_ending(board):
with open("contest_results.dat", "a") as file:
file.write("Game Ended. Winner: Player 2\n")
if check_for_remi(board):
file.write("Reason: Remi\n")
elif check_for_checkmate(board):
file.write("Reason: Checkmate\n")
elif check_king_hill(board):
file.write("Reason: King Hill\n")
return
if move.capture:
REMI_D = {}
else:
if hash(board) in REMI_D:
REMI_D[hash(board)] = REMI_D[hash(board)] + 1
if REMI_D[hash(board)] == 3:
with open("contest_results.dat", "a") as file:
file.write("Game Ended. No Winner")
file.write("Reason: Real Remi --\n")
return
else:
REMI_D[hash(board)]= 1
with open("contest_results.dat", "a") as file:
file.write("Game did not End after 200 Moves. Abort.\n")
return
def start_contest(chess_clock_time=300):
try:
play(player_1_function=mcts, player_2_function=pvs_sort, chess_clock_time=chess_clock_time)
play(player_1_function=mcts, player_2_function=pvs_multi_sort, chess_clock_time=chess_clock_time)
play(player_1_function=mcts, player_2_function=pvs_sort_qs, chess_clock_time=chess_clock_time)
play(player_1_function=mcts, player_2_function=pvs_multi_sort_qs, chess_clock_time=chess_clock_time)
except Exception as e:
with open("contest_results.dat", "a") as file:
file.write(f"Exception in 1: {e}\n")
try:
play(player_1_function=pvs_sort, player_2_function=pvs_multi_sort_qs, chess_clock_time=chess_clock_time)
play(player_1_function=pvs_sort, player_2_function=pvs_multi_sort, chess_clock_time=chess_clock_time)
play(player_1_function=pvs_sort, player_2_function=pvs_sort_qs, chess_clock_time=chess_clock_time)
play(player_1_function=pvs_sort, player_2_function=mcts, chess_clock_time=chess_clock_time)
except Exception as e:
with open("contest_results.dat", "a") as file:
file.write(f"Exception in 2: {e}\n")
try:
play(player_1_function=pvs_multi_sort, player_2_function=pvs_multi_sort_qs, chess_clock_time=chess_clock_time)
play(player_1_function=pvs_multi_sort, player_2_function=pvs_sort, chess_clock_time=chess_clock_time)
play(player_1_function=pvs_multi_sort, player_2_function=pvs_sort_qs, chess_clock_time=chess_clock_time)
play(player_1_function=pvs_multi_sort, player_2_function=mcts, chess_clock_time=chess_clock_time)
except Exception as e:
with open("contest_results.dat", "a") as file:
file.write(f"Exception in 3: {e}\n")
try:
play(player_1_function=pvs_sort_qs, player_2_function=pvs_multi_sort_qs, chess_clock_time=chess_clock_time)
play(player_1_function=pvs_sort_qs, player_2_function=pvs_sort, chess_clock_time=chess_clock_time)
play(player_1_function=pvs_sort_qs, player_2_function=pvs_multi_sort, chess_clock_time=chess_clock_time)
play(player_1_function=pvs_sort_qs, player_2_function=mcts, chess_clock_time=chess_clock_time)
except Exception as e:
with open("contest_results.dat", "a") as file:
file.write(f"Exception in 4: {e}\n")
try:
play(player_1_function=pvs_multi_sort_qs, player_2_function=pvs_sort_qs, chess_clock_time=chess_clock_time)
play(player_1_function=pvs_multi_sort_qs, player_2_function=pvs_sort, chess_clock_time=chess_clock_time)
play(player_1_function=pvs_multi_sort_qs, player_2_function=pvs_multi_sort, chess_clock_time=chess_clock_time)
play(player_1_function=pvs_multi_sort_qs, player_2_function=mcts, chess_clock_time=chess_clock_time)
except Exception as e:
with open("contest_results.dat", "a") as file:
file.write(f"Exception in 5: {e}\n")
if __name__ == "__main__":
gen_all_masks()
start_contest(7)