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
| 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
move_type, row, col, row1, col1 = command.split()
row, col, row1, col1 = int(row), int(col), int(row1), int(col1)
if board.move_piece(row, col, row1, col1):
print('Ход успешен')
else:
print('Координаты некорректы! Попробуйте другой ход!')
def correct_coords(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):
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):
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 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
if piece.__class__.__name__ in {'Rook', 'King'}:
piece.move()
self.field[row][col] = None
self.field[row1][col1] = piece
self.color = opponent(self.color)
return True
def move_and_promote_pawn(self, row, col, row1, col1, char):
if self.field[row][col].char() != 'P':
return False
piece = self.field[row][col]
color = self.field[row][col].get_color()
if piece.can_move(self, row, col, row1, col1)\
or piece.can_attack(self, row, col, row1, col1):
self.move_piece(row, col, row1, col1)
if char == 'Q':
self.field[row1][col1] = Queen(color)
elif char == 'R':
self.field[row1][col1] = Rook(color)
elif char == 'B':
self.field[row1][col1] = Bishop(color)
elif char == 'N':
self.field[row1][col1] = Knight(color)
return True
return False
def castling0(self):
if self.color == WHITE:
if self.field[0][0] is None:
return False
if self.field[0][0].char() != 'R':
return False
if self.field[0][0].has_moved:
return False
if self.field[0][4] is None:
return False
if self.field[0][4].char() != 'K':
return False
if self.field[0][4].has_moved:
return False
if self.field[0][0].can_move(self, 0, 0, 0, 3):
if self.field[0][3] is not None:
return False
self.move_piece(0, 0, 0, 3)
self.color = opponent(self.color)
self.move_piece(0, 4, 0, 2)
return True
else:
if self.field[7][0] is None:
return False
if self.field[7][0].char() != 'R':
return False
if self.field[7][0].has_moved:
return False
if self.field[7][4] is None:
return False
if self.field[7][4].char() != 'K':
return False
if self.field[7][4].has_moved:
return False
if self.field[7][0].can_move(self, 7, 0, 7, 3):
if self.field[7][3] is not None:
return False
self.move_piece(7, 0, 7, 3)
self.color = opponent(self.color)
self.move_piece(7, 4, 7, 2)
return True
return False
def castling7(self):
if self.color == WHITE:
if self.field[0][7] is None:
return False
if self.field[0][7].char() != 'R':
return False
if self.field[0][7].has_moved:
return False
if self.field[0][4] is None:
return False
if self.field[0][4].char() != 'K':
return False
if self.field[0][4].has_moved:
return False
if self.field[0][7].can_move(self, 0, 7, 0, 5):
if self.field[0][5] is not None:
return False
self.move_piece(0, 7, 0, 5)
self.color = opponent(self.color)
self.move_piece(0, 4, 0, 6)
return True
else:
if self.field[7][7] is None:
return False
if self.field[7][7].char() != 'R':
return False
if self.field[7][7].has_moved:
return False
if self.field[7][4] is None:
return False
if self.field[7][4].char() != 'K':
return False
if self.field[7][4].has_moved:
return False
if self.field[7][7].can_move(self, 7, 7, 7, 5):
if self.field[7][5] is not None:
return False
self.move_piece(7, 7, 7, 5)
self.color = opponent(self.color)
self.move_piece(7, 4, 7, 6)
return True
return False
class Rook:
def __init__(self, color):
self.color = color
self.has_moved = False
def move(self):
self.has_moved = True
def get_color(self):
return self.color
def char(self):
return 'R'
def can_move(self, board, row, col, row1, col1):
color = board.get_piece(row, col).get_color()
if row != row1 and col != col1:
return False
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
if board.get_piece(row1, col1) is not None:
return board.get_piece(row1, col1).get_color() == opponent(color)
return True
def can_attack(self, board, row, col, row1, col1):
return self.can_move(board, row, col, row1, col1)
class Pawn:
def __init__(self, color):
self.color = color
def get_color(self):
return self.color
def char(self):
return 'P'
def can_move(self, board, row, col, row1, col1):
if col != col1:
return False
if self.color == WHITE:
direction = 1
start_row = 1
else:
direction = -1
start_row = 6
if row + direction == row1:
return board.get_piece(row1, col1) is None
if (row == start_row
and row + 2 * direction == row1
and board.field[row + direction][col] is None):
return board.get_piece(row1, col1) is None
return False
def can_attack(self, board, row, col, row1, col1):
direction = 1 if (self.color == WHITE) else -1
if (row + direction == row1
and (col + 1 == col1 or col - 1 == col1)):
return board.get_piece(row1, col1).get_color() == opponent(self.color)
class Knight:
def __init__(self, color):
self.color = color
def get_color(self):
return self.color
def char(self):
return 'N'
def can_move(self, board, row, col, row1, col1):
return True # Заглушка
def can_attack(self, board, row, col, row1, col1):
return self.can_move(board, row, col, row1, col1)
class King:
def __init__(self, color):
self.color = color
self.has_moved = False
def get_color(self):
return self.color
def move(self):
self.has_moved = True
def char(self):
return 'K'
def can_move(self, board, row, col, row1, col1):
return True # Заглушка
def can_attack(self, board, row, col, row1, col1):
return self.can_move(board, row, col, row1, col1)
class Queen:
def __init__(self, color):
self.color = color
def get_color(self):
return self.color
def char(self):
return 'Q'
def can_move(self, board, row, col, row1, col1):
color = board.get_piece(row, col).get_color()
if abs(row1 - row) == abs(col1 - col) and (row1 != row or col1 != col)\
or row1 == row and col1 != col or row1 != row and col1 == col:
row_step = row1 - row
if row_step != 0:
row_step //= abs(row_step)
col_step = col1 - col
if col_step != 0:
col_step //= abs(col_step)
if row_step != 0:
cur_col = col
for cur_row in range(row + row_step, row1, row_step):
cur_col += col_step
if board.get_piece(cur_row, cur_col) is not None:
return False
else:
cur_row = row
for cur_col in range(col + col_step, col1, col_step):
cur_row += row_step
if board.get_piece(cur_row, cur_col) is not None:
return False
if board.get_piece(row1, col1) is not None:
return board.get_piece(row1, col1).get_color() == opponent(color)
return True
return False
def can_attack(self, board, row, col, row1, col1):
return self.can_move(board, row, col, row1, col1)
class Bishop:
def __init__(self, color):
self.color = color
def get_color(self):
return self.color
def char(self):
return 'B'
def can_move(self, board, row, col, row1, col1):
return True # Заглушка
def can_attack(self, board, row, col, row1, col1):
return self.can_move(board, row, col, row1, col1)
if __name__ == "__main__":
main() |