-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
670 lines (534 loc) · 24.3 KB
/
game.py
File metadata and controls
670 lines (534 loc) · 24.3 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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import pygame
# Initializing pygame window
pygame.init()
width , height = 480 , 480
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Chess")
white_cell = (255, 255, 255)
black_cell = (0, 89, 179)
cell_size = width // 8
select_cell_color = (255 , 0, 0)
valid_spot_color = (79 ,121, 66)
grid_color = (150, 150 ,150)
black = (0, 0, 0)
check_color = (255, 0 ,0)
class ChessPiece:
def __init__(self,color,position):
self.color = color
self.position = position
def move(self, position , board):
new_row , new_col = position
old_row , old_col = self.position
# Moving character from current piece to new pos
board[old_row][old_col] = 0
board[new_row][new_col] = self
self.position = new_row , new_col
# Castling checks
if isinstance(self, (King, Rook)) and not self.has_moved:
self.has_moved = True
class Bishop(ChessPiece):
def valid_moves(self, board):
valid_moves = []
curr_row , curr_col = self.position
rows = cols = len(board)
# Moving directions of Bishop
offsets = [
(-1, -1), # Up-Left
(-1, 1), # Up-Right
(1, -1), # Down-Left
(1, 1) # Down-Right
]
for offset in offsets:
offset_row , offset_col = offset
final_row , final_col = curr_row + offset_row , curr_col + offset_col
while 0 <= final_row < rows and 0 <= final_col < cols:
dest_piece = board[final_row][final_col]
if not dest_piece:
valid_moves.append((final_row,final_col))
# Opponent in square then can be captured and not move further beyond
elif dest_piece.color != self.color:
valid_moves.append((final_row,final_col))
break
else:
break
final_row += offset_row
final_col += offset_col
return valid_moves
class Knight(ChessPiece):
def valid_moves(self,board):
valid_moves = []
curr_row , curr_col = self.position
rows = cols = len(board)
# Define all possible offsets for knights movements
offsets = [
(-2, -1), (-2, 1),(-1, -2), (-1, 2),
(1, -2), (1, 2),(2, -1), (2, 1)
]
for offset in offsets:
offset_row , offset_col = offset
final_row , final_col = curr_row + offset_row , curr_col + offset_col
if 0 <= final_row < rows and 0 <= final_col < cols:
dest_piece = board[final_row][final_col]
# Checking if square is empty or can be captured (opponent)
if not dest_piece or dest_piece.color != self.color:
valid_moves.append((final_row,final_col))
return valid_moves
class Rook(ChessPiece):
def __init__(self,color,position):
super().__init__(color,position)
self.has_moved = False
def valid_moves(self,board):
valid_moves = []
curr_row , curr_col = self.position
rows = cols = len(board)
# 8 Moving directions of Rook
offsets = [
(-1, 0), # Up
(1, 0), # Down
(0, -1), # Left
(0, 1), # Right
]
for offset in offsets:
offset_row , offset_col = offset
final_row , final_col = curr_row + offset_row , curr_col + offset_col
while 0 <= final_row < rows and 0 <= final_col < cols:
dest_piece = board[final_row][final_col]
if not dest_piece:
valid_moves.append((final_row,final_col))
# Opponent in square then can be captured and not move further beyond
elif dest_piece.color != self.color:
valid_moves.append((final_row,final_col))
break
else:
break
final_row += offset_row
final_col += offset_col
return valid_moves
class Queen(ChessPiece):
def valid_moves(self,board):
valid_moves = []
curr_row , curr_col = self.position
rows = cols = len(board)
# 8 Moving directions of Queen
offsets = [
(-1, 0), # Up
(1, 0), # Down
(0, -1), # Left
(0, 1), # Right
(-1, -1), # Up-Left
(-1, 1), # Up-Right
(1, -1), # Down-Left
(1, 1) # Down-Right
]
for offset in offsets:
offset_row , offset_col = offset
final_row , final_col = curr_row + offset_row , curr_col + offset_col
while 0 <= final_row < rows and 0 <= final_col < cols:
dest_piece = board[final_row][final_col]
if not dest_piece:
valid_moves.append((final_row,final_col))
# Opponent in square then can be captured and not move further beyond
elif dest_piece.color != self.color:
valid_moves.append((final_row,final_col))
break
else:
break
final_row += offset_row
final_col += offset_col
return valid_moves
class King(ChessPiece):
def __init__(self,color,position):
super().__init__(color,position)
self.has_moved = False
def valid_moves(self,board):
valid_moves = []
curr_row , curr_col = self.position
rows = cols = len(board)
offsets = [
(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)
]
for offset_row , offset_col in offsets:
new_row = curr_row + offset_row
new_col = curr_col + offset_col
if 0 <= new_row < rows and 0 <= new_col < cols:
piece = board[new_row][new_col]
if not piece or piece.color != self.color:
# Temporarily make the move and check for check
temp = board[new_row][new_col]
board[new_row][new_col] = self
board[curr_row][curr_col] = 0
if not game.is_check(board,self.color):
valid_moves.append((new_row,new_col))
# Undo the move
board[new_row][new_col] = temp
board[curr_row][curr_col] = self
# Castling possibility
if not self.has_moved:
curr_row , curr_col = self.position
left_castle , right_castle = self.check_castling(board)
if left_castle and isinstance(board[curr_row][curr_col - 4],Rook)\
and not board[curr_row][curr_col -4].has_moved:
valid_moves.append((curr_row , curr_col -2))
if right_castle and isinstance(board[curr_row][curr_col +3],Rook)\
and not board[curr_row][curr_col +3].has_moved:
valid_moves.append((curr_row, curr_col + 2))
return valid_moves
def check_castling(self,board):
curr_row , curr_col = self.position
left_castle = right_castle = False
if not self.has_moved:
if not board[curr_row][curr_col-1] and not board[curr_row][curr_col-2] and not board[curr_row][curr_col -3]:
left_castle = True
if not board[curr_row][curr_col + 1] and not board[curr_row][curr_col +2]:
right_castle = True
if left_castle:
offsets = [ (0 ,-1),(0, -2)]
for row_offset , col_offset in offsets:
new_row = curr_row + row_offset
new_col = curr_col + col_offset
board[new_row][new_col] = self
board[curr_row][curr_col] = 0
if game.is_check(board,self.color):
board[new_row][new_col] = 0
board[curr_row][curr_col] = self
left_castle = False
board[new_row][new_col] = 0
board[curr_row][curr_col] = self
if right_castle:
offsets = [ (0 , 1),(0, 2)]
for row_offset , col_offset in offsets:
new_row = curr_row + row_offset
new_col = curr_col + col_offset
board[new_row][new_col] = self
board[curr_row][curr_col] = 0
if game.is_check(board,self.color):
board[new_row][new_col] = 0
board[curr_row][curr_col] = self
right_castle = False
board[new_row][new_col] = 0
board[curr_row][curr_col] = self
# Returns a tuple of bool whether left or right castle
return (left_castle, right_castle)
def perform_castle(self,position,board):
curr_row , curr_col = self.position
final_row , final_col = position
# Left Castle
if curr_col - final_col > 0:
board[curr_row][curr_col] = 0
board[final_row][final_col] = self
board[curr_row][curr_col-4].move((final_row,final_col+1),board)
self.position = (final_row,final_col)
self.has_moved = True
# Right Castle
if curr_col - final_col < 0:
board[curr_row][curr_col] = 0
board[final_row][final_col] = self
board[curr_row][curr_col +3].move((final_row,final_col-1),board)
self.position = (final_row,final_col)
self.has_moved = True
def move(self,position,board):
if abs(position[1] - self.position[1]) == 2:
self.perform_castle(position,board)
return
new_row , new_col = position
old_row , old_col = self.position
board[old_row][old_col] = 0
board[new_row][new_col] = self
self.position = new_row , new_col
self.has_moved = True
class Pawn(ChessPiece):
def __init__(self,color,position):
super().__init__(color,position)
self.en_passant_target = False
def valid_moves(self,board):
valid_moves = []
curr_row , curr_col = self.position
if self.color == "white":
direction = -1
start_row = 6
else:
direction = 1
start_row = 1
final_row = curr_row + direction
rows = cols = len(board)
if 0 <= final_row < rows and not board[final_row][curr_col]:
valid_moves.append((final_row,curr_col))
if curr_row == start_row:
final_row = curr_row + direction*2
if not board[final_row][curr_col]:
valid_moves.append((final_row,curr_col))
# Checking if pawn can capture
capture_offset = [(direction,-1),(direction,1)]
for row_offset , col_offset in capture_offset:
final_row , final_col = curr_row + row_offset , curr_col + col_offset
if 0 <= final_row < rows and 0 <= final_col < cols:
dest_piece = board[final_row][final_col]
en_passant = board[curr_row][final_col]
if dest_piece and dest_piece.color != self.color:
valid_moves.append((final_row,final_col))
if en_passant and en_passant.color != self.color and isinstance(en_passant, Pawn) and en_passant.en_passant_target and not game.en_passant_check and game.en_passant_check == self.color:
valid_moves.append((final_row,final_col))
return valid_moves
def perform_promotion(self,position,board):
target_row , target_col = position
curr_row , curr_col = self.position
board[target_row][target_col] = Queen(self.color,position)
board[curr_row][curr_col] = 0
def move(self,position,board):
if position[0] == 0 or position[0] == 7:
self.perform_promotion(position,board)
return
new_row , new_col = position
old_row , old_col = self.position
# En passant move
# sketchy code but pawn moves to empty square diagonally
if abs(old_col - new_col) == 1:
if isinstance(board[old_row][old_col - (old_col - new_col)],Pawn):
board[old_row][old_col - (old_col - new_col)] = 0
# Moving character from current piece to new pos
board[old_row][old_col] = 0
board[new_row][new_col] = self
self.position = new_row , new_col
if abs(old_row - new_row) == 2 and game.en_passant_check != self.color:
self.en_passant_target = True
game.en_passant_check = self.color
else:
self.en_passant_target = False
piece_fen = {"r":Rook,"n":Knight,"b":Bishop,"q":Queen,"k":King,"p":Pawn
}
pieces = {
(King,'white') : pygame.image.load("Assets/w_king.jpg"),
(Queen,'white') : pygame.image.load("Assets/w_queen.jpg"),
(Bishop,'white'): pygame.image.load("Assets/w_bishop.jpg"),
(Knight,'white'): pygame.image.load("Assets/w_knight.jpg"),
(Rook,'white') : pygame.image.load("Assets/w_rook.jpg"),
(Pawn,'white') : pygame.image.load("Assets/w_pawn.jpg"),
(King,'black') : pygame.image.load("Assets/b_king.jpg"),
(Queen,'black') : pygame.image.load("Assets/b_queen.jpg"),
(Bishop,'black'): pygame.image.load("Assets/b_bishop.jpg"),
(Knight,'black'): pygame.image.load("Assets/b_knight.jpg"),
(Rook,'black') : pygame.image.load("Assets/b_rook.jpg"),
(Pawn,'black') : pygame.image.load("Assets/b_pawn.jpg"),
}
class Game:
def __init__(self,screen):
self.screen = screen
self.board = [[0]* 8 for _ in range(8)]
self.current_player = "white"
self.selected_piece = None
self.has_check = None
self.has_mate = None
self.en_passant_check = None
def load_board(self,fen = "DEFAULT"):
if fen == "DEFAULT":
fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w" # Starting fen
# Splitting fen into board state and active player
fen = fen.split()
board_state = fen[0]
self.current_player = "white" if fen[1] == "w" else "black"
rows = board_state.split("/")
for row , fen_row in enumerate(rows):
col = 0
for char in fen_row:
if char.isdigit():
col += int(char)
else:
if char.isupper():
self.board[row][col] = piece_fen[char.lower()]('white',(row,col))
else:
self.board[row][col] = piece_fen[char]('black',(row,col))
if isinstance(self.board[row][col], King):
if (row,col) not in ((0,4),(7,4)):
self.board[row][col].has_moved = True
col += 1
def handle_click(self,pos):
row = pos[1] // cell_size
col = pos[0] // cell_size
cell = self.board[row][col]
# Selecting an unselected square
if not self.selected_piece:
if cell:
if cell.color != self.current_player:
return
self.selected_piece = cell
return
else:
self.selected_piece = None
# Square is already selected
if self.selected_piece:
if (row, col) in self.selected_piece.valid_moves(self.board):
pos = self.selected_piece.position
temp = self.board[row][col]
self.selected_piece.move((row,col),self.board)
# If check after move ... illegal move
if self.is_check(self.board,self.current_player):
self.board[pos[0]][pos[1]] = self.selected_piece
self.board[row][col] = temp
self.selected_piece.position = (pos)
self.selected_piece = None
return
# Changing the en passant target
if self.en_passant_check == self.selected_piece.color:
self.en_passant_check = None
self.current_player = "white" if self.current_player != "white" else "black"
# Checkmate condition
if self.check_mate(self.board,self.current_player):
self.has_mate = self.current_player
# King in check
if self.is_check(self.board,self.current_player):
self.has_check = self.current_player
else:
self.has_check = None
self.selected_piece = None
def is_check(self,board,color):
king_pos = None
# Finding king position
for row in range(8):
for col in range(8):
piece = board[row][col]
if isinstance(piece, King) and piece.color == color:
king_pos = (row , col)
break
# Checking for attac
for row in range(8):
for col in range(8):
piece = board[row][col]
if piece and piece.color != color and not isinstance(piece , King):
valid_moves = piece.valid_moves(board)
if king_pos in valid_moves:
return True
for row in range(8):
for col in range(8):
piece = board[row][col]
if piece and piece.color != color and isinstance(piece, King):
curr_row , curr_col = piece.position
king_offsets = [
(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)
]
for offset_row , offset_col in king_offsets:
new_row = curr_row + offset_row
new_col = curr_col + offset_col
if 0 <= new_row < 8 and 0 <= new_col < 8:
valid_moves.append((new_row,new_col))
# Minor optimization
if king_pos in valid_moves:
return True
return False
def check_mate(self,board,color):
# Check if player's king is in Check
if self.is_check(board,color):
# Check if any piece can make a valid move to get our of check
for row in range(8):
for col in range(8):
piece = board[row][col]
if piece and piece.color == color:
valid_moves = piece.valid_moves(board)
for move in valid_moves:
target_row , target_col = move
# Try making a move and see if still in check
temp = board[target_row][target_col]
board[target_row][target_col] = piece
board[row][col] = 0
# If not check , king can escape check
if not self.is_check(board,color):
board[row][col] = piece
board[target_row][target_col] = temp
return False
# Undo the move
board[row][col] = piece
board[target_row][target_col] = temp
# If no valid move can get king out of check , it's checkmate
return True
# King not in check
return False
def display(self):
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Quitting the game
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# Selecting a tile
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = pygame.mouse.get_pos()
self.handle_click(pos)
# Coloring all the tiles
for row in range(8):
for col in range(8):
color = white_cell if (row + col) % 2 == 0 else black_cell
pygame.draw.rect(screen,color,(col * cell_size, row * cell_size, cell_size, cell_size))
# Selected piece
if self.selected_piece:
select_row,select_col = self.selected_piece.position
pygame.draw.rect(screen,select_cell_color,(select_col * cell_size, select_row * cell_size, cell_size, cell_size))
valid_moves = self.selected_piece.valid_moves(self.board)
for x , y in valid_moves:
pygame.draw.rect(screen,valid_spot_color,(y * cell_size, x * cell_size, cell_size, cell_size))
# Rank and File Indexing
label_font = pygame.font.Font(None ,22)
rank_labels = ['8', '7', '6', '5', '4', '3', '2', '1']
file_labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
for i in range(8):
color = white_cell if i%2 != 0 else black_cell
rank_text = label_font.render(rank_labels[i],True, color)
rank_rect = rank_text.get_rect(x = 5 , y = i * cell_size + 5)
screen.blit(rank_text,rank_rect)
color = white_cell if i%2 == 0 else black_cell
file_text = label_font.render(file_labels[i],True, color)
file_rect = rank_text.get_rect(x = i * cell_size + cell_size - 17 , y = height - 15)
screen.blit(file_text,file_rect)
# Loading all the pieces into the game
for row in range(8):
for col in range(8):
piece = self.board[row][col]
if piece:
screen.blit(pieces[type(piece),piece.color], (col*cell_size , row * cell_size))
# Grid lines
for x in range(0, width, cell_size):
pygame.draw.line(screen, grid_color, (x, 0), (x, height))
for y in range(0, height, cell_size):
pygame.draw.line(screen, grid_color, (0, y), (width, y))
# Updating the board
pygame.display.flip()
# Checkmate message
if self.has_mate:
while running:
# Quitting
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if event.type == pygame.QUIT:
running = False
message = pygame.font.Font(None, 40).render("CHECK MATE" , True , check_color)
message_rect = message.get_rect(center = (width//2,height//2))
screen.blit(message,message_rect)
pygame.display.flip()
pygame.quit()
exit(1)
if self.has_check:
message = pygame.font.Font(None, 34).render("Check !!", True, check_color)
message_rect = message.get_rect(center=(width//2, height//2))
screen.blit(message, message_rect)
pygame.display.flip()
pygame.time.wait(2000)
self.has_check = None
pygame.quit()
exit(1)
test_fen1 = "8/Np1P1PP1/1P2Q2r/1r5p/1Pp1n3/5k2/5B2/3K4 w"
test_fen2 = "b2n2N1/qp1QnRp1/2p5/8/4k2N/1p2P3/6P1/1K6 b"
test_fen3 = "8/3p1P2/2npP2R/P3k1p1/P5p1/5rp1/K4Nb1/8 b"
test_mate = "r2qk2r/pb4pp/1n2Pb2/2B2Q2/p1p5/2P5/2B2PPP/RN2R1K1 w"
mate_in_1 = "k7/ppp5/8/3q4/1P3RK1/7r/P4Q2/8 b"
final_test = "2nb2k1/3P4/p2p1Ppp/3P2p1/3QR3/1P6/3K2P1/8 w - - 0 1"
game = Game(screen)
game.load_board(test_fen1)
game.display()