-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestmain.py
More file actions
360 lines (286 loc) · 13.4 KB
/
testmain.py
File metadata and controls
360 lines (286 loc) · 13.4 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import pygame
import chess
import chess.pgn
import time
from var import *
from initi import *
from savingtsptables import pure_minimax_best_move,enhanced_best_move, load_transposition_table, save_transposition_table, enhanced_tt
from A0NN import *
MaxDepth = 4
def board_score_fast(board):
"""Lightning-fast evaluation using python-chess"""
if board.is_checkmate():
return -9999 if board.turn else 9999
if board.is_stalemate() or board.is_insufficient_material():
return 0
piece_values = {
chess.PAWN: 100,
chess.KNIGHT: 320,
chess.BISHOP: 330,
chess.ROOK: 500,
chess.QUEEN: 900,
chess.KING: 0
}
score = 0
for square in chess.SQUARES:
piece = board.piece_at(square)
if piece:
value = piece_values[piece.piece_type]
if piece.color == chess.WHITE:
score += value
else:
score -= value
return score
def convert_square_to_chess(rank, file):
"""Convert pygame coordinates to chess square"""
# pygame uses (rank, file) where rank 0 is top, file 0 is left
# chess uses algebraic notation where a1 is bottom-left
chess_file = file # a=0, b=1, ..., h=7
chess_rank = 7 - rank # 1st rank = 7, 8th rank = 0 in pygame
return chess.square(chess_file, chess_rank)
def convert_chess_to_square(chess_square):
"""Convert chess square to pygame coordinates"""
file = chess.square_file(chess_square)
rank = chess.square_rank(chess_square)
pygame_rank = 7 - rank
pygame_file = file
return pygame_rank, pygame_file
def get_piece_at_pygame_pos(board, rank, file):
"""Get piece at pygame position"""
chess_square = convert_square_to_chess(rank, file)
return board.piece_at(chess_square)
def is_valid_position(rank, file):
"""Check if position is within board bounds"""
return 0 <= rank < 8 and 0 <= file < 8
def get_legal_moves_for_square(board, rank, file):
"""Get legal moves for a piece at pygame coordinates"""
chess_square = convert_square_to_chess(rank, file)
piece = board.piece_at(chess_square)
if not piece:
return []
legal_moves = []
for move in board.legal_moves:
if move.from_square == chess_square:
to_rank, to_file = convert_chess_to_square(move.to_square)
legal_moves.append((to_rank, to_file))
return legal_moves
def make_move_from_pygame(board, from_rank, from_file, to_rank, to_file):
"""Make a move using pygame coordinates"""
from_square = convert_square_to_chess(from_rank, from_file)
to_square = convert_square_to_chess(to_rank, to_file)
# Handle pawn promotion - always promote to queen for simplicity
piece = board.piece_at(from_square)
move = chess.Move(from_square, to_square)
if piece and piece.piece_type == chess.PAWN:
# Check if it's a promotion move
if (piece.color == chess.WHITE and to_rank == 0) or \
(piece.color == chess.BLACK and to_rank == 7):
move = chess.Move(from_square, to_square, promotion=chess.QUEEN)
if move in board.legal_moves:
board.push(move)
return True
return False
def draw_board_from_chess(screen, board, selected_pos=None, legal_moves=None, last_move=None):
"""Draw the chess board using python-chess board state"""
# Draw squares
for rank in range(8):
for file in range(8):
color = LIGHT_COLOR if (rank + file) % 2 == 0 else DARK_COLOR
# Highlight selected square
if selected_pos and selected_pos == (rank, file):
color = SELECTED_COLOR
# Highlight legal moves
if legal_moves and (rank, file) in legal_moves:
color = HIGHLIGHT_COLOR_LIGHT if (rank + file) % 2 == 0 else HIGHLIGHT_COLOR_DARK
# Highlight last move
if last_move:
from_rank, from_file = convert_chess_to_square(last_move.from_square)
to_rank, to_file = convert_chess_to_square(last_move.to_square)
if (rank, file) in [(from_rank, from_file), (to_rank, to_file)]:
color = LAST_MOVE_COLOR
rect = pygame.Rect(file * SQUARE_SIZE, rank * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE)
pygame.draw.rect(screen, color, rect)
# Draw pieces
for rank in range(8):
for file in range(8):
chess_square = convert_square_to_chess(rank, file)
piece = board.piece_at(chess_square)
if piece:
# Convert python-chess piece to our piece notation
color_char = 'w' if piece.color == chess.WHITE else 'b'
piece_char = piece.symbol().upper()
piece_key = color_char + piece_char
if piece_key in PIECE_IMAGES:
piece_image = PIECE_IMAGES[piece_key]
piece_rect = piece_image.get_rect()
piece_rect.center = (file * SQUARE_SIZE + SQUARE_SIZE // 2,
rank * SQUARE_SIZE + SQUARE_SIZE // 2)
screen.blit(piece_image, piece_rect)
def check_game_over(board):
"""Check if the game is over and update game state"""
if board.is_game_over():
if board.is_checkmate():
winner = "White" if board.turn == chess.BLACK else "Black"
print(f"Checkmate! {winner} wins!")
game_state.winner = winner
elif board.is_stalemate():
print("Stalemate!")
game_state.winner = "Draw"
elif board.is_insufficient_material():
print("Draw by insufficient material!")
game_state.winner = "Draw"
else:
print("Game over!")
game_state.winner = "Draw"
game_state.game_over = True
return True
return False
def train_on_engine(model,num_games=100,max_moves=50):
for j in range(num_games):
board=chess.Board()
moves_made=0
while not board.is_game_over() and moves_made<max_moves:
#engine_move=enhanced_best_move(board,save=False,load=False,use_tt=False)
engine_move=pure_minimax_best_move(board)
if not engine_move:
break
board_tensor=board_to_tensor(board)
target_policy=np.zeros(4672)
move_idx=move_to_idx(engine_move)
target_policy[move_idx]=1
engine_score=board_score_enhanced(board)
target_values=np.tanh(engine_score/1000.0)
model.fit(board_tensor[None,...],{
'policy':target_policy[None,...],'value':np.array([[target_values]])
},epochs=1,verbose=0)
#After making the move
board.push(engine_move)
moves_made+=1
if not board.is_game_over():
result_board_tensor=board_to_tensor(board)
result_score=board_score_enhanced(board)
result_values=np.tanh(result_score/1000.0)
model.fit(result_board_tensor[None,...],{
'policy':target_policy[None,...],'value':np.array([[result_values]])
},epochs=1,verbose=0)
return model
def save_model(model,filename='training_model.weights.h5'):
model.save_weights(filename)
print("Saved model to file successfully")
def load_model(model,filename='training_model.weights.h5'):
try:
model.load_weights(filename)
print("Loaded model successfulyl")
return True
except Exception as e:
print(f"Couldn't load model. Erros: {e}")
return False
def main():
pygame.init()
load_piece_images()
load_transposition_table()
# Initialize python-chess board
board = chess.Board()
model=create_alpha0()
#print(model.summary())
if not load_model(model):
print("No existing model found. Training...")
train_on_engine(model)
save_model(model)
running = True
selected_piece_pos = None
legal_moves = []
print("Enhanced Chess AI loaded with python-chess!")
print("Transposition table ready.")
print(f"Starting position: {board.fen()}")
while running:
mouse_pos = pygame.mouse.get_pos()
# AI's turn (Black)
if board.turn == chess.BLACK and not board.is_game_over():
start_time = time.time()
print(f"\nBlack thinking (depth {MaxDepth})...")
print(f"Position: {board.fen()}")
# Use the enhanced best move function
best_move = enhanced_best_move(board, save=True, load=False)
best_move2=mcts_search(model,board)
if best_move2:
print(f"AB plays: {best_move}")
print(f"Black plays: {best_move2}")
board.push(best_move2)
end_time = time.time()
print(f"Black move completed in {end_time-start_time:.3f}s")
print(enhanced_tt.get_stats())
check_game_over(board)
else:
print("No legal moves found for black!")
game_state.game_over = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("Saving transposition table...")
save_transposition_table()
running = False
elif event.type == pygame.MOUSEBUTTONDOWN and not board.is_game_over():
if board.turn == chess.WHITE: # Only allow moves on White's turn
rank = mouse_pos[1] // SQUARE_SIZE
file = mouse_pos[0] // SQUARE_SIZE
if is_valid_position(rank, file):
piece = get_piece_at_pygame_pos(board, rank, file)
# Only allow selection of White pieces
if piece and piece.color == chess.WHITE:
possible_moves = get_legal_moves_for_square(board, rank, file)
if possible_moves:
selected_piece_pos = (rank, file)
legal_moves = possible_moves
print(f"Selected {piece.symbol()} at ({rank},{file}) with {len(possible_moves)} legal moves")
elif event.type == pygame.MOUSEBUTTONUP and not board.is_game_over():
if board.turn == chess.WHITE and selected_piece_pos:
rank = mouse_pos[1] // SQUARE_SIZE
file = mouse_pos[0] // SQUARE_SIZE
orig_rank, orig_file = selected_piece_pos
if is_valid_position(rank, file) and (rank, file) in legal_moves:
piece = get_piece_at_pygame_pos(board, orig_rank, orig_file)
print(f"White moves {piece.symbol()} from ({orig_rank},{orig_file}) to ({rank},{file})")
# Make the move
if make_move_from_pygame(board, orig_rank, orig_file, rank, file):
print(f"Move successful. New position: {board.fen()}")
check_game_over(board)
else:
print("Invalid move!")
# Clear selection
selected_piece_pos = None
legal_moves = []
# Draw everything
screen.fill((0, 0, 0))
last_move = board.peek() if board.move_stack else None
draw_board_from_chess(screen, board, selected_piece_pos, legal_moves, last_move)
# If a piece is selected, show it following the mouse
if selected_piece_pos and board.turn == chess.WHITE and not board.is_game_over():
piece = get_piece_at_pygame_pos(board, selected_piece_pos[0], selected_piece_pos[1])
if piece:
color_char = 'w' if piece.color == chess.WHITE else 'b'
piece_char = piece.symbol().upper()
piece_key = color_char + piece_char
if piece_key in PIECE_IMAGES:
piece_image = PIECE_IMAGES[piece_key]
piece_rect = piece_image.get_rect(center=mouse_pos)
screen.blit(piece_image, piece_rect.topleft)
# Display game status
if board.is_game_over():
if board.is_checkmate():
winner = "White" if board.turn == chess.BLACK else "Black"
status_text = f"Checkmate! {winner} wins!"
elif board.is_stalemate():
status_text = "Stalemate!"
else:
status_text = "Game Over!"
rl_agent.save_model()
text_surface = SMALL_FONT.render(status_text, True, (255, 255, 255))
text_rect = text_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2))
screen.blit(text_surface, text_rect)
pygame.display.flip()
print("Final save of transposition table...")
save_transposition_table()
print(f"Final stats: {enhanced_tt.get_stats()}")
pygame.quit()
if __name__ == '__main__':
main()