-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessLib.py
411 lines (339 loc) · 13.9 KB
/
ChessLib.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
WHITE = 1
BLACK = 2
# Удобная функция для вычисления цвета противника
def opponent(color):
if color == WHITE:
return BLACK
else:
return WHITE
def print_board(board): # Распечатать доску в текстовом виде (см. скриншот)
print(' +----+----+----+----+----+----+----+----+')
for row in range(7, -1, -1):
print(' ', row, end=' ')
for col in range(8):
print('|', board.cell(row, col), end=' ')
print('|')
print(' +----+----+----+----+----+----+----+----+')
print(end=' ')
for col in range(8):
print(col, end=' ')
print()
def main():
# Создаём шахматную доску
board = Board()
# Цикл ввода команд игроков
while True:
# Выводим положение фигур на доске
print_board(board)
# Подсказка по командам
print('Команды:')
print(' exit -- выход')
print(' move <row> <col> <row1> <row1> -- ход из клетки (row, col)')
print(' в клетку (row1, col1)')
# Выводим приглашение игроку нужного цвета
if board.current_player_color() == WHITE:
print('Ход белых:')
else:
print('Ход чёрных:')
command = input()
if command == 'exit':
break
try:
move_type, row, col, row1, col1 = command.split()
row, col, row1, col1 = int(row), int(col), int(row1), int(col1)
except ValueError:
print('Неверные координаты хода.')
continue
if board.move_piece(row, col, row1, col1):
print('Ход успешен')
else:
print('Координаты некорректы! Попробуйте другой ход!')
def correct_coords(row, col):
'''Функция проверяет, что координаты (row, col) лежат
внутри доски'''
return 0 <= row < 8 and 0 <= col < 8
class Board:
def __init__(self):
self.color = WHITE
self.field = []
for row in range(8):
self.field.append([None] * 8)
self.field[0] = [
Rook(WHITE), Knight(WHITE), Bishop(WHITE), Queen(WHITE),
King(WHITE), Bishop(WHITE), Knight(WHITE), Rook(WHITE)
]
self.field[1] = [
Pawn(WHITE), Pawn(WHITE), Pawn(WHITE), Pawn(WHITE),
Pawn(WHITE), Pawn(WHITE), Pawn(WHITE), Pawn(WHITE)
]
self.field[6] = [
Pawn(BLACK), Pawn(BLACK), Pawn(BLACK), Pawn(BLACK),
Pawn(BLACK), Pawn(BLACK), Pawn(BLACK), Pawn(BLACK)
]
self.field[7] = [
Rook(BLACK), Knight(BLACK), Bishop(BLACK), Queen(BLACK),
King(BLACK), Bishop(BLACK), Knight(BLACK), Rook(BLACK)
]
def current_player_color(self):
return self.color
def cell(self, row, col):
'''Возвращает строку из двух символов. Если в клетке (row, col)
находится фигура, символы цвета и фигуры. Если клетка пуста,
то два пробела.'''
piece = self.field[row][col]
if piece is None:
return ' '
color = piece.get_color()
c = 'w' if color == WHITE else 'b'
return c + piece.char()
def get_piece(self, row, col):
if correct_coords(row, col):
return self.field[row][col]
else:
return None
def move_piece(self, row, col, row1, col1):
'''Переместить фигуру из точки (row, col) в точку (row1, col1).
Если перемещение возможно, метод выполнит его и вернёт True.
Если нет --- вернёт False'''
if not correct_coords(row, col) or not correct_coords(row1, col1):
return False
if row == row1 and col == col1:
return False # нельзя пойти в ту же клетку
piece = self.field[row][col]
if isinstance(piece, Pawn) and (row1 == 7 or row1 == 0):
return False
if piece is None:
return False
if piece.get_color() != self.color:
return False
if self.field[row1][col1] is None:
if not piece.can_move(self, row, col, row1, col1):
return False
elif self.field[row1][col1].get_color() == opponent(piece.get_color()):
if not piece.can_attack(self, row, col, row1, col1):
return False
else:
return False
self.field[row][col] = None # Снять фигуру.
self.field[row1][col1] = piece # Поставить на новое место.
self.color = opponent(self.color)
return True
def winner(self):
if all(p is None or isinstance(p, King) for row in self.field for p in row):
return 'Equal'
if all(not isinstance(p, King) or p.get_color() != WHITE for row in self.field for p in row):
return BLACK
if all(not isinstance(p, King) or p.get_color() != BLACK for row in self.field for p in row):
return WHITE
def move_and_promote_pawn(self, row, col, row1, col1, char):
if not correct_coords(row, col) or not correct_coords(row1, col1):
return False
if row == row1 and col == col1:
return False
pawn = self.field[row][col]
if pawn is None or not isinstance(pawn, Pawn):
return False
if (row1 != len(self.field) - 1 and pawn.get_color() == WHITE) \
or (row1 != 0 and pawn.get_color() == BLACK):
return False
if self.field[row1][col1] is None:
if not pawn.can_move(self, row, col, row1, col1):
return False
elif self.field[row1][col1].get_color() == opponent(pawn.get_color()):
if not pawn.can_attack(self, row, col, row1, col1):
return False
else:
return False
self.field[row][col] = None # Снять фигуру.
chars_to_figures = {'Q': Queen, 'R': Rook, 'B': Bishop, 'N': Knight}
if char not in chars_to_figures:
return False
self.field[row1][col1] = chars_to_figures[char](self.color)
self.color = opponent(self.color)
return True
def castle_check(self, king, rook):
if rook is None or king is None:
return False
if not (isinstance(rook, Rook) and isinstance(king, King)):
return False
if rook.moved or king.moved:
return False
if rook.get_color() != self.color or king.get_color() != self.color:
return False
return True
def castling0(self):
coords = ((0, 0), (0, 4)) if self.color == WHITE else ((7, 0), (7, 4))
rook = self.field[coords[0][0]][coords[0][1]]
king = self.field[coords[1][0]][coords[1][1]]
if not self.castle_check(king, rook):
return False
if not rook.can_move(self, coords[0][0], 0, coords[1][0], 4):
return False
self.field[coords[0][0]][coords[0][1]] = None
self.field[coords[1][0]][coords[1][1]] = None
self.field[coords[0][0]][2] = King(self.color)
self.field[coords[0][0]][2].moved = True
self.field[coords[0][0]][3] = Rook(self.color)
self.field[coords[0][0]][3].moved = True
self.color = opponent(self.color)
return True
def castling7(self):
coords = ((0, 7), (0, 4)) if self.color == WHITE else ((7, 7), (7, 4))
rook = self.field[coords[0][0]][coords[0][1]]
king = self.field[coords[1][0]][coords[1][1]]
if not self.castle_check(king, rook):
return False
if not rook.can_move(self, coords[0][0], 7, coords[1][0], 4):
return False
self.field[coords[0][0]][coords[0][1]] = None
self.field[coords[1][0]][coords[1][1]] = None
self.field[coords[0][0]][6] = King(self.color)
self.field[coords[0][0]][6].moved = True
self.field[coords[0][0]][5] = Rook(self.color)
self.field[coords[0][0]][5].moved = True
self.color = opponent(self.color)
return True
class Figure:
def __init__(self, color):
self.color = color
self.character = None
def char(self):
return self.character
def get_color(self):
return self.color
def can_move(self, board, row, col, row1, col1):
return correct_coords(row, col) and correct_coords(row1, col1)
def can_attack(self, board, row, col, row1, col1):
return self.can_move(board, row, col, row1, col1)
def get_image(self):
return f'Icons/{"Black" if self.color == BLACK else "White"}/{self.character.lower()}.png'
class Rook(Figure):
def __init__(self, color):
super().__init__(color)
self.character = 'R'
self.moved = False
def get_color(self):
return self.color
def can_move(self, board, row, col, row1, col1):
# Невозможно сделать ход в клетку, которая не лежит в том же ряду
# или столбце клеток.
if row != row1 and col != col1:
return False
step = 1 if (row1 >= row) else -1
for r in range(row + step, row1, step):
# Если на пути по вертикали есть фигура
if not (board.get_piece(r, col) is None):
return False
step = 1 if (col1 >= col) else -1
for c in range(col + step, col1, step):
# Если на пути по горизонтали есть фигура
if not (board.get_piece(row, c) is None):
return False
self.moved = True
return True
class Pawn(Figure):
def __init__(self, color):
super().__init__(color)
self.character = 'P'
def get_color(self):
return self.color
def can_move(self, board, row, col, row1, col1):
# Пешка может ходить только по вертикали
# "взятие на проходе" не реализовано
if col != col1:
return False
# Пешка может сделать из начального положения ход на 2 клетки
# вперёд, поэтому поместим индекс начального ряда в start_row.
if self.color == WHITE:
direction = 1
start_row = 1
else:
direction = -1
start_row = 6
# ход на 1 клетку
if row + direction == row1:
return True
# ход на 2 клетки из начального положения
if (row == start_row
and row + 2 * direction == row1
and board.field[row + direction][col] is None):
return True
return False
def can_attack(self, board, row, col, row1, col1):
direction = 1 if (self.color == WHITE) else -1
return (row + direction == row1
and (col + 1 == col1 or col - 1 == col1))
class Knight(Figure):
def __init__(self, color):
super().__init__(color)
self.character = 'N'
def get_color(self):
return self.color
def can_move(self, board, row, col, row1, col1):
return abs(col - col1) == 2 and abs(row - row1) == 1 \
or abs(col - col1) == 1 and abs(row - row1) == 2
class King(Figure):
def __init__(self, color):
super().__init__(color)
self.character = 'K'
self.moved = False
def get_color(self):
return self.color
def can_move(self, board, row, col, row1, col1):
if not super().can_move(board, row, col, row1, col1):
return False
if abs(row - row1) <= 1 and abs(col - col1) <= 1:
self.moved = True
return True
class Queen(Figure):
def __init__(self, color):
super().__init__(color)
self.character = 'Q'
def get_color(self):
return self.color
def can_move(self, board, row, col, row1, col1):
if not (abs(row - row1) == abs(col - col1) or row == row1 or col == col1):
return False
if row != row1:
normalized_row = -(row - row1) // abs(row - row1)
else:
normalized_row = 0
if col != col1:
normalized_col = -(col - col1) // abs(col - col1)
else:
normalized_col = 0
cur_row, cur_col = row, col
while True:
cur_row += normalized_row
cur_col += normalized_col
if cur_row == row1 and cur_col == col1:
return True
if board.field[cur_row][cur_col] is not None:
return False
class Bishop(Figure):
def __init__(self, color):
super().__init__(color)
self.character = 'B'
def get_color(self):
return self.color
def can_move(self, board, row, col, row1, col1):
if not (abs(row - row1) == abs(col - col1)):
return False
if row != row1:
normalized_row = -(row - row1) // abs(row - row1)
else:
normalized_row = 0
if col != col1:
normalized_col = -(col - col1) // abs(col - col1)
else:
normalized_col = 0
cur_row, cur_col = row, col
while True:
cur_row += normalized_row
cur_col += normalized_col
if cur_row == row1 and cur_col == col1:
return True
if board.field[cur_row][cur_col] is not None:
return False
if __name__ == "__main__":
main()