-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_parts.py
224 lines (161 loc) · 6.62 KB
/
game_parts.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
from typing import List, Union
from enum import Enum
from helper_funcs import get_checker_pattern
class PieceType(Enum):
Black = 0
White = 1
class DefaultRepr:
def __repr__(self):
return "{}({})".format(self.__class__, self.__dict__)
class Square(DefaultRepr):
def __init__(self, x: int, y: int, piece: 'Piece' = None):
self.x = x
self.y = y
self.piece = piece
def set_piece(self, piece):
self.piece = piece
def get_char_form(self):
if self.piece is None:
return "."
else:
return str(self.piece)
def __str__(self):
return "Square ({}, {})".format(self.x, self.y)
class Piece(DefaultRepr):
def __init__(self, current_square: Square, type: PieceType, is_king=False):
self.current_square = current_square
self.type = type
self.is_king = is_king
@property
def x(self):
return self.current_square.x
@property
def y(self):
return self.current_square.y
def __str__(self):
if self.type == PieceType.Black:
return "X"
else:
return "O"
class Move:
def __init__(self, origin: Piece, steps: Union[Square, List[Square]], captures: Union[Piece, List[Piece]] = None):
if captures is None:
self.captures = [] # type: List[Piece]
elif isinstance(captures, list):
self.captures = captures
else:
self.captures = [captures]
self.origin = origin
if isinstance(steps, list):
self.steps = steps
else:
self.steps = [steps]
def clone(self):
return Move(self.origin, self.steps, self.captures)
def add_step(self, new_dest: Square, capture: Piece):
self.steps.append(new_dest)
self.captures.append(capture)
@property
def dest(self):
return self.steps[-1]
def __str__(self):
return " -> ".join([str(sq) for sq in [self.origin.current_square] + self.steps])
class Board(DefaultRepr):
def __init__(self):
self.squares = []
self.piece_by_type = {
PieceType.Black: [],
PieceType.White: []
}
for row_num in range(10):
row = []
for column_num in range(10):
row.append(Square(column_num, row_num))
self.squares.append(row)
@property
def all_pieces(self):
return self.piece_by_type[PieceType.Black] + self.piece_by_type[PieceType.White]
def has_player_lost(self, piece_type: PieceType):
return self.piece_by_type[piece_type] == []
def add_piece(self, square, type, is_king=False):
piece = Piece(square, type, is_king)
square.set_piece(piece)
self.piece_by_type[type].append(piece)
return piece
def remove_piece(self, square: Square):
piece = square.piece
square.set_piece(None)
self.piece_by_type[piece.type].remove(piece)
def init_default_state(self):
def fill_squares(init_offset, init_row, piece_type):
for (col, row) in get_checker_pattern(init_offset, init_row, 4, 10):
self.add_piece(self.squares[col][row], piece_type)
fill_squares(1, 0, PieceType.White)
fill_squares(1, 6, PieceType.Black)
def get_str_form(self):
lines = []
for row in self.squares:
line = ""
for square in row:
line += square.get_char_form()
lines.append(line)
return "\n".join(lines)
def get_square_by_coords(self, x, y) -> Square:
if y < 0 or x < 0 or y >= len(self.squares) or x >= len(self.squares[0]):
return None
else:
return self.squares[y][x]
def apply_move(self, move: Move):
move.origin.current_square.piece = None
move.origin.current_square = move.dest
move.dest.piece = move.origin
# promote to kings
if move.dest.piece.type == PieceType.White and move.dest.piece.y == 9:
move.dest.piece.is_king = True
elif move.dest.piece.type == PieceType.Black and move.dest.piece.y == 0:
move.dest.piece.is_king = True
#remove captured pieces
for capture in move.captures:
self.remove_piece(capture.current_square)
def get_moves_by_colour(self, piece_type: PieceType):
moves_by_colour = []
for piece in self.piece_by_type[piece_type]:
moves_by_colour.extend(self.get_piece_moves(piece))
return moves_by_colour
def get_piece_moves(self, piece: Piece) -> List[Move]:
moves = []
def add_close_moves(trying_x: int, trying_y: int):
trying_square = self.get_square_by_coords(trying_x, trying_y)
if trying_square is not None and trying_square.piece is None:
moves.append(Move(piece, self.get_square_by_coords(trying_x, trying_y)))
def add_jump_moves(direction_x: int, direction_y: int):
jump_moves = [] # type: List[Move]
(current_x, current_y) = (piece.x, piece.y)
dest_square = self.get_square_by_coords(current_x + 2 * direction_x, current_y + 2 * direction_y)
over_square = self.get_square_by_coords(current_x + direction_x, current_y + direction_y)
while over_square is not None \
and dest_square is not None \
and over_square.piece is not None \
and dest_square.piece is None \
and over_square.piece.type != piece.type:
if len(jump_moves) == 0:
jump_moves.append(Move(piece, dest_square, over_square.piece))
else:
move = jump_moves[-1].clone()
move.add_step(dest_square, over_square.piece)
jump_moves.append(move)
(current_x, current_y) = (over_square.x, over_square.y)
dest_square = self.get_square_by_coords(current_x + 2 * direction_x, current_y + 2 * direction_y)
over_square = self.get_square_by_coords(current_x + direction_x, current_y + direction_y)
moves.extend(jump_moves)
if piece.is_king or piece.type == PieceType.White:
add_close_moves(piece.x + 1, piece.y + 1)
add_close_moves(piece.x - 1, piece.y + 1)
add_jump_moves(1, 1)
add_jump_moves(-1, 1)
if piece.is_king or piece.type == PieceType.Black:
add_close_moves(piece.x - 1, piece.y - 1)
add_close_moves(piece.x + 1, piece.y - 1)
add_jump_moves(-1, -1)
add_jump_moves(1, -1)
return moves