-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpieces.py
More file actions
293 lines (214 loc) · 9.41 KB
/
pieces.py
File metadata and controls
293 lines (214 loc) · 9.41 KB
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
import board, ai
class Piece():
WHITE = "W"
BLACK = "B"
def __init__(self, x, y, color, piece_type, value):
self.x = x
self.y = y
self.color = color
self.piece_type = piece_type
self.value = value
# Returns all diagonal moves for this piece. This should therefore only
# be used by the Bishop and Queen since they are the only pieces that can
# move diagonally.
def get_possible_diagonal_moves(self, board):
moves = []
for i in range(1, board.Board.WIDTH):
if (not board.in_bounds(self.x+i, self.y+i)):
break
piece = board.get_piece(self.x+i, self.y+i)
moves.append(self.get_move(board, self.x+i, self.y+i))
if (piece != 0):
break
for i in range(1, board.Board.WIDTH):
if (not board.in_bounds(self.x+i, self.y-i)):
break
piece = board.get_piece(self.x+i, self.y-i)
moves.append(self.get_move(board, self.x+i, self.y-i))
if (piece != 0):
break
for i in range(1, board.Board.WIDTH):
if (not board.in_bounds(self.x-i, self.y-i)):
break
piece = board.get_piece(self.x-i, self.y-i)
moves.append(self.get_move(board, self.x-i, self.y-i))
if (piece != 0):
break
for i in range(1, board.Board.WIDTH):
if (not board.in_bounds(self.x-i, self.y+i)):
break
piece = board.get_piece(self.x-i, self.y+i)
moves.append(self.get_move(board, self.x-i, self.y+i))
if (piece != 0):
break
return self.remove_null_from_list(moves)
# Returns all horizontal moves for this piece. This should therefore only
# be used by the Rooks and Queen since they are the only pieces that can
# move horizontally.
def get_possible_horizontal_moves(self, board):
moves = []
# Moves to the right of the piece.
for i in range(1, board.Board.WIDTH - self.x):
piece = board.get_piece(self.x + i, self.y)
moves.append(self.get_move(board, self.x+i, self.y))
if (piece != 0):
break
# Moves to the left of the piece.
for i in range(1, self.x + 1):
piece = board.get_piece(self.x - i, self.y)
moves.append(self.get_move(board, self.x-i, self.y))
if (piece != 0):
break
# Downward moves.
for i in range(1, board.Board.HEIGHT - self.y):
piece = board.get_piece(self.x, self.y + i)
moves.append(self.get_move(board, self.x, self.y+i))
if (piece != 0):
break
# Upward moves.
for i in range(1, self.y + 1):
piece = board.get_piece(self.x, self.y - i)
moves.append(self.get_move(board, self.x, self.y-i))
if (piece != 0):
break
return self.remove_null_from_list(moves)
# Returns a Move object with (xfrom, yfrom) set to the piece current position.
# (xto, yto) is set to the given position. If the move is not valid 0 is returned.
# A move is not valid if it is out of bounds, or a piece of the same color is
# being eaten.
def get_move(self, board, xto, yto):
move = 0
if (board.in_bounds(xto, yto)):
piece = board.get_piece(xto, yto)
if (piece != 0):
if (piece.color != self.color):
move = ai.Move(self.x, self.y, xto, yto, False)
else:
move = ai.Move(self.x, self.y, xto, yto, False)
return move
# Returns the list of moves cleared of all the 0's.
def remove_null_from_list(self, l):
return [move for move in l if move != 0]
def to_string(self):
return self.color + self.piece_type + " "
class Rook(Piece):
PIECE_TYPE = "R"
VALUE = 500
def __init__(self, x, y, color):
super(Rook, self).__init__(x, y, color, Rook.PIECE_TYPE, Rook.VALUE)
def get_possible_moves(self, board):
return self.get_possible_horizontal_moves(board)
def clone(self):
return Rook(self.x, self.y, self.color)
class Knight(Piece):
PIECE_TYPE = "N"
VALUE = 320
def __init__(self, x, y, color):
super(Knight, self).__init__(x, y, color, Knight.PIECE_TYPE, Knight.VALUE)
def get_possible_moves(self, board):
moves = []
moves.append(self.get_move(board, self.x+2, self.y+1))
moves.append(self.get_move(board, self.x-1, self.y+2))
moves.append(self.get_move(board, self.x-2, self.y+1))
moves.append(self.get_move(board, self.x+1, self.y-2))
moves.append(self.get_move(board, self.x+2, self.y-1))
moves.append(self.get_move(board, self.x+1, self.y+2))
moves.append(self.get_move(board, self.x-2, self.y-1))
moves.append(self.get_move(board, self.x-1, self.y-2))
return self.remove_null_from_list(moves)
def clone(self):
return Knight(self.x, self.y, self.color)
class Bishop(Piece):
PIECE_TYPE = "B"
VALUE = 330
def __init__(self, x, y, color):
super(Bishop, self).__init__(x, y, color, Bishop.PIECE_TYPE, Bishop.VALUE)
def get_possible_moves(self, board):
return self.get_possible_diagonal_moves(board)
def clone(self):
return Bishop(self.x, self.y, self.color)
class Queen(Piece):
PIECE_TYPE = "Q"
VALUE = 900
def __init__(self, x, y, color):
super(Queen, self).__init__(x, y, color, Queen.PIECE_TYPE, Queen.VALUE)
def get_possible_moves(self, board):
diagonal = self.get_possible_diagonal_moves(board)
horizontal = self.get_possible_horizontal_moves(board)
return horizontal + diagonal
def clone(self):
return Queen(self.x, self.y, self.color)
class King(Piece):
PIECE_TYPE = "K"
VALUE = 20000
def __init__(self, x, y, color):
super(King, self).__init__(x, y, color, King.PIECE_TYPE, King.VALUE)
def get_possible_moves(self, board):
moves = []
moves.append(self.get_move(board, self.x+1, self.y))
moves.append(self.get_move(board, self.x+1, self.y+1))
moves.append(self.get_move(board, self.x, self.y+1))
moves.append(self.get_move(board, self.x-1, self.y+1))
moves.append(self.get_move(board, self.x-1, self.y))
moves.append(self.get_move(board, self.x-1, self.y-1))
moves.append(self.get_move(board, self.x, self.y-1))
moves.append(self.get_move(board, self.x+1, self.y-1))
moves.append(self.get_top_castling_move(board))
moves.append(self.get_bottom_castling_move(board))
return self.remove_null_from_list(moves)
def get_top_castling_move(self, board):
if (self.color == Piece.WHITE and board.white_king_moved):
return 0
if (self.color == Piece.BLACK and board.black_king_moved):
return 0
piece = board.get_piece(self.x, self.y-3)
if (piece != 0):
if (piece.color == self.color and piece.piece_type == Rook.PIECE_TYPE):
if (board.get_piece(self.x, self.y-1) == 0 and board.get_piece(self.x, self.y-2) == 0):
return ai.Move(self.x, self.y, self.x, self.y-2, True)
return 0
def get_bottom_castling_move(self, board):
if (self.color == Piece.WHITE and board.white_king_moved):
return 0
if (self.color == Piece.BLACK and board.black_king_moved):
return 0
piece = board.get_piece(self.x, self.y+4)
if (piece != 0):
if (piece.color == self.color and piece.piece_type == Rook.PIECE_TYPE):
if (board.get_piece(self.x, self.y+1) == 0 and board.get_piece(self.x, self.y+2) == 0 and board.get_piece(self.x, self.y+3) == 0):
return ai.Move(self.x, self.y, self.x, self.y+2, True)
return 0
def clone(self):
return King(self.x, self.y, self.color)
class Pawn(Piece):
PIECE_TYPE = "P"
VALUE = 100
def __init__(self, x, y, color):
super(Pawn, self).__init__(x, y, color, Pawn.PIECE_TYPE, Pawn.VALUE)
def is_starting_position(self):
if (self.color == Piece.BLACK):
return self.x == board.Board.WIDTH - 2
else:
return self.x == 1
def get_possible_moves(self, board):
moves = []
# Direction the pawn can move in.
direction = 1
if (self.color == Piece.BLACK):
direction = -1
# The general 1 step forward move.
if (board.get_piece(self.x+direction, self.y) == 0):
moves.append(self.get_move(board, self.x + direction, self.y))
# The Pawn can take 2 steps as the first move.
if (self.is_starting_position() and board.get_piece(self.x + direction, self.y) == 0 and board.get_piece(self.x + direction*2, self.y) == 0):
moves.append(self.get_move(board, self.x + direction * 2, self.y))
# Eating pieces.
piece = board.get_piece(self.x + direction, self.y + 1)
if (piece != 0):
moves.append(self.get_move(board, self.x + direction, self.y + 1))
piece = board.get_piece(self.x + direction, self.y - 1)
if (piece != 0):
moves.append(self.get_move(board, self.x + direction, self.y - 1))
return self.remove_null_from_list(moves)
def clone(self):
return Pawn(self.x, self.y, self.color)