-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
407 lines (343 loc) · 18.1 KB
/
board.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
'''
/* //////////////////////////////////////////////////////////////////////////////////
//
// Project : Chess 2D
// Description : 2D chess game in Python
// Programmer : Uldrix | https://github.com/Uldrix
// Licence : GNU
//
//
//
// ⠀⠀ ⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⣴⠞⠋⠁⣀⣠⣴⣶⣾⣿⣷⣦⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠆
// ⠀ ⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣴⠟⢻⣧⣤⣴⣿⠿⠋⠁⣴⡿⠿⢿⣿⣿⣿⣷⣶⣶⣶⣶⡶⠶⠚⠁⠀⠀⣠⣾⠏⠀
//⠀⠀⠀ ⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⣀⣠⣴⣾⣿⣥⣤⣤⣭⣍⣁⡀⠀⠀⠘⣿⣇⠀⠀⠀⠈⠉⠉⠉⠉⠀⠀⠀⠀⢀⣠⣴⣿⠟⠁⠀⠀
//⠀⠀⠀⠀⠀ ⠀ ⠀⠀⠀⢀⣤⡶⠟⠋⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠁⠀⠀⠘⢿⣷⣦⣄⣀⣀⣀⣀⣀⣤⣤⣶⣾⣿⡿⠛⠁⠀⠀⢀⡀
//⠀⠀ ⠀⠀ ⠀⠀⠀⣠⡾⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠙⠛⠛⠛⠛⠛⣩⣿⠿⠋⠁⠀⠀⣠⣶⠟⠋⠀
//⠀⠀ ⠀ ⠀⠀⢠⡾⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢄⠀⠀⠀⠀⣼⣿⠁⠀⠀⠀⢠⣾⡿⠃⠀⠀⠀
//⠀ ⠀⠀⠀⣠⡟⡡⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀⠀⠸⣿⣄⠀⢀⣴⣿⣿⠁⠀⠀⠀⠀
//⠀ ⠀⠀⢠⡟⡜⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⠀⠀⠀⠀⠀⢀⣤⠶⠒⠛⠛⠒⠲⢤⡀⠀⠘⣷⠀⠀⠀⠙⠻⠿⠿⢿⣿⠇⠀⠀⠀⠀⠀
//⠀ ⣠⡶⠿⢇⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠇⠀⢀⡴⢪⡿⠋⠀⠀⠀⠀⠀⠀⠀⠀⠙⢄⠀⢹⡄⠀⠀⠀⠀⠀⢀⣿⡟⠀⠀⠀⠀⠀⠀
//⠀ ⠙⢷⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⠿⣦⣤⡞⣰⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡄⠀⡇⠀⠀⠀⢀⣠⣾⠟⢠⠀⠀⠀⠀⠀⠀
//⠀ ⠀⠀⡟⣿⣆⢀⠀⠀⠀⠀⠀⣠⠾⠕⠉⢉⣉⠀⣿⡁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⠀⠃⠀⣠⣶⣿⠟⠋⢀⣾⠀⠀⠀⠀⠀⠀
//⠀⠀ ⠀⢰⣿⣿⣯⣧⠠⠮⠴⠞⣁⣠⣴⣾⣿⣿⣷⡌⣿⠋⠉⠉⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣾⡟⠉⠀⠀⣠⣾⡏⠀⠀⠀⠀⠀⠀
// ⠀⣠⠴⢾⣿⣿⣿⣿⠀⠳⣾⣿⣿⣿⣿⣿⣿⣿⣿⡇⠸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣿⣷⣤⣴⣾⣿⠟⠀⠀⠀⠀⠀⠀⠀
// ⢰⠁⠀⠀⠙⣿⣿⣧⠈⡆⢻⢿⣿⣿⣿⣿⣿⠿⠟⣃⣀⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠂⣼⠀⢈⣩⣽⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀
// ⣞⠀⠀⠀⣠⣿⣿⡿⠀⢺⡀⢑⡈⠉⠉⠉⠀⠀⠀⠀⠀⠀⠈⠻⣦⡞⠀⠀⠀⠀⠀⢠⣮⣾⣿⠿⠛⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⢻⣦⣄⣴⡿⡿⠷⣿⠠⣀⡨⠥⣞⣳⡄⠀⢀⡀⠀⠀⠀⠀⠀⣀⣿⣇⠀⠀⠀⣀⣴⣿⠟⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠉⢻⠇⠀⠀⠀⠈⠣⠀⠀⠀⠀⢀⡽⣷⣄⡈⠉⠉⠀⠀⣊⣽⣿⣿⣿⣿⣿⡿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⣾⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⠉⡴⣫⣿⣿⣒⡢⢄⣠⡾⠋⠉⠉⠛⠋⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⢸⠛⣷⣤⢀⡄⢀⠇⣠⠂⣸⣡⣚⣼⠋⠁⠀⠉⠉⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠘⣾⡀⡏⠙⡗⠻⠟⢻⠚⢻⢹⠙⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠈⠓⠧⠴⣇⣴⣄⢼⣤⠟⠚⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//
// In Tenebris we code :: Forged by Uldrix :: Be aware of the Matrix :: ~~~~~~~~~~~~
//
// ////////////////////////////////////////////////////////////////////////////////// */
'''
import pygame
from pieces import PiecePosition, PieceColor, ChessPiece
class Board:
def __init__(self, renderer, font):
self.renderer = renderer
self.font = font
self.board = [[PiecePosition() for _ in range(8)] for _ in range(8)]
self.pieces_texture = None
self.dragging_piece = None
self.drag_start_pos = None
self.drag_mouse_pos = None
self.move_made = False
self.selected_piece = None
self.selected_pos = None
self.move_stack = [] # Stack to keep track of moves for undoing
def initialize(self):
self.pieces_texture = pygame.image.load("assets/chessPieces.png").convert_alpha()
self._setup_board()
self.move_made = False
def _setup_board(self):
# Black pieces on the top rows
self.board[0] = [
PiecePosition(ChessPiece.ROOK, PieceColor.BLACK),
PiecePosition(ChessPiece.KNIGHT, PieceColor.BLACK),
PiecePosition(ChessPiece.BISHOP, PieceColor.BLACK),
PiecePosition(ChessPiece.QUEEN, PieceColor.BLACK),
PiecePosition(ChessPiece.KING, PieceColor.BLACK),
PiecePosition(ChessPiece.BISHOP, PieceColor.BLACK),
PiecePosition(ChessPiece.KNIGHT, PieceColor.BLACK),
PiecePosition(ChessPiece.ROOK, PieceColor.BLACK),
]
self.board[1] = [PiecePosition(ChessPiece.PAWN, PieceColor.BLACK) for _ in range(8)]
# Empty squares
for row in range(2, 6):
self.board[row] = [PiecePosition(ChessPiece.EMPTY, PieceColor.NONE) for _ in range(8)]
# White pieces on the bottom rows
self.board[7] = [
PiecePosition(ChessPiece.ROOK, PieceColor.WHITE),
PiecePosition(ChessPiece.KNIGHT, PieceColor.WHITE),
PiecePosition(ChessPiece.BISHOP, PieceColor.WHITE),
PiecePosition(ChessPiece.QUEEN, PieceColor.WHITE),
PiecePosition(ChessPiece.KING, PieceColor.WHITE),
PiecePosition(ChessPiece.BISHOP, PieceColor.WHITE),
PiecePosition(ChessPiece.KNIGHT, PieceColor.WHITE),
PiecePosition(ChessPiece.ROOK, PieceColor.WHITE),
]
self.board[6] = [PiecePosition(ChessPiece.PAWN, PieceColor.WHITE) for _ in range(8)]
def render(self):
square_size = self.renderer.get_height() // 8
colors = [(240, 217, 181), (181, 136, 99)]
# Render the board squares
for row in range(8):
for col in range(8):
color = colors[(row + col) % 2]
rect = pygame.Rect(col * square_size, row * square_size, square_size, square_size)
pygame.draw.rect(self.renderer, color, rect)
# Render pieces (skip rendering dragged piece in its original position)
piece = self.board[row][col]
if piece.piece != ChessPiece.EMPTY and (not self.dragging_piece or (row, col) != self.drag_start_pos):
self._render_piece(piece, col * square_size, row * square_size, square_size)
# Render dragged piece
if self.dragging_piece and self.drag_mouse_pos:
self._render_piece(self.dragging_piece, self.drag_mouse_pos[0] - square_size // 2,
self.drag_mouse_pos[1] - square_size // 2, square_size)
def _render_piece(self, piece, x, y, square_size):
piece_sprites = {
PieceColor.BLACK: {
ChessPiece.KING: (0, 0, 124, 290),
ChessPiece.QUEEN: (124, 0, 120, 290),
ChessPiece.BISHOP: (244, 0, 120, 290),
ChessPiece.ROOK: (360, 0, 120, 290),
ChessPiece.KNIGHT: (480, 0, 146, 290),
ChessPiece.PAWN: (624, 0, 86, 290),
},
PieceColor.WHITE: {
ChessPiece.KING: (0, 290, 124, 290),
ChessPiece.QUEEN: (124, 290, 120, 290),
ChessPiece.BISHOP: (244, 290, 120, 290),
ChessPiece.ROOK: (360, 290, 120, 290),
ChessPiece.KNIGHT: (480, 290, 146, 290),
ChessPiece.PAWN: (624, 290, 86, 290),
},
}
sprite = piece_sprites[piece.color].get(piece.piece)
if sprite:
src_rect = pygame.Rect(*sprite)
piece_width = 80
piece_height = 140
dest_rect = pygame.Rect(
x + (square_size - piece_width) // 2,
y + (square_size - piece_height) // 2,
piece_width,
piece_height,
)
self.renderer.blit(pygame.transform.scale(self.pieces_texture.subsurface(src_rect), (piece_width, piece_height)), dest_rect)
def handle_event(self, event, current_player):
square_size = self.renderer.get_height() // 8
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
col, row = event.pos[0] // square_size, event.pos[1] // square_size
if 0 <= row < 8 and 0 <= col < 8:
piece = self.get_piece_at(row, col)
if piece.color == current_player:
self.dragging_piece = piece
self.drag_start_pos = (row, col)
self.drag_mouse_pos = event.pos
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
if self.dragging_piece:
drop_col, drop_row = event.pos[0] // square_size, event.pos[1] // square_size
if 0 <= drop_row < 8 and 0 <= drop_col < 8:
if self.is_legal_move(self.drag_start_pos, (drop_row, drop_col), self.dragging_piece):
self.move_piece(self.drag_start_pos, (drop_row, drop_col))
# Else, move is illegal; piece remains in place
self.dragging_piece = None
self.drag_start_pos = None
self.drag_mouse_pos = None
elif event.type == pygame.MOUSEMOTION and self.dragging_piece:
self.drag_mouse_pos = event.pos
def is_legal_move(self, start, end, piece, ignore_check=False):
start_row, start_col = start
end_row, end_col = end
if not (0 <= end_row < 8 and 0 <= end_col < 8):
return False # Move is out of bounds
target_piece = self.get_piece_at(end_row, end_col)
if target_piece.color == piece.color:
return False # Cannot capture own piece
delta_row = end_row - start_row
delta_col = end_col - start_col
# Movement rules as before
if piece.piece == ChessPiece.PAWN:
if not self._validate_pawn_move(start, end, piece):
return False
elif piece.piece == ChessPiece.KNIGHT:
if not self._validate_knight_move(delta_row, delta_col):
return False
elif piece.piece == ChessPiece.BISHOP:
if not self._validate_bishop_move(start, end):
return False
elif piece.piece == ChessPiece.ROOK:
if not self._validate_rook_move(start, end):
return False
elif piece.piece == ChessPiece.QUEEN:
if not self._validate_queen_move(start, end):
return False
elif piece.piece == ChessPiece.KING:
if not self._validate_king_move(delta_row, delta_col):
return False
else:
return False # Unknown piece type
if ignore_check:
return True
# Simulate the move to check for self-check
original_end_piece = self.board[end_row][end_col]
self.board[end_row][end_col] = piece
self.board[start_row][start_col] = PiecePosition()
in_check = self.is_in_check(piece.color)
# Undo the move
self.board[start_row][start_col] = piece
self.board[end_row][end_col] = original_end_piece
return not in_check
def move_piece(self, start, end):
move = {'start': start, 'end': end}
self.make_move(move)
self.move_made = True # Indicate that a move was made
def get_piece_at(self, row, col):
if 0 <= row < 8 and 0 <= col < 8:
return self.board[row][col]
return PiecePosition()
def update(self):
pass # Placeholder for future updates
def is_king_captured(self, color):
for row in self.board:
for piece in row:
if piece.piece == ChessPiece.KING and piece.color == color:
return False
return True # King not found; captured
def _validate_pawn_move(self, start, end, piece):
start_row, start_col = start
end_row, end_col = end
direction = -1 if piece.color == PieceColor.WHITE else 1
starting_row = 6 if piece.color == PieceColor.WHITE else 1
delta_row = end_row - start_row
delta_col = end_col - start_col
target_piece = self.get_piece_at(end_row, end_col)
# Normal move forward
if delta_col == 0:
if delta_row == direction and target_piece.piece == ChessPiece.EMPTY:
return True
# Double move from starting position
if start_row == starting_row and delta_row == 2 * direction and self.get_piece_at(start_row + direction, start_col).piece == ChessPiece.EMPTY and target_piece.piece == ChessPiece.EMPTY:
return True
# Capture
elif abs(delta_col) == 1 and delta_row == direction and target_piece.piece != ChessPiece.EMPTY and target_piece.color != piece.color:
return True
return False
def _validate_knight_move(self, delta_row, delta_col):
return (abs(delta_row), abs(delta_col)) in [(2, 1), (1, 2)]
def _validate_bishop_move(self, start, end):
delta_row = end[0] - start[0]
delta_col = end[1] - start[1]
if abs(delta_row) != abs(delta_col):
return False
step_row = 1 if delta_row > 0 else -1
step_col = 1 if delta_col > 0 else -1
for i in range(1, abs(delta_row)):
if self.get_piece_at(start[0] + i * step_row, start[1] + i * step_col).piece != ChessPiece.EMPTY:
return False
return True
def _validate_rook_move(self, start, end):
if start[0] != end[0] and start[1] != end[1]:
return False
if start[0] == end[0]:
step = 1 if end[1] > start[1] else -1
for col in range(start[1] + step, end[1], step):
if self.get_piece_at(start[0], col).piece != ChessPiece.EMPTY:
return False
else:
step = 1 if end[0] > start[0] else -1
for row in range(start[0] + step, end[0], step):
if self.get_piece_at(row, start[1]).piece != ChessPiece.EMPTY:
return False
return True
def _validate_queen_move(self, start, end):
return self._validate_bishop_move(start, end) or self._validate_rook_move(start, end)
def _validate_king_move(self, delta_row, delta_col):
return max(abs(delta_row), abs(delta_col)) == 1
def is_in_check(self, color):
king_position = self.find_king(color)
if king_position is None:
return True # King is not on the board, considered as in check
opponent_color = PieceColor.BLACK if color == PieceColor.WHITE else PieceColor.WHITE
for row in range(8):
for col in range(8):
piece = self.get_piece_at(row, col)
if piece.color == opponent_color:
if self.is_attack_move((row, col), king_position, piece):
return True
return False
def find_king(self, color):
for row in range(8):
for col in range(8):
piece = self.get_piece_at(row, col)
if piece.piece == ChessPiece.KING and piece.color == color:
return (row, col)
return None
def is_attack_move(self, start, end, piece):
# Similar to is_legal_move but ignores checks and assumes it's the opponent's turn
return self.is_legal_move(start, end, piece, ignore_check=True)
def generate_all_legal_moves(self, color):
moves = []
for row in range(8):
for col in range(8):
piece = self.get_piece_at(row, col)
if piece.color == color:
legal_moves = self.get_legal_moves((row, col), piece)
for move in legal_moves:
moves.append({'start': (row, col), 'end': move})
return moves
def get_legal_moves(self, position, piece):
legal_moves = []
for row in range(8):
for col in range(8):
if self.is_legal_move(position, (row, col), piece):
legal_moves.append((row, col))
return legal_moves
def make_move(self, move):
start_row, start_col = move['start']
end_row, end_col = move['end']
piece = self.board[start_row][start_col]
captured_piece = self.board[end_row][end_col]
# Save the move to the move stack for undoing later
self.move_stack.append({
'move': move,
'piece_moved': piece,
'piece_captured': captured_piece,
})
self.board[end_row][end_col] = piece
self.board[start_row][start_col] = PiecePosition()
def undo_move(self, move):
last_move = self.move_stack.pop()
start_row, start_col = last_move['move']['start']
end_row, end_col = last_move['move']['end']
piece_moved = last_move['piece_moved']
piece_captured = last_move['piece_captured']
self.board[start_row][start_col] = piece_moved
self.board[end_row][end_col] = piece_captured
def is_game_over(self):
# Check if either king is captured
if self.is_king_captured(PieceColor.WHITE) or self.is_king_captured(PieceColor.BLACK):
return True
return False
def generate_all_legal_moves(self, color):
moves = []
for row in range(8):
for col in range(8):
piece = self.get_piece_at(row, col)
if piece.color == color:
legal_moves = self.get_legal_moves((row, col), piece)
for move in legal_moves:
moves.append({'start': (row, col), 'end': move})
return moves
def get_legal_moves(self, position, piece):
legal_moves = []
for row in range(8):
for col in range(8):
if self.is_legal_move(position, (row, col), piece):
legal_moves.append((row, col))
return legal_moves