-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpieces.py
319 lines (265 loc) · 13.2 KB
/
pieces.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
from screen import *
import abc
import os
WHITE_PIECES_PATH = os.path.join(PICTURES_PATH, 'white_pieces')
BLACK_PIECES_PATH = os.path.join(PICTURES_PATH, 'black_pieces')
class Piece(metaclass=abc.ABCMeta):
BASIC_SCORE = 0
SCORE_EVOLUTION_TABLE = None
def __init__(self, square, team):
self.image = self.WHITE_IMAGE if team.is_white_team else self.BLACK_IMAGE
# resize the image
self.image = pygame.transform.scale(self.image, (int(Square.SIDE * 2/3), int(Square.SIDE * 2/3)))
self.square = square
self.square.current_piece = self
self.team = team
self.is_eaten = False
self.save_location = None
self.starting_square = square
self.move_counter = 0
if team.is_white_team:
self.SCORE_EVOLUTION_TABLE = self.SCORE_EVOLUTION_TABLE[::-1]
def _is_already_moved(self):
return self.square is self.starting_square
def color_next_step(self):
valid_square = self.get_valid_move_squares()
for square in valid_square:
square.coloring_square_by_original_color()
def move(self, next_square: Square):
# Free current square.
self.square.current_piece = None
# Check if next square is taken by other team.
if next_square.current_piece is not None:
eaten_piece = next_square.current_piece
eaten_piece.is_eaten = True
# Move to next square.
self.square = next_square
self.square.current_piece = self
@abc.abstractmethod
def get_valid_move_squares(self):
pass
def draw(self):
if not self.is_eaten:
screen.blit(self.image, (self.square.x_mid - self.image.get_width()/2,
self.square.y_mid - self.image.get_height()/2))
@property
def score(self):
return self.BASIC_SCORE + self.SCORE_EVOLUTION_TABLE[self.square.line_cord][self.square.tur_cord]
def __str__(self):
team = 'white' if self.team.is_white_team else 'black'
state = 'alive' if not self.is_eaten else 'eaten'
type_of_piece = type(self).__name__
return f'{type_of_piece}, position: {self.square}, ' \
f'{state}, {team} team'
class King(Piece):
BASIC_SCORE = 2000
WHITE_IMAGE = pygame.image.load(os.path.join(WHITE_PIECES_PATH, 'white_king.png'))
BLACK_IMAGE = pygame.image.load(os.path.join(BLACK_PIECES_PATH, 'black_king.png'))
SCORE_EVOLUTION_TABLE = [[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-20, -30, -30, -40, -40, -30, -30, -20],
[-10, -20, -20, -20, -20, -20, -20, -10],
[20, 20, 0, 0, 0, 0, 20, 20],
[20, 30, 10, 0, 0, 10, 30, 20]]
def __init__(self, team: Team):
square = squares[0][3] if team.is_white_team else squares[7][3]
super().__init__(square, team)
def get_valid_move_squares(self):
line = self.square.line_cord
valid_squares = []
for line in range(line - 1, line + 2):
tur = self.square.tur_cord
for tur in range(tur - 1, tur + 2):
if is_move_to_square_valid(tur, line, self.team):
valid_squares.append(squares[line][tur])
return valid_squares
class Pawn(Piece):
BASIC_SCORE = 100
LINE_OF_WHITE_PAWNS = 1
LINE_OF_BLACK_PAWNS = 6
WHITE_IMAGE = pygame.image.load(os.path.join(WHITE_PIECES_PATH, 'white_pawn.png'))
BLACK_IMAGE = pygame.image.load(os.path.join(BLACK_PIECES_PATH, 'black_pawn.png'))
SCORE_EVOLUTION_TABLE = [[0, 0, 0, 0, 0, 0, 0, 0],
[50, 50, 50, 50, 50, 50, 50, 50],
[10, 10, 20, 30, 30, 20, 10, 10],
[5, 5, 10, 25, 25, 10, 5, 5],
[0, 0, 0, 20, 20, 0, 0, 0],
[5, -5, -10, 0, 0, -10, -5, 5],
[5, 10, 10, -20, -20, 10, 10, 5],
[0, 0, 0, 0, 0, 0, 0, 0]]
def __init__(self, team: Team, tur=None, square=None):
if square is None:
square = squares[Pawn.LINE_OF_WHITE_PAWNS][tur] if team.is_white_team else squares[Pawn.LINE_OF_BLACK_PAWNS][tur]
super().__init__(square, team)
def is_reached_to_end(self):
end = NUMBER_OF_SQUARES - 1 if self.team.is_white_team else 0
return self.square.line_cord == end
def get_valid_move_squares(self):
line = self.square.line_cord
tur = self.square.tur_cord
valid_moves = []
# Direction represent one square walk.
direction = 1 if self.team.is_white_team else -1
line += direction
# Check if nest step is out of board.
if line > 7 or line < 0:
return valid_moves
next_square = squares[line][tur]
valid_moves.extend(self._diagonal_eat(next_square))
if next_square.current_piece is not None:
return valid_moves
valid_moves.append(next_square)
if self.move_counter == 0:
line += direction
next_square = squares[line][tur]
if next_square.current_piece is None:
valid_moves.append(next_square)
return valid_moves
def _diagonal_eat(self, next_square):
valid_eat_moves = []
# Left check.
line = next_square.line_cord
tur = next_square.tur_cord - 1
if is_move_to_square_valid(tur, line, self.team):
current_square = squares[line][tur]
if current_square.current_piece is not None:
valid_eat_moves.append(current_square)
# Right check.
tur += 2
if is_move_to_square_valid(tur, line, self.team):
current_square = squares[line][tur]
if current_square.current_piece is not None:
valid_eat_moves.append(current_square)
return valid_eat_moves
class Knight(Piece):
BASIC_SCORE = 320
BLACK_IMAGE = pygame.image.load(os.path.join(BLACK_PIECES_PATH, 'black_knight.png'))
WHITE_IMAGE = pygame.image.load(os.path.join(WHITE_PIECES_PATH, 'white_knight.png'))
SCORE_EVOLUTION_TABLE = [[-50, -40, -30, -30, -30, -30, -40, -50],
[-40, -20, 0, 0, 0, 0, -20, -40],
[-30, 0, 10, 15, 15, 10, 0, -30],
[-30, 5, 15, 20, 20, 15, 5, -30],
[-30, 0, 15, 20, 20, 15, 0, -30],
[-30, 5, 10, 15, 15, 10, 5, -30],
[-40, -20, 0, 5, 5, 0, -20, -40],
[-50, -40, -30, -30, -30, -30, -40, -50]]
def get_valid_move_squares(self):
valid_moves = []
self_line = self.square.line_cord
self_tur = self.square.tur_cord
team = self.team
knight_moves = [(1, 2), (1, -2), (2, -1), (2, 1), (-2, -1), (-1, -2), (-2, 1), (-1, 2)]
for tur_move, line_move in knight_moves:
if is_move_to_square_valid(self_tur + tur_move, self_line + line_move, team):
valid_moves.append(squares[self_line+line_move][self_tur+tur_move])
return valid_moves
class Rook(Piece):
BASIC_SCORE = 500
WHITE_IMAGE = pygame.image.load(os.path.join(WHITE_PIECES_PATH, 'white_rook.png'))
BLACK_IMAGE = pygame.image.load(os.path.join(BLACK_PIECES_PATH, 'black_rook.png'))
SCORE_EVOLUTION_TABLE = [[0, 0, 0, 0, 0, 0, 0, 0],
[5, 10, 10, 10, 10, 10, 10, 5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[0, 0, 0, 5, 5, 0, 0, 0]]
def get_valid_move_squares(self):
return _get_valid_straight_move_squares(self)
class Bishop(Piece):
BASIC_SCORE = 330
WHITE_IMAGE = pygame.image.load(os.path.join(WHITE_PIECES_PATH, 'white_bishop.png'))
BLACK_IMAGE = pygame.image.load(os.path.join(BLACK_PIECES_PATH, 'black_bishop.png'))
SCORE_EVOLUTION_TABLE = [[-20,-10,-10,-10,-10,-10,-10,-20],
[-10, 0, 0, 0, 0, 0, 0,-10],
[-10, 0, 5, 10, 10, 5, 0,-10],
[-10, 5, 5, 10, 10, 5, 5,-10],
[-10, 0, 10, 10, 10, 10, 0,-10],
[-10, 10, 10, 10, 10, 10, 10,-10],
[-10, 5, 0, 0, 0, 0, 5,-10],
[-20,-10,-10,-10,-10,-10,-10,-20]]
def get_valid_move_squares(self):
return _get_diagonal_valid_moves(self)
class Queen(Piece):
BASIC_SCORE = 900
BLACK_IMAGE = pygame.image.load(os.path.join(BLACK_PIECES_PATH, 'black_queen.png'))
WHITE_IMAGE = pygame.image.load(os.path.join(WHITE_PIECES_PATH, 'white_queen.png'))
SCORE_EVOLUTION_TABLE = [[-20, -10, -10, -5, -5, -10, -10, -20],
[-10, 0, 0, 0, 0, 0, 0, -10],
[-10, 0, 5, 5, 5, 5, 0, -10],
[ -5, 0, 5, 5, 5, 5, 0, -5],
[ 0, 0, 5, 5, 5, 5, 0, -5],
[-10, 5, 5, 5, 5, 5, 0, -10],
[-10, 0, 5, 0, 0, 0, 0, -10],
[-20, -10, -10, -5, -5, -10, -10, -20]]
def get_valid_move_squares(self):
valid_squares = []
valid_squares.extend(_get_diagonal_valid_moves(self))
valid_squares.extend(_get_valid_straight_move_squares(self))
return valid_squares
def _get_diagonal_valid_moves(piece):
valid_squares = []
found_right_down = True
found_left_down = True
found_right_up = True
found_left_up = True
for current_distance in range(1, NUMBER_OF_SQUARES):
if found_right_down:
found_right_down = _check_next_diagonal_valid_move(piece, current_distance, current_distance,
valid_squares)
if found_left_down:
found_left_down = _check_next_diagonal_valid_move(piece, current_distance,
current_distance * -1, valid_squares)
if found_right_up:
found_right_up = _check_next_diagonal_valid_move(piece, current_distance * -1,
current_distance, valid_squares)
if found_left_up:
found_left_up = _check_next_diagonal_valid_move(piece, current_distance * -1,
current_distance * -1,
valid_squares)
return valid_squares
def _check_next_diagonal_valid_move(piece, line_distance_from_square, tur_distance_from_square, valid_squares):
next_tur = piece.square.tur_cord + tur_distance_from_square
next_line = piece.square.line_cord + line_distance_from_square
if is_move_to_square_valid(next_tur, next_line, piece.team):
square = squares[next_line][next_tur]
valid_squares.append(square)
return square.current_piece is None
return False
def _get_valid_straight_move_squares(piece):
valid_moves = _get_straight_valid_move_squares(piece, is_vertical=True)
valid_moves.extend(_get_straight_valid_move_squares(piece, is_vertical=False))
return valid_moves
def _get_straight_valid_move_squares(piece, is_vertical):
piece_tur = piece.square.tur_cord
piece_line = piece.square.line_cord
move_on = piece_tur if is_vertical else piece_line
permanent = piece_line if is_vertical else piece_tur
valid_moves = []
for tur_or_line in range(move_on):
square = squares[permanent][tur_or_line] if is_vertical else squares[tur_or_line][permanent]
if square.current_piece is None: # If square is empty we just add him to the row.
valid_moves.append(square)
continue
if square.current_piece.team is piece.team: # If square taken by teammate piece we have to make a new row.
valid_moves = []
else: # square is taken by enemy.
# we start a new raw from this square(including).
valid_moves = [square]
continue
# Add squares after piece
for tur_or_line in range(move_on+1, NUMBER_OF_SQUARES):
square = squares[permanent][tur_or_line] if is_vertical else squares[tur_or_line][permanent]
if square.current_piece is None:
valid_moves.append(square) # If square is empty we just add him to the row.
continue
if square.current_piece.team is piece.team: # If square taken by teammate piece
break
else:
# square is taken by enemy.
valid_moves.append(square)
break
return valid_moves