-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
229 lines (204 loc) · 8.45 KB
/
game.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
from board import Board
from piece import Pawn, Bishop, Knight, Rook, Queen, King
from player import Player, Color
from position import Position
class Game(object):
def __init__(self, board,
players=(Player(Color.WHITE), Player(Color.BLACK))):
self.moves = []
self.board = board
self.players = players
self.ply = 0
@property
def current_player(self):
"""The player whose turn it is."""
return self.players[self.ply % 2]
def _make_move(self, move):
"""Apply the given move to the board."""
#Handle castling
if type(move.piece) == King:
dy = move.to[0] - move.start[0]
if abs(dy) == 2:
if dy == 2:
rook = self.board.piece_at((7, move.to[1]))
assert rook is not None, (move.piece, self.board._pieces)
self.board.move_piece(rook, (5, move.to[1]))
else:
rook = self.board.piece_at((0, move.to[1]))
assert rook is not None, (move.piece, self.board._pieces)
self.board.move_piece(rook, (3, move.to[1]))
move.piece.owner.castling.append((False, False))
#Remove castling rights on rook moves
elif type(move.piece) == Rook:
if move.start[0] == 0:
move.piece.owner.castling.append(
(False, move.piece.owner.castling[-1][1]))
elif move.start[0] == 7:
move.piece.owner.castling.append(
(move.piece.owner.castling[-1][0], False))
else:
move.piece.owner.castling.append(move.piece.owner.castling[-1])
#Otherwise repeat the last set of castling rights
else:
move.piece.owner.castling.append(move.piece.owner.castling[-1])
# handle en-passant
if type(move.piece) == Pawn:
pawn = move.piece
pawn.just_moved = (pawn.y == pawn.start_rank)
if move.captured is not None:
self.board.remove_piece(move.captured)
self.board.move_piece(move.piece, move.to)
if move.promotion is not None:
promoted = move.promotion(move.piece.owner, move.piece.location)
self.board.remove_piece(move.piece)
self.board.add_piece(promoted)
self.moves.append(move)
self.ply += 1
def _undo_move(self):
"""Apply the move in reverse to the board."""
# restore castling rights
self.ply -= 1
move = self.moves.pop()
move.piece.owner.castling.pop()
# if move was a castle, restore rook position
if type(move.piece) == King:
dy = move.to[0] - move.start[0]
if dy == 2:
rook = self.board.piece_at((5, move.to[1]))
self.board.move_piece(rook, (7, move.to[1]))
elif dy == -2:
rook = self.board.piece_at((3, move.to[1]))
self.board.move_piece(rook, (0, move.to[1]))
# remove promoted piece
# restore promoting pawn
if move.promotion is not None:
promoted = move.promotion(move.piece.owner, move.to)
self.board.remove_piece(promoted)
self.board.add_piece(move.piece)
# restore piece location
self.board.move_piece(move.piece, move.start)
# restore captured piece
if move.captured is not None:
self.board.add_piece(move.captured)
def is_legal(self, move):
"""Check if a move is legal.
A move is legal if
* a piece can reach the target square
* an allied piece is not on the target square
* moving would not place the owner in check."""
piece = self.board.piece_at(move.to)
if piece is None or piece.owner != move.piece.owner:
# disallow castling through check
if type(move.piece) == King:
dx = move.to[0] - move.start[0]
if abs(dx) == 2: # castle
through = move.start[0] + dx / 2, move.to[1]
if any((piece.can_attack(self.board, through) or
piece.can_attack(self.board, move.start))
for piece in self.board.pieces
if piece.owner != move.piece.owner):
return False
with Position(self, move) as position:
return not move.piece.owner.is_in_check(position)
else:
return False
@property
def is_over(self):
"""Check if the game is over for the given player.
The game is over if a player cannot make any moves."""
return not any(self.is_legal(move)
for move in self.current_player.moves(self.board))
def is_checkmate(self, player):
"""Check if the given player is checkmated.
A player is checkmated if the game is over
and the player is in check."""
return self.is_over and player.is_in_check(self.board)
def is_stalemate(self, player):
"""Check if the given player is stalemated.
A player is stalemated if the game is over
and the player is not in check."""
return self.is_over and not player.is_in_check(self.board)
def from_fen(self, fen):
"""Reset game to match given FEN string"""
self.board = Board()
components = fen.split(" ")
rows = components[0].split("/")
for row, row_str in enumerate(rows):
column = 0
for entry in row_str:
if entry.isdigit():
column += int(entry)
else:
sq = (column, 7 - row)
player = self.players[entry.islower()]
entry = entry.lower()
if entry.lower() == "r":
self.board.add_piece(Rook(player, sq))
elif entry.lower() == "n":
self.board.add_piece(Knight(player, sq))
elif entry.lower() == "b":
self.board.add_piece(Bishop(player, sq))
elif entry.lower() == "q":
self.board.add_piece(Queen(player, sq))
elif entry.lower() == "k":
self.board.add_piece(King(player, sq))
elif entry.lower() == "p":
self.board.add_piece(Pawn(player, sq))
column += 1
# self.ply = int(components[1] == "b") + int(components[5]) - 1
def play(self):
"""Play the game."""
print "Starting game."
print "Press CTRL+C to quit."
print self.board
while not self.is_over:
print "%s's turn." % self.current_player
move = self.current_player.get_move(self)
print move
if self.is_legal(move):
self._make_move(move)
print self.board
else:
print "Illegal move."
if self.is_checkmate(self.current_player):
print "Checkmate."
else:
print "Stalemate"
print "Game over."
def perft(self, depth):
nodes = 0
for move in self.current_player.moves(self.board):
if self.is_legal(move):
if (depth == 1):
nodes += 1
else:
self._make_move(move)
nodes += self.perft(depth - 1)
self._undo_move()
return nodes
def perft_captures(self, depth):
nodes = 0
for move in self.current_player.moves(self.board):
if self.is_legal(move):
if (depth == 1):
if (move.captured):
nodes += 1
else:
self._make_move(move)
nodes += self.perft_captures(depth - 1)
self._undo_move()
return nodes
def divide(self, depth):
print self.board
if (depth == 1):
for move in self.current_player.moves(self.board):
if self.is_legal(move):
self._make_move(move)
print move
self._undo_move()
else:
for move in self.current_player.moves(self.board):
if self.is_legal(move):
self._make_move(move)
print move, " ", self.perft(depth-1)
self._undo_move()