-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessMain.py
More file actions
563 lines (465 loc) · 26.2 KB
/
Copy pathChessMain.py
File metadata and controls
563 lines (465 loc) · 26.2 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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
"""
By Paul Robert Andreini
08 Feb 2026
Code here is LOOSELY based on a YouTube tutorial series, whose playlist is visible at the following link:
https://www.youtube.com/playlist?list=PLBwF487qi8MGU81nDGaeNE1EnNEPYWKY_
This code is for EPISODE 11 (cf. Sharick Ep. 9).
MAIN DRIVER FILE is responsible for:
(a) handling user input;
(b) displaying the current GameState object.
"""
## Importing relevant packages...
from ChessEngine11 import *
import pygame as p
p.init()
p.display.set_caption("Chess!")
#######################################################################################################################
## Defining CONSTANTS:
WIDTH = HEIGHT = 1024 ## 1024 = 2^10.
DIMENSION = 8 ## Chess board is 8x8; 8 = 2^3.
SQ_SIZE = WIDTH // DIMENSION ## 2^10 / 2^3 = 2^7; double-divide is integer division.
MAX_FPS = 60 ## For animations later on...
PIECES = {} ## Empty (for now) dictionary to store the image files for each piece.
## For colors, we will use the same as seen on Chess.com, specified via an (R,G,B)-tuple.
LIGHT = p.Color((235, 236, 211)) ## Light color is OFF-WHITE.
DARK = p.Color((122, 148, 90)) ## Dark color is DARK-GREEN.
COLORS = [LIGHT, DARK]
ALPHA = 150 ## How opaque do we want highlights to be, on the open set [0, 255] (larger num ==> more opaque)?
#######################################################################################################################
## Defining STATIC FUNCTIONS.
def load_piece_images(sq_size: int):
"""
Initializes a dictionary (GLOBAL scope) of image files for each piece.
Need to use the "global" keyword to modify a global-variable within the scope of this function.
"""
global PIECES
colors = ['w', 'b']
types = ['P', 'N', 'B', 'R', 'Q', 'K']
for c in colors:
for t in types:
PIECES[f"{c}{t}"] = p.transform.scale(p.image.load(f"pieces/{c}{t}.png"), (sq_size, sq_size))
def draw_board(win, sq_size: int, gs: GameState):
"""
Draws the squares on the board (from white's perspective, unless gs.flipped).
N.B. the rhyming mnemonic: WHICHEVER perspective, the BOTTOM-RIGHT square is ALWAYS LIGHT!
"""
for r in range(DIMENSION):
for c in range(DIMENSION):
fill_color = COLORS[((r + c) % 2)]
p.draw.rect(win, fill_color, p.Rect(c*sq_size, r*sq_size, sq_size, sq_size))
## Labeling the RANKS: upper-left corner of the squares on the left-edge of the board.
if c == 0:
label_color = COLORS[((r + c + 1) % 2)]
rank_number = Move.rows_to_ranks[r] if not gs.flipped else Move.flipped_rows_to_ranks[r]
font = p.font.SysFont(name="Arial", size=20, bold=True, italic=False)
text_object = font.render(rank_number, True, label_color)
text_location = p.Rect(0, 0, WIDTH, HEIGHT).move(
(sq_size - text_object.get_width())/30,
(r * sq_size) + (sq_size - text_object.get_height()) / 30
)
win.blit(text_object, text_location)
## Labeling the FILES: lower-right corner of the squares on the squares on the bottom-edge of the board.
if r == DIMENSION-1:
label_color = COLORS[((r + c + 1) % 2)]
file_letter = Move.cols_to_files[c] if not gs.flipped else Move.flipped_cols_to_files[c]
font = p.font.SysFont(name="Arial", size=20, bold=True, italic=False)
text_object = font.render(file_letter, True, label_color)
text_location = p.Rect(0, 0, WIDTH, HEIGHT).move(
(c * sq_size) + (sq_size - text_object.get_width()) * (29 / 30),
(7 * sq_size) + (sq_size - text_object.get_height()) * (29/30)
)
win.blit(text_object, text_location)
def draw_pieces(win, sq_size: int, gs: GameState):
"""
Draws the pieces on top of the squares on the board.
"""
board = gs.board
for r in range(DIMENSION):
for c in range(DIMENSION):
piece = board[r][c]
if piece != "--":
row = r if not gs.flipped else 7 - r
col = c if not gs.flipped else 7 - c
## If the square has a piece on it (i.e., NOT empty), then draw it!
win.blit(PIECES[piece], p.Rect(col*sq_size, row*sq_size, sq_size, sq_size))
def highlight_possible_squares(win, sq_size: int, gs: GameState, valid_moves, square_selected):
"""
Highlights the piece selected (in blue) and available moves (if any) in orange.
This convenient feature for human players allows one to explore all legal moves for a given piece.
"""
if not square_selected == ():
r, c = square_selected ## Add in a case for the flipped-board!
## Making sure that the current player selected a piece that s/he can move (his/her color).
if gs.board[r][c][0] == ("w" if gs.white_to_move else "b"):
## Highlight the selected square.
s = p.Surface((sq_size, sq_size))
s.set_alpha(ALPHA) ## Transparency value on the closed set [0, 255]; 0 is transparent, 255 is opaque.
s.fill(p.Color("blue"))
if gs.flipped:
win.blit(s, ((7-c)*sq_size, (7-r)*sq_size))
else:
win.blit(s, (c*sq_size, r*sq_size))
## If the player can move this piece, then highlight the squares to which it can move (validly).
s.fill(p.Color("orange"))
for m in valid_moves:
if (m.start_r == r) and (m.start_c == c):
if gs.flipped:
win.blit(s, ((7-m.end_c)*sq_size, (7-m.end_r)*sq_size))
else:
win.blit(s, (m.end_c*sq_size, m.end_r*sq_size))
def highlight_most_recent_move(win, sq_size: int, gs: GameState):
"""
Highlights (in yellow) both the starting- and ending-squares for the most-recent move.
This convenient feature for human players allows one to identify the opponent's most-recent move.
"""
## If this is the beginning of the game, then there will be no "most-recent move" made.
if len(gs.move_log) > 0:
m = gs.move_log[-1] ## Data about the move.
start_r = m.start_r if not gs.flipped else 7 - m.start_r
start_c = m.start_c if not gs.flipped else 7 - m.start_c
end_r = m.end_r if not gs.flipped else 7 - m.end_r
end_c = m.end_c if not gs.flipped else 7 - m.end_c
## Highlighting the relevant squares in yellow.
s = p.Surface((sq_size, sq_size))
s.set_alpha(ALPHA) ## Transparency value in (0, 255); 0 is transparent, 255 is opaque.
s.fill(p.Color("yellow"))
win.blit(s, (start_c*sq_size, start_r*sq_size))
win.blit(s, (end_c*sq_size, end_r*sq_size))
def animate(m: Move, win, sq_size: int, gs: GameState, clock):
"""
Animating a move: making pieces move more slowly/progressively than just disappearing and reappearing.
"""
end_r = m.end_r if not gs.flipped else 7 - m.end_r
end_c = m.end_c if not gs.flipped else 7 - m.end_c
start_r = m.start_r if not gs.flipped else 7 - m.start_r
start_c = m.start_c if not gs.flipped else 7 - m.start_c
dr = end_r - start_r
dc = end_c - start_c
frame_count = 20
for frame in range(frame_count+1):
progress_frac = frame / frame_count
gfx_r, gfx_c = (start_r + (dr*progress_frac), start_c + (dc*progress_frac))
draw_board(win=win, sq_size=sq_size, gs=gs)
draw_pieces(win=win, sq_size=sq_size, gs=gs) ## This already draws the piece at its end-square ...
## ... so we need to ERASE it by re-drawing the square back over it ...
color = COLORS[((end_r + end_c) % 2)]
end_square = p.Rect(end_c * sq_size, end_r * sq_size, sq_size, sq_size)
p.draw.rect(win, color, end_square)
## ... and we also need to draw back the captured piece (if any) ...
## ... unless it's an "en-passant" move, which would draw a "phantom pawn".
if m.piece_captured != "--":
if m.is_en_passant:
## White to move, capturing en-passant.
if m.piece_captured[0] == "b":
epr = end_r + 1
else:
epr = end_r - 1
end_square = p.Rect(end_c * sq_size, epr * sq_size, sq_size, sq_size)
win.blit(PIECES[m.piece_captured], end_square)
## Draw the moving piece to complete the animation protocol!
if m.piece_moved != "--":
win.blit(PIECES[m.piece_moved], p.Rect(gfx_c*sq_size, gfx_r*sq_size, sq_size, sq_size))
p.display.flip()
clock.tick(MAX_FPS)
def draw_mate_text(win, message: str, pct: float=0.9):
"""
Writes the appropriate message on screen given a "mate", i.e., at the END of the game.
NEW: Font SIZE and POSITION are now RELATIVE to the screen size and location, NOT ABSOLUTE.
"""
## Getting (and storing in relevant vars) the width and height of the window object in integer-pixels.
win_w, win_h = win.get_size()
## Binary-searching for the largest font-size that will still fit within "pct" of the window size.
## By "casting to int", we essentially implement the "floor function" (from mathematics).
target_text_width = int(win_w * pct)
lo, hi = 1, win_h ## Target font-size will never be taller than the square window-height.
while lo <= hi:
mid = (lo + hi) // 2 ## In Python, "x // y" is the same as "floor(x / y)" in mathematics.
test_this_font_size = p.font.SysFont(name="Palatino", size=mid, bold=True, italic=False)
test_text_width = test_this_font_size.size(message)[0]
if test_text_width <= target_text_width:
lo = mid + 1
else:
hi = mid - 1
font_size = hi ## Largest font-size that fits within "pct" of the total window size.
font = p.font.SysFont(name="Palatino", size=font_size, bold=True, italic=False)
text_object = font.render(message, True, p.Color((0, 100, 195))) ## Navy blue.
text_location = text_object.get_rect(
center=(win_w // 2, win_h // 2)
)
win.blit(text_object, text_location)
def pawn_promo_console_text(desired_promo_piece: str):
"""
Returns a string (to be PRINTED ON THE CONSOLE, not shown on the screen) reflecting the player's latest
choice for a pawn-promotion piece.
"""
string = f"I will promote all future pawns to a {desired_promo_piece} "
string += "unless a player chooses otherwise in the future."
return string
def board_flip_str(fl: bool) -> str:
if not fl:
return "Flipping the board's perspective now: White --> Black at the bottom!"
else:
return "Flipping the board's perspective now: Black --> White at the bottom!"
def draw_game_state(win, gs: GameState, dynamic_sq_size: int, vm_list: list[Move], square_selected: tuple):
"""
Performs all the graphics-operations involved in displaying the current GameState object.
"""
draw_board(win=win, sq_size=dynamic_sq_size, gs=gs)
highlight_possible_squares(win=win, sq_size=dynamic_sq_size,
gs=gs, valid_moves=vm_list, square_selected=square_selected)
highlight_most_recent_move(win=win, sq_size=dynamic_sq_size, gs=gs)
draw_pieces(win=win, gs=gs, sq_size=dynamic_sq_size)
#######################################################################################################################
## Main-driver function.
def main():
## The argument "RESIZABLE" allows one to resize the window via mouse click-drag.
window = p.display.set_mode((WIDTH, HEIGHT), p.constants.RESIZABLE)
clock = p.time.Clock()
window.fill(p.Color("white"))
gs = GameState() ## Initializing GameState --> starting a new game.
load_piece_images(sq_size=SQ_SIZE) ## This is COMPUTATIONALLY-EXPENSIVE; only do this if the window-size changes!
valid_moves = gs.get_all_valid_moves()
move_made = False ## Flag variable to determine when to call "get_all_valid_moves()" again.
animated = False ## Flag variable denoting that an amimation has not yet been produced.
print_mate_once = False ## Flag variable that makes sure the code does not continuous print out the game result.
clear_board_pending = False
resignation_pending = False ## Flag variable involved in resigning (i.e., voluntarily-losing) the game.
square_selected = () ## Keep track of user's most-recent click. Tuple: (row, col).
player_clicks = [] ## Keeps track of up-to TWO TUPLES (see above) denoting a player's piece's move.
running = True
while running:
dynamic_width, dynamic_height = window.get_size()
dynamic_sq_size = min(dynamic_width, dynamic_height) // DIMENSION
## Clearing the event queue by getting events of ALL TYPES.
for e in p.event.get():
if e.type == p.QUIT:
running = False
##################################################
## Handling WINDOW RESIZING.
elif e.type == p.VIDEORESIZE:
new_size = min(e.w, e.h)
window = p.display.set_mode((new_size, new_size), p.constants.RESIZABLE) ## Resize the window.
dynamic_sq_size = new_size // DIMENSION ## Recalculate the square size.
load_piece_images(sq_size=dynamic_sq_size) ## Reload piece images with new size.
##################################################
## Handling KEY PRESSES.
elif e.type == p.KEYDOWN:
## Any key OTHER than 'Y' cancels a resignation or board-reset.
if resignation_pending and e.key != p.K_y:
resignation_pending = False
print("Resignation cancelled.")
if clear_board_pending and e.key != p.K_y:
clear_board_pending = False
print("Board reset cancelled.")
## Any key OTHER than 'Y' DECLINES a draw-offer and resets the turn.
if gs.draw_offered and e.key not in (p.K_d, p.K_y):
gs.draw_offered = False
gs.draw_offered_by_white = None
gs.white_to_move = not gs.white_to_move
print("Draw offer declined.")
## RESIGNATION: press 'L' to initiate, then 'Y' to confirm:
if e.key == p.K_l:
if not (gs.checkmate or gs.stalemate or gs.resigned or gs.draw_agreed):
## First, cancel any pending draw-offer.
if gs.draw_offered:
gs.draw_offered = False
gs.draw_offered_by_white = None
gs.white_to_move = not gs.white_to_move
resignation_pending = True
clear_board_pending = False
who = "White" if gs.white_to_move else "Black"
resignation_string = f"{who} wants to resign. "
resignation_string += "Press 'Y' to confirm your resignation, "
resignation_string += "or any other key (or click the mouse) to cancel."
print(resignation_string)
## DRAW OFFER: press 'D' to INITIATE the draw-offer, then the turn switches.
## If the other player presses 'Y', then the draw is accepted. Any other key/click declines.
## If declined, the turn switches back and the game continues as before.
elif e.key == p.K_d:
if not (gs.checkmate or gs.stalemate or gs.resigned or gs.draw_agreed):
resignation_pending = False
clear_board_pending = False
if not gs.draw_offered:
## Offering a draw.
who = "White" if gs.white_to_move else "Black"
gs.draw_offered = True
gs.draw_offered_by_white = gs.white_to_move
## Switching turns.
gs.white_to_move = not gs.white_to_move
opponent = "White" if gs.white_to_move else "Black"
print_line_1 = f"{who} offers a draw. {opponent}: press 'Y' to accept; "
print_line_2 = "any other key (or click the mouse) to cancel."
print(print_line_1 + print_line_2)
## Pressing 'Y' after 'L' confirms the current player's resignation.
elif e.key == p.K_y:
if gs.draw_offered:
## User has pressed 'Y' to agree to a draw that has previously been offered.
gs.draw_agreed = True
gs.draw_offered = False
gs.draw_offered_by_white = None
gs.draw_message = "Draw! Nobody wins!"
print(f"\n{gs.draw_message}\n1/2 - 1/2\n")
elif resignation_pending:
resignation_pending = False
who = "White" if gs.white_to_move else "Black"
winner = "Black" if gs.white_to_move else "White"
result = "0 - 1" if gs.white_to_move else "1 - 0"
gs.resigned_message = f"{who} resigns. {winner} wins!"
gs.resigned = True
print(f"\n{gs.resigned_message}\n{result}\n")
draw_mate_text(win=window, message=gs.resigned_message)
print_mate_once = True
elif clear_board_pending:
clear_board_pending = False
print("Restarting the game now!\n")
gs = GameState()
valid_moves = gs.get_all_valid_moves()
move_made = False
animated = False
resignation_pending = False
print_mate_once = False
square_selected = ()
player_clicks = []
##############################################
## UNDO when 'Z' key is pressed.
if e.key == p.K_z:
gs.undo_move()
print("Undoing the most-recent move!")
move_made = True
animated = False
## Without the line below, if mate is achieved, and then undo, next time mate happens the ...
## ... computer won't print out the result of the game!
print_mate_once = False
## Resetting resignation parameters... We don't have to reset "resignation_pending"; see K_y.
gs.resigned = False
gs.resigned_message = ""
## Resetting draw parameters...
gs.draw_offered = False
gs.draw_offered_by_white = None
gs.draw_agreed = False
gs.draw_message = ""
## RESET THE BOARD when 'C' key is pressed ('C' for "Clear" the board and restart).
## Then press the 'Y' key to confirm (unless the game is over).
elif e.key == p.K_c:
if not (gs.checkmate or gs.stalemate or gs.resigned or gs.draw_agreed):
clear_board_pending = True
resignation_pending = False
print_string = "Are you sure you want to reset the board? "
print_string += "Press 'Y' to confirm, or any other key (or click the mouse) to cancel."
print(print_string)
else: ## The game is OVER, no need for confirmation, in this case!
print("Restarting the game now!\n")
gs = GameState()
valid_moves = gs.get_all_valid_moves()
move_made = False
animated = False
clear_board_pending = False
resignation_pending = False
print_mate_once = False
square_selected = ()
player_clicks = []
## FLIP THE PERSPECTIVE when 'F' key is pressed (white-to-black and vice versa).
elif e.key == p.K_f:
print(board_flip_str(fl=gs.flipped))
gs.flip()
##############################################
## Setting the character gs.desired_promo_piece to a Queen; a Rook; a Bishop; or a Knight.
elif e.key == p.K_RETURN: ## Press "RETURN" to promote to Queen (this is also the default option).
print(pawn_promo_console_text(desired_promo_piece="Queen"))
gs.desired_promo_piece = "Q"
elif e.key == p.K_r: ## Press "R" to promote to Rook.
print(pawn_promo_console_text(desired_promo_piece="Rook"))
gs.desired_promo_piece = "R"
elif e.key == p.K_b: ## Press "B" to promote to Bishop.
print(pawn_promo_console_text(desired_promo_piece="Bishop"))
gs.desired_promo_piece = "B"
elif e.key == p.K_n: ## Press "N" to promote to Knight.
print(pawn_promo_console_text(desired_promo_piece="Knight"))
gs.desired_promo_piece = "N"
##################################################
## Handling MOUSE CLICKS; click on a piece and then click on its destination to make a move.
## LATER: add "click-n-drag" functionality!
elif e.type == p.MOUSEBUTTONDOWN:
if gs.draw_offered:
gs.draw_offered = False
gs.draw_offered_by_white = None
gs.white_to_move = not gs.white_to_move
print("Draw offer declined.")
if resignation_pending:
## Do not resign if the player clicks the mouse after pressing "L" but before pressing "Y"!
## Instead, treat a mouse-click, just like a key-press (other than 'Y') as a cancellation!
resignation_pending = False
print("Resignation cancelled.")
if clear_board_pending:
clear_board_pending = False
print("Board reset cancelled.")
mouse_xy_loc = p.mouse.get_pos() ## (x, y) location of mouse click
## Adding in logic for selecting a given square on the board from a mouse-click.
if not gs.flipped:
col = mouse_xy_loc[0] // dynamic_sq_size
row = mouse_xy_loc[1] // dynamic_sq_size
else:
col = 7 - (mouse_xy_loc[0] // dynamic_sq_size)
row = 7 - (mouse_xy_loc[1] // dynamic_sq_size)
## If player clicks the same square twice: (a) de-select that piece and (b) reset the player clicks.
if square_selected == (row, col):
square_selected = ()
player_clicks = []
## Otherwise, this is a valid click.
else:
square_selected = (row, col)
player_clicks.append(square_selected) ## Append for BOTH 1st AND 2nd clicks!
## AFTER player has made 2nd click, make the indicated move!
if len(player_clicks) > 1:
move = Move(start_square=player_clicks[0], end_square=player_clicks[1], b=gs.board)
## Validate the move before allowing the player to make it.
## We use a FOR-LOOP instead of an IF-STATEMENT because, in the future, we'll add FLAGS to moves.
for j in range(len(valid_moves)):
if move == valid_moves[j]:
print(gs.get_move_text(m=valid_moves[j], vm_list=valid_moves))
gs.make_move(valid_moves[j])
move_made = True
animated = True
## Reset selection variables...
square_selected = ()
player_clicks = []
## The user may have clicked on a piece, but then decided to move a different piece.
if not move_made:
player_clicks = [square_selected]
## Once the player makes his/her move, get new valid moves and reset the flag-variable.
if move_made:
if animated:
animate(m=gs.move_log[-1], win=window, sq_size=dynamic_sq_size, gs=gs, clock=clock)
valid_moves = gs.get_all_valid_moves()
move_made = False
draw_game_state(win=window, gs=gs, dynamic_sq_size=dynamic_sq_size,
vm_list=valid_moves, square_selected=square_selected)
## Handling game-ending conditions...
if gs.resigned:
draw_mate_text(win=window, message=gs.resigned_message)
elif gs.draw_agreed:
draw_mate_text(win=window, message=gs.draw_message)
if not print_mate_once:
print_mate_once = True
elif gs.checkmate:
if gs.white_to_move:
draw_mate_text(win=window, message="Checkmate! Black wins!")
if not print_mate_once:
print("\n0 - 1\n")
print_mate_once = True
else:
draw_mate_text(win=window, message="Checkmate! White wins!")
if not print_mate_once:
print("\n1 - 0\n")
print_mate_once = True
elif gs.stalemate:
draw_mate_text(win=window, message="Stalemate! Nobody wins!")
if not print_mate_once:
print("\n1/2 - 1/2\n")
print_mate_once = True
clock.tick(MAX_FPS)
p.display.flip()
#######################################################################################################################
if __name__ == '__main__':
main()
## E.O.F.