-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess2.py
504 lines (445 loc) · 18.8 KB
/
chess2.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import chess
import tkinter as tk
from tkinter import messagebox
# Global variables for GUI
board_buttons = {}
selected_square = None
last_move_start = None
last_move_end = None
# Board dimensions
BOARD_WIDTH = BOARD_HEIGHT = 700
SQUARE_SIZE = BOARD_WIDTH // 8
MAX_DEPTH = 1
# Colors
LIGHT_SQUARE_COLOR = "#F0D9B5" # Hexadecimal value for (240, 217, 181)
DARK_SQUARE_COLOR = "#B58863" # Hexadecimal value for (181, 136, 99)
SELECTED_SQUARE_COLOR = "#7A9ECA" # Hexadecimal value for (122, 158, 202)
LAST_MOVE_COLOR = "#FFA500" # Hexadecimal value for red
WHITE_PIECE_ICONS = {
chess.PAWN: "♙",
chess.KNIGHT: "♘",
chess.BISHOP: "♗",
chess.ROOK: "♖",
chess.QUEEN: "♕",
chess.KING: "♔",
}
BLACK_PIECE_ICONS = {
chess.PAWN: "♟",
chess.KNIGHT: "♞",
chess.BISHOP: "♝",
chess.ROOK: "♜",
chess.QUEEN: "♛",
chess.KING: "♚",
}
# Initialize the chess board
board = chess.Board()
def evaluate_board(board, turn):
#print("{}".format(turn))
'''
if turn == chess.BLACK:
print("black")
else:
print("white")
'''
piece_weights = {
chess.PAWN: 100,
chess.KNIGHT: 320,
chess.BISHOP: 330,
chess.ROOK: 500,
chess.QUEEN: 900,
chess.KING: 20000
}
#using piece square tables from the simplified evaluation fucntion
p_w = [
[ 0, 0, 0, 0, 0, 0, 0, 0],
[50, 50, 50, 50, 50, 50, 50, 50],
[10, 10, 20, 30, 30, 20, 10, 10],
[ 5, 5, 10, 25, 25, 10, 5, 5],
[ 0, 0, 0, 20, 20, 0, 0, 0],
[ 5, -5,-10, 0, 0,-10, -5, 5],
[ 5, 10, 10,-20,-20, 10, 10, 5],
[ 0, 0, 0, 0, 0, 0, 0, 0]
]
p_b = [
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 5, 10, 10,-20,-20, 10, 10, 5],
[ 5, -5,-10, 0, 0,-10, -5, 5],
[ 0, 0, 0, 20, 20, 0, 0, 0],
[ 5, 5, 10, 25, 25, 10, 5, 5],
[10, 10, 20, 30, 30, 20, 10, 10],
[50, 50, 50, 50, 50, 50, 50, 50],
[ 0, 0, 0, 0, 0, 0, 0, 0]
]
n_w = [
[-50, -40, -30, -30, -30, -30, -40, -50],
[-40, -20, 0, 0, 0, 0, -20, -40],
[-30, 0, 10, 15, 15, 10, 0, -30],
[-30, 5, 15, 20, 20, 15, 5, -30],
[-30, 0, 15, 20, 20, 15, 0, -30],
[-30, 5, 10, 15, 15, 10, 5, -30],
[-40, -20, 0, 5, 5, 0, -20, -40],
[-50, -40, -30, -30, -30, -30, -40, -50]
]
n_b = [
[-50, -40, -30, -30, -30, -30, -40, -50],
[-40, -20, 0, 5, 5, 0, -20, -40],
[-30, 5, 10, 15, 15, 10, 5, -30],
[-30, 0, 15, 20, 20, 15, 0, -30],
[-30, 5, 15, 20, 20, 15, 5, -30],
[-30, 0, 10, 15, 15, 10, 0, -30],
[-40, -20, 0, 0, 0, 0, -20, -40],
[-50, -40, -30, -30, -30, -30, -40, -50]
]
b_w = [
[-20, -10, -10, -10, -10, -10, -10, -20],
[-10, 0, 0, 0, 0, 0, 0, -10],
[-10, 0, 5, 10, 10, 5, 0, -10],
[-10, 5, 5, 10, 10, 5, 5, -10],
[-10, 0, 10, 10, 10, 10, 0, -10],
[-10, 10, 10, 10, 10, 10, 10, -10],
[-10, 5, 0, 0, 0, 0, 5, -10],
[-20, -10, -10, -10, -10, -10, -10, -20]
]
b_b = [
[-20, -10, -10, -10, -10, -10, -10, -20],
[-10, 5, 0, 0, 0, 0, 5, -10],
[-10, 10, 10, 10, 10, 10, 10, -10],
[-10, 0, 10, 10, 10, 10, 0, -10],
[-10, 5, 5, 10, 10, 5, 5, -10],
[-10, 0, 5, 10, 10, 5, 0, -10],
[-10, 0, 0, 0, 0, 0, 0, -10],
[-20, -10, -10, -10, -10, -10, -10, -20]
]
r_w = [
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 5, 10, 10, 10, 10, 10, 10, 5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[ 0, 0, 0, 5, 5, 0, 0, 0]
]
r_b = [
[ 0, 0, 0, 5, 5, 0, 0, 0],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[ 5, 10, 10, 10, 10, 10, 10, 5],
[ 0, 0, 0, 0, 0, 0, 0, 0]
]
q_w = [
[-20, -10, -10, -5, -5, -10, -10, -20],
[-10, 0, 0, 0, 0, 0, 0, -10],
[-10, 0, 5, 5, 5, 5, 0, -10],
[ -5, 0, 5, 5, 5, 5, 0, -5],
[ 0, 0, 5, 5, 5, 5, 0, -5],
[-10, 5, 5, 5, 5, 5, 0, -10],
[-10, 0, 5, 0, 0, 0, 0, -10],
[-20, -10, -10, -5, -5, -10, -10, -20]
]
q_b = [
[-20, -10, -10, -5, -5, -10, -10, -20],
[-10, 0, 5, 0, 0, 0, 0, -10],
[-10, 5, 5, 5, 5, 5, 0, -10],
[ 0, 0, 5, 5, 5, 5, 0, -5],
[ -5, 0, 5, 5, 5, 5, 0, -5],
[ -5, 0, 5, 5, 5, 5, 0, -10],
[-10, 0, 0, 0, 0, 0, 0, -10],
[-20, -10, -10, -5, -5, -10, -10, -20]
]
#not great for end game
k_w = [
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-20, -30, -30, -40, -40, -30, -30, -20],
[-10, -20, -20, -20, -20, -20, -20, -10],
[ 20, 20, 0, 0, 0, 0, 20, 20],
[ 20, 30, 10, 0, 0, 10, 30, 20]
]
k_b = [
[ 20, 30, 10, 0, 0, 10, 30, 20],
[ 20, 20, 0, 0, 0, 0, 20, 20],
[-10, -20, -20, -20, -20, -20, -20, -10],
[-20, -30, -30, -40, -40, -30, -30, -20],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30]
]
total_evaluation = 0
pawns = {}
isolated_pawns = {}
doubled_pawns = {}
blocked_pawns = {}
black_pawns = {}
black_isolated_pawns = {}
black_doubled_pawns = {}
black_blocked_pawns = {}
for rank in range(8):
for file in range(8):
square = chess.square(file, rank)
piece = board.piece_at(square)
if piece is not None:
if piece.piece_type == chess.PAWN:
if piece.color == chess.WHITE:
pawn_value = chess.PAWN + p_w[rank][file]
else:
pawn_value = -chess.PAWN - p_b[rank][file]
total_evaluation += pawn_value
if piece.color == chess.WHITE:
if file in pawns:
pawns[file].append(pawn_value)
else:
pawns[file] = [pawn_value]
if file - 1 not in pawns and file + 1 not in pawns:
isolated_pawns[file] = True
if file in doubled_pawns:
doubled_pawns[file] += 1
else:
doubled_pawns[file] = 1
if piece.color == chess.WHITE:
blocking_rank = rank - 1
else:
blocking_rank = rank + 1
if blocking_rank >= 0 and blocking_rank <= 7:
blocking_square = chess.square(file, blocking_rank)
blocking_piece = board.piece_at(blocking_square)
if blocking_piece is not None and blocking_piece.piece_type == chess.PAWN:
blocked_pawns[file] = True
else:
if file in black_pawns:
black_pawns[file].append(pawn_value)
else:
black_pawns[file] = [pawn_value]
if file - 1 not in black_pawns and file + 1 not in black_pawns:
black_isolated_pawns[file] = True
if file in black_doubled_pawns:
black_doubled_pawns[file] += 1
else:
black_doubled_pawns[file] = 1
blocking_rank = rank + 1
if blocking_rank >= 0 and blocking_rank <= 7:
blocking_square = chess.square(file, blocking_rank)
blocking_piece = board.piece_at(blocking_square)
if blocking_piece is not None and blocking_piece.piece_type == chess.PAWN:
black_blocked_pawns[file] = True
elif piece.piece_type == chess.ROOK:
total_evaluation += chess.ROOK * (1 if piece.color == chess.WHITE else -1) + (r_w[rank][file] if piece.color == chess.WHITE else -r_b[rank][file])
elif piece.piece_type == chess.KNIGHT:
total_evaluation += chess.KNIGHT * (1 if piece.color == chess.WHITE else -1) + (n_w[rank][file] if piece.color == chess.WHITE else -n_b[rank][file])
elif piece.piece_type == chess.BISHOP:
total_evaluation += chess.BISHOP * (1 if piece.color == chess.WHITE else -1) + (b_w[rank][file] if piece.color == chess.WHITE else -b_b[rank][file])
elif piece.piece_type == chess.QUEEN:
total_evaluation += chess.QUEEN * (1 if piece.color == chess.WHITE else -1) + (q_w[rank][file] if piece.color == chess.WHITE else -q_b[rank][file])
elif piece.piece_type == chess.KING:
total_evaluation += chess.KING * (1 if piece.color == chess.WHITE else -1) + (k_w[rank][file] if piece.color == chess.WHITE else -k_b[rank][file])
total_evaluation += len(isolated_pawns) * (-100)
total_evaluation += len(doubled_pawns) * (-200)
total_evaluation += len(blocked_pawns) * (-100)
total_evaluation += len(black_isolated_pawns) * (100)
total_evaluation += len(black_doubled_pawns) * (200)
total_evaluation += len(black_blocked_pawns) * (100)
player_legal_moves = list(board.legal_moves)
#print("player_legal_moves: {}".format(player_legal_moves))
board.turn = not board.turn # Switch to opponent's turn
ai_legal_moves = list(board.legal_moves)
#print("ai_legal_moves: {}".format(ai_legal_moves))
board.turn = not board.turn # Switch to opponent's turn
mobility_score = 10 * (len(ai_legal_moves) - len(player_legal_moves))
total_evaluation += mobility_score
captures_score = 5 * len([move for move in ai_legal_moves if board.is_capture(move)])
total_evaluation += captures_score
captured_score = -5 * len([move for move in player_legal_moves if board.is_capture(move)])
total_evaluation += captured_score
#weight might be off
threatened_pieces_score = 0.0
for square in chess.SQUARES:
piece = board.piece_at(square)
if piece and piece.color == chess.BLACK:
if any(move.to_square == square and board.is_capture(move) for move in player_legal_moves):
threatened_pieces_score -= 10 * piece_weights.get(piece.piece_type, 0)
total_evaluation += threatened_pieces_score
threatening_pieces_score = 0.0
for square in chess.SQUARES:
piece = board.piece_at(square)
if piece and piece.color == chess.WHITE:
if any(move.to_square == square and board.is_capture(move) for move in ai_legal_moves):
#weights commented until balanced
if piece.piece_type != chess.KING or not board.is_check():
threatened_pieces_score = threatened_pieces_score
#threatening_pieces_score += piece_weights.get(piece.piece_type, 0)
else:
threatening_piece_square = get_threatening_piece_square(board, square)
if board.is_checkmate():
threatening_pieces_score = float('inf')
elif is_square_safe(board, threatening_piece_square):
threatened_pieces_score = threatened_pieces_score
#threatening_pieces_score += piece_weights.get(piece.piece_type, 0)
total_evaluation += threatening_pieces_score
'''
print("Total Evaluation: {}".format(total_evaluation))
print("Isolated Pawns: {}".format(isolated_pawns))
print("Doubled Pawns: {}".format(doubled_pawns))
print("Blocked Pawns: {}".format(blocked_pawns))
print("Black Pawns: {}".format(black_pawns))
print("Black Isolated Pawns: {}".format(black_isolated_pawns))
print("Black Doubled Pawns: {}".format(black_doubled_pawns))
print("Black Blocked Pawns: {}".format(black_blocked_pawns))
#print("Player Legal Moves: {}".format(player_legal_moves))
#print("Opponent Legal Moves: {}".format(opponent_legal_moves))
print("Mobility Score: {}".format(mobility_score))
print("Captures Score: {}".format(captured_score))
print("Captures Score: {}".format(captures_score))
print("Threatened Pieces Score: {}".format(threatened_pieces_score))
print("Threatening Pieces Score: {}\n".format(threatening_pieces_score))
'''
if turn == chess.BLACK:
total_evaluation = -total_evaluation
return total_evaluation
def get_threatening_piece_square(board, target_square):
piece = board.piece_at(target_square)
if piece is not None:
attackers = board.attackers(not piece.color, target_square)
if attackers:
return attackers.pop()
return None
def is_square_safe(board, square):
piece = board.piece_at(square)
if piece is not None:
attackers = board.attackers(not piece.color, square)
return len(attackers) == 0
return True
def alphabeta(board, depth, alpha, beta, maximizing_player):
best_value = float('-inf') if maximizing_player else float('inf')
if depth:
if maximizing_player:
for move in list(board.legal_moves):
board.push(move)
min_player = not maximizing_player
recurse = alphabeta(board, depth-1, alpha, beta, min_player)
if best_value < recurse:
best_value = recurse
board.pop()
if best_value > alpha:
alpha = best_value
if alpha >= beta:
break
#print(" " * (depth) + f"Depth {depth} Max: {best_value}")
return best_value
else:
for move in list(board.legal_moves):
board.push(move)
min_player = not maximizing_player
recurse = alphabeta(board, depth-1, alpha, beta, min_player)
if best_value > recurse:
best_value = recurse
board.pop()
if best_value < beta:
beta = best_value
if alpha >= beta:
break
#print(" " * (depth) + f"Depth {depth} Min: {best_value}")
return best_value
else:
x=evaluate_board(board, not maximizing_player)
#print(" " * (depth) + f"Depth {depth} Score: {x}")
return x
def on_square_click(square):
global selected_square
if selected_square is None:
selected_square = square
else:
move = chess.Move(selected_square, square)
if move in board.legal_moves:
board.push(move)
if (
board.is_checkmate()
or board.is_stalemate()
or board.is_insufficient_material()
or board.is_seventyfive_moves()
or board.is_fivefold_repetition()
):
messagebox.showinfo("Game Over", "Game Over!")
else:
# Check if the move resulted in pawn promotion
if move.promotion:
promotion_piece = move.promotion
# Manually set the promoted piece on the board
board.set_piece_at(square, chess.Piece.from_symbol(promotion_piece))
refresh_board()
selected_square = None
make_ai_move()
refresh_board()
# Check if the game is over (checkmate)
if board.is_checkmate():
messagebox.showinfo("Game Over", "Checkmate! You lost.") # Display checkmate message
else:
messagebox.showinfo("Invalid Move", "Invalid move. Please try again.")
selected_square = None
def refresh_board():
for square, button in board_buttons.items():
piece = board.piece_at(square)
if piece:
if piece.color == chess.WHITE:
piece_symbol = WHITE_PIECE_ICONS.get(piece.piece_type, "")
else:
piece_symbol = BLACK_PIECE_ICONS.get(piece.piece_type, "")
else:
piece_symbol = ""
button["text"] = piece_symbol
button.config(font=("Arial", 15)) # Increased font size
button_color = LIGHT_SQUARE_COLOR if (chess.square_file(square) + chess.square_rank(square)) % 2 == 0 else DARK_SQUARE_COLOR
if square == selected_square:
button_color = SELECTED_SQUARE_COLOR
elif square == last_move_start or square == last_move_end:
button_color = LAST_MOVE_COLOR
button["bg"] = button_color
def make_ai_move():
global last_move_start, last_move_end
depth = MAX_DEPTH # 5 takes ~3min for the first move, 4 takes ~1min, 3 takes ~3sec
legal_moves = list(board.legal_moves)
if len(legal_moves) == 0:
print("Game Over - No valid moves available")
return
best_move = None
best_score = -float('inf')
for move in legal_moves:
board.push(move)
score = alphabeta(board, depth - 1, -float('inf'), float('inf'), False)
#print("move: {}".format(move))
board.pop()
#print("score: {}\n".format(score))
if score > best_score:
best_score = score
best_move = move
#print("best: {}".format(best_score))
if best_move is not None:
last_move_start = best_move.from_square
last_move_end = best_move.to_square
board.push(best_move)
else:
print("No valid move found by AI")
def create_board_ui(root):
global board_buttons
board_frame = tk.Frame(root)
board_frame.pack()
for row in range(8):
for col in range(8):
square = chess.square(col, 7 - row)
button = tk.Button(board_frame, width=7, height=3, command=lambda sq=square: on_square_click(sq)) # Increased button size
button.grid(row=row, column=col)
board_buttons[square] = button
def play_chess():
root = tk.Tk()
root.title("Chess")
root.geometry(f"{BOARD_WIDTH}x{BOARD_HEIGHT}") # Set window size
create_board_ui(root)
refresh_board()
root.mainloop()
# Example usage
play_chess()