2019-09-03 22:44:48 +02:00
|
|
|
from itertools import product
|
2019-09-01 21:29:25 +02:00
|
|
|
from unicodedata import lookup
|
2019-09-10 14:30:43 +02:00
|
|
|
from copy import deepcopy
|
2019-09-01 21:29:25 +02:00
|
|
|
|
2019-09-01 16:52:51 +02:00
|
|
|
def cross(A, B):
|
|
|
|
return tuple(a+b for a in A for b in B)
|
|
|
|
|
2019-09-04 21:05:22 +02:00
|
|
|
pieces = ["pawn", "knight", "bishop", "rook", "queen", "king"]
|
|
|
|
colors = ["white", "black"]
|
|
|
|
|
2019-09-03 21:01:02 +02:00
|
|
|
ranks = "87654321"
|
2019-09-01 16:52:51 +02:00
|
|
|
files = "abcdefgh"
|
|
|
|
squares = cross(files, ranks)
|
|
|
|
|
2019-09-06 19:43:35 +02:00
|
|
|
pawn_ranks = {"white" : '2',
|
|
|
|
"black" : '7'
|
|
|
|
}
|
|
|
|
home_ranks = {"white" : '1',
|
|
|
|
"black" : '8'
|
|
|
|
}
|
|
|
|
init_positions = {"pawn" : cross(files, pawn_ranks.values()),
|
|
|
|
"rook" : cross("ah", home_ranks.values()),
|
|
|
|
"knight" : cross("bg", home_ranks.values()),
|
|
|
|
"bishop" : cross("cf", home_ranks.values()),
|
|
|
|
"queen" : cross("d", home_ranks.values()),
|
|
|
|
"king" : cross("e", home_ranks.values())
|
2019-09-01 16:52:51 +02:00
|
|
|
}
|
2019-09-01 21:29:25 +02:00
|
|
|
|
2019-09-06 17:10:43 +02:00
|
|
|
AN_names = {'R' : "rook",
|
|
|
|
'N' : "knight",
|
|
|
|
'B' : "bishop",
|
|
|
|
'Q' : "queen",
|
|
|
|
'K' : "king"}
|
|
|
|
|
2019-09-04 21:05:22 +02:00
|
|
|
def move(sq, v):
|
2019-09-03 22:44:48 +02:00
|
|
|
return chr(ord(sq[0]) + v[0]) + str(int(sq[1]) + v[1])
|
|
|
|
|
2019-09-05 19:16:58 +02:00
|
|
|
def moves(sq, dirs):
|
|
|
|
targets = [move(sq, dir) for dir in dirs]
|
|
|
|
return [sq for sq in targets if sq in squares]
|
|
|
|
|
|
|
|
def invert(color):
|
|
|
|
if color == "white":
|
|
|
|
return "black"
|
|
|
|
return "white"
|
|
|
|
|
2019-09-06 17:10:43 +02:00
|
|
|
class Empty:
|
|
|
|
def __repr__(self):
|
|
|
|
return str(vars(self))
|
|
|
|
|
|
|
|
def parse_AN(move):
|
|
|
|
def suffix_in(l, dval=False, i=1):
|
|
|
|
nonlocal move
|
|
|
|
x = move[-i:]
|
|
|
|
if x in list(l):
|
|
|
|
move = move[:-i]
|
|
|
|
return x
|
|
|
|
return dval
|
|
|
|
|
|
|
|
queenside = ["{}-{}-{}".format(c,c,c) for c in "0O"]
|
|
|
|
kingside = ["{}-{}".format(c,c) for c in "0O"]
|
|
|
|
|
|
|
|
r = Empty()
|
|
|
|
r.kingside = r.queenside = False
|
|
|
|
if move in queenside:
|
|
|
|
r.queenside = True
|
|
|
|
return r
|
|
|
|
if move in kingside:
|
|
|
|
r.kingside = True
|
|
|
|
return r
|
|
|
|
|
|
|
|
r.move = move
|
|
|
|
r.mate = suffix_in('#') == '#'
|
|
|
|
r.check = suffix_in('+') == '+'
|
2019-09-10 16:28:12 +02:00
|
|
|
r.promotion = suffix_in(AN_names, None)
|
2019-09-06 17:10:43 +02:00
|
|
|
if r.promotion:
|
|
|
|
r.promotion = AN_names[r.promotion]
|
|
|
|
_ = suffix_in('=')
|
|
|
|
r.dest = suffix_in(squares, i=2)
|
|
|
|
r.capture = suffix_in("xX") != False
|
|
|
|
r.rank = suffix_in(ranks, None)
|
|
|
|
r.file = suffix_in(files, None)
|
|
|
|
r.piece = suffix_in(AN_names, 'pawn')
|
|
|
|
if len(move) > 0 or (r.promotion and r.piece != "pawn") or not r.dest:
|
|
|
|
return False
|
|
|
|
if r.piece != "pawn":
|
|
|
|
r.piece = AN_names[r.piece]
|
|
|
|
return r
|
|
|
|
|
2019-09-04 21:05:22 +02:00
|
|
|
up, down = (0, 1), (0, -1)
|
|
|
|
left, right = (-1, 0), (1, 0)
|
|
|
|
rook_dirs = (up, down, left, right)
|
2019-09-03 22:44:48 +02:00
|
|
|
|
2019-09-05 19:16:58 +02:00
|
|
|
upper_left, upper_right = (-1, 1), (1, 1)
|
|
|
|
lower_left, lower_right = (-1, -1), (1, -1)
|
|
|
|
bishop_dirs = (upper_left, upper_right,
|
|
|
|
lower_left, lower_right)
|
2019-09-01 21:29:25 +02:00
|
|
|
|
2019-09-03 20:46:59 +02:00
|
|
|
class Piece:
|
|
|
|
def __init__(self, color=None, piece=None):
|
|
|
|
self.color = color
|
|
|
|
self.piece = piece
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
if None in (self.piece, self.color):
|
2019-09-10 18:47:34 +02:00
|
|
|
return ""
|
2019-09-03 20:46:59 +02:00
|
|
|
name = self.color.upper() + " CHESS " + self.piece.upper()
|
|
|
|
return lookup(name)
|
2019-09-01 21:29:25 +02:00
|
|
|
|
2019-09-10 18:47:34 +02:00
|
|
|
def __str__(self):
|
|
|
|
return repr(self)
|
|
|
|
|
2019-09-03 21:01:02 +02:00
|
|
|
def __eq__(self, other):
|
|
|
|
if other == None:
|
|
|
|
return None in (self.color, self.piece)
|
|
|
|
if not isinstance(other, Piece):
|
|
|
|
return NotImplemented
|
|
|
|
return self.piece == other.piece and self.color == other.color
|
|
|
|
|
2019-09-01 16:52:51 +02:00
|
|
|
class Game:
|
|
|
|
def __init__(self):
|
2019-09-10 21:36:49 +02:00
|
|
|
self.index = 0
|
2019-09-01 16:52:51 +02:00
|
|
|
self.make_board()
|
2019-09-03 23:43:32 +02:00
|
|
|
self.moves = []
|
2019-09-10 21:36:49 +02:00
|
|
|
self.AN_moves = []
|
|
|
|
self.history = []
|
2019-09-06 17:10:43 +02:00
|
|
|
self.turn = "white"
|
2019-09-10 14:01:17 +02:00
|
|
|
self.can_castle = {"white" : {"queen" : True,
|
|
|
|
"king" : True},
|
|
|
|
"black" : {"queen" : True,
|
|
|
|
"king" : True}
|
|
|
|
}
|
2019-09-10 21:36:49 +02:00
|
|
|
self.history.append(deepcopy(self))
|
2019-09-01 16:52:51 +02:00
|
|
|
|
2019-09-01 21:29:25 +02:00
|
|
|
def __repr__(self):
|
|
|
|
# Unicode board representation
|
|
|
|
r = ""
|
|
|
|
for rank in ranks:
|
|
|
|
r += rank + " |"
|
|
|
|
for file in files:
|
2019-09-03 20:50:52 +02:00
|
|
|
sq = file+rank
|
|
|
|
r += " "
|
|
|
|
if self.is_empty(sq):
|
|
|
|
r += " "
|
2019-09-01 21:29:25 +02:00
|
|
|
else:
|
2019-09-03 20:50:52 +02:00
|
|
|
r += repr(self.board[sq])
|
2019-09-01 21:29:25 +02:00
|
|
|
r += "\n"
|
|
|
|
r += " +" + "-"*16 + "\n"
|
|
|
|
r += " "*4 + " ".join(list(files))
|
|
|
|
return r
|
|
|
|
|
2019-09-01 16:52:51 +02:00
|
|
|
def make_board(self):
|
2019-09-03 20:46:59 +02:00
|
|
|
self.board = dict((sq, Piece()) for sq in squares)
|
2019-09-01 16:52:51 +02:00
|
|
|
# Add pieces
|
|
|
|
for piece, positions in init_positions.items():
|
|
|
|
for sq in positions:
|
2019-09-03 20:46:59 +02:00
|
|
|
self.board[sq].piece = piece
|
2019-09-01 16:52:51 +02:00
|
|
|
# Add colors
|
|
|
|
for sq in cross(files, "12"):
|
2019-09-03 20:46:59 +02:00
|
|
|
self.board[sq].color = "white"
|
2019-09-01 16:52:51 +02:00
|
|
|
for sq in cross(files, "78"):
|
2019-09-03 20:46:59 +02:00
|
|
|
self.board[sq].color = "black"
|
2019-09-10 14:01:17 +02:00
|
|
|
# Castling markers
|
|
|
|
self.board["a1"].side = "queen"
|
|
|
|
self.board["a8"].side = "queen"
|
|
|
|
self.board["h1"].side = "king"
|
|
|
|
self.board["h8"].side = "king"
|
2019-09-01 16:52:51 +02:00
|
|
|
|
2019-09-10 21:36:49 +02:00
|
|
|
def rebase(self):
|
|
|
|
self.moves = self.moves[:self.index+1]
|
|
|
|
self.history = self.history[:self.index+1]
|
|
|
|
|
|
|
|
def timetravel(self, i):
|
|
|
|
if i >= len(self.history) or i < 0:
|
|
|
|
return False
|
|
|
|
new = self.history[i]
|
|
|
|
self.board = new.board
|
|
|
|
self.turn = new.turn
|
|
|
|
self.can_castle = new.can_castle
|
|
|
|
self.index = i
|
|
|
|
|
|
|
|
def prev(self):
|
|
|
|
return self.timetravel(self.index-1)
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
return self.timetravel(self.index+1)
|
|
|
|
|
|
|
|
def last(self):
|
|
|
|
return self.timetravel(len(self.history)-1)
|
|
|
|
|
|
|
|
def first(self):
|
|
|
|
return self.timetravel(0)
|
|
|
|
|
2019-09-03 20:50:52 +02:00
|
|
|
def is_empty(self, sq):
|
2019-09-03 21:01:02 +02:00
|
|
|
return self.board[sq] == None
|
|
|
|
|
2019-09-10 14:30:43 +02:00
|
|
|
def is_check(self, color=None):
|
|
|
|
if color == None:
|
|
|
|
color = self.turn
|
|
|
|
sq = self.occupying(Piece(color, "king"))[0]
|
|
|
|
return self.is_attacked(color, sq)
|
|
|
|
|
|
|
|
def is_mate(self):
|
2019-09-10 15:22:15 +02:00
|
|
|
return self.is_check() and self.is_stall()
|
|
|
|
|
|
|
|
def is_stall(self):
|
2019-09-10 14:30:43 +02:00
|
|
|
for move in self.all_legal_moves():
|
|
|
|
new = deepcopy(self)
|
2019-09-10 15:22:15 +02:00
|
|
|
if self.board[move[0]].piece == "pawn" and move[1][1]==home_ranks[invert(self.turn)]:
|
|
|
|
move += ("queen",)
|
|
|
|
if new.move(*move):
|
2019-09-10 14:30:43 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2019-09-10 16:28:12 +02:00
|
|
|
def endofgame(self):
|
|
|
|
return self.is_stall() or self.is_mate()
|
|
|
|
|
2019-09-03 21:01:02 +02:00
|
|
|
def occupying(self, piece):
|
|
|
|
return [sq for sq in squares if self.board[sq] == piece]
|
2019-09-03 20:50:52 +02:00
|
|
|
|
2019-09-10 14:01:17 +02:00
|
|
|
def castle(self, side):
|
|
|
|
color = self.turn
|
|
|
|
rank = home_ranks[self.turn]
|
|
|
|
|
|
|
|
if side not in ("queen", "king"):
|
|
|
|
return False
|
|
|
|
if not self.can_castle[color][side]:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if side == "queen":
|
|
|
|
check_if_empty = cross("bcd", rank)
|
|
|
|
check_if_attacked = cross("cde", rank)
|
|
|
|
rook_move = "a"+rank, "d"+rank
|
|
|
|
king_move = "e"+rank, "c"+rank
|
|
|
|
elif side == "king":
|
|
|
|
check_if_empty = cross("fg", rank)
|
|
|
|
check_if_attacked = check_if_empty
|
|
|
|
rook_move = "h"+rank, "f"+rank
|
|
|
|
king_move = "e"+rank, "g"+rank
|
|
|
|
are_empty = all(map(self.is_empty, check_if_empty))
|
|
|
|
not_attacked = not any(self.is_attacked(color, sq)
|
|
|
|
for sq in check_if_attacked)
|
|
|
|
|
|
|
|
if not (are_empty and not_attacked):
|
|
|
|
return False
|
|
|
|
|
2019-09-10 21:36:49 +02:00
|
|
|
self.rebase()
|
2019-09-10 14:01:17 +02:00
|
|
|
self.board[king_move[1]] = self.board[king_move[0]]
|
|
|
|
self.board[rook_move[1]] = self.board[rook_move[0]]
|
|
|
|
self.board[king_move[0]] = Piece()
|
|
|
|
self.board[rook_move[0]] = Piece()
|
|
|
|
self.can_castle[color]["queen"] = False
|
|
|
|
self.can_castle[color]["king"] = False
|
2019-09-10 15:22:15 +02:00
|
|
|
self.moves.append(king_move)
|
2019-09-10 16:28:12 +02:00
|
|
|
self.turn = invert(self.turn)
|
2019-09-10 21:36:49 +02:00
|
|
|
self.history.append(deepcopy(self))
|
|
|
|
self.index += 1
|
2019-09-10 15:22:15 +02:00
|
|
|
return True
|
2019-09-10 14:01:17 +02:00
|
|
|
|
2019-09-10 13:08:06 +02:00
|
|
|
def move(self, source, target, promotion=None):
|
2019-09-10 15:22:15 +02:00
|
|
|
new = deepcopy(self)
|
|
|
|
if not new.is_legal(source, target, promotion):
|
2019-09-03 23:43:32 +02:00
|
|
|
return False
|
2019-09-10 15:22:15 +02:00
|
|
|
|
|
|
|
board = new.board
|
2019-09-03 23:43:32 +02:00
|
|
|
moved = board[source]
|
|
|
|
eaten = board[target]
|
2019-09-10 14:01:17 +02:00
|
|
|
|
2019-09-10 13:08:06 +02:00
|
|
|
if promotion == None:
|
|
|
|
promotion = moved
|
|
|
|
else:
|
2019-09-10 15:22:15 +02:00
|
|
|
promotion = Piece(new.turn, promotion)
|
2019-09-10 13:08:06 +02:00
|
|
|
|
2019-09-10 14:01:17 +02:00
|
|
|
if moved.piece == "pawn" and target in moves(source, bishop_dirs) and eaten==None:
|
2019-09-10 13:08:06 +02:00
|
|
|
# En passant
|
|
|
|
a, b = move(target, up), move(target, down)
|
|
|
|
board[a] = Piece()
|
|
|
|
board[b] = Piece()
|
2019-09-03 23:43:32 +02:00
|
|
|
|
2019-09-10 14:01:17 +02:00
|
|
|
# Castling
|
|
|
|
if moved.piece == "rook":
|
2019-09-10 15:22:15 +02:00
|
|
|
new.can_castle[new.turn][moved.side] = False
|
2019-09-10 14:01:17 +02:00
|
|
|
if eaten.piece == "rook":
|
2019-09-10 15:22:15 +02:00
|
|
|
new.can_castle[eaten.color][eaten.side] = False
|
2019-09-10 14:01:17 +02:00
|
|
|
if moved.piece == "king":
|
2019-09-10 15:22:15 +02:00
|
|
|
new.can_castle[new.turn]["queen"] = False
|
|
|
|
new.can_castle[new.turn]["king"] = False
|
2019-09-03 23:43:32 +02:00
|
|
|
|
|
|
|
board[source] = Piece()
|
2019-09-10 13:08:06 +02:00
|
|
|
board[target] = promotion
|
2019-09-10 15:22:15 +02:00
|
|
|
|
|
|
|
if new.is_check(self.turn):
|
|
|
|
return False
|
|
|
|
|
2019-09-10 21:36:49 +02:00
|
|
|
self.rebase()
|
2019-09-10 15:22:15 +02:00
|
|
|
self.moves.append((source, target))
|
|
|
|
self.board = new.board
|
|
|
|
self.can_castle = new.can_castle
|
2019-09-06 17:10:43 +02:00
|
|
|
self.turn = invert(moved.color)
|
2019-09-10 21:36:49 +02:00
|
|
|
self.history.append(deepcopy(self))
|
|
|
|
self.index += 1
|
2019-09-03 23:43:32 +02:00
|
|
|
return True
|
|
|
|
|
2019-09-10 13:08:06 +02:00
|
|
|
def is_legal(self, source, target, promotion):
|
|
|
|
piece = self.board[source].piece
|
2019-09-06 17:10:43 +02:00
|
|
|
color = self.board[source].color
|
2019-09-10 13:08:06 +02:00
|
|
|
pawn_on_last_rank = piece == "pawn" and \
|
|
|
|
home_ranks[invert(color)] == target[1]
|
|
|
|
if promotion != None and not pawn_on_last_rank:
|
|
|
|
return False
|
|
|
|
if promotion == None and pawn_on_last_rank:
|
|
|
|
return False
|
2019-09-10 14:01:17 +02:00
|
|
|
if promotion in ("king", "pawn"):
|
2019-09-10 13:08:06 +02:00
|
|
|
return False
|
2019-09-06 17:10:43 +02:00
|
|
|
return color == self.turn and target in self.possible_moves(source)
|
2019-09-03 23:43:32 +02:00
|
|
|
|
2019-09-05 19:16:58 +02:00
|
|
|
def is_attacked(self, color, sq):
|
|
|
|
enemy_color = invert(color)
|
|
|
|
attacked = []
|
|
|
|
for piece in pieces:
|
|
|
|
occupied = self.occupying(Piece(enemy_color, piece))
|
|
|
|
attacked += sum(list(map(self.attacks, occupied)), [])
|
|
|
|
return sq in attacked
|
2019-09-03 23:33:24 +02:00
|
|
|
|
2019-09-10 14:30:43 +02:00
|
|
|
def all_legal_moves(self):
|
|
|
|
color = self.turn
|
|
|
|
sqrs = []
|
|
|
|
for piece in pieces:
|
|
|
|
piece = Piece(color, piece)
|
|
|
|
sqrs += self.occupying(piece)
|
|
|
|
r = []
|
|
|
|
for sq in sqrs:
|
|
|
|
mvs = self.possible_moves(sq)
|
|
|
|
r += [(sq, m) for m in mvs]
|
|
|
|
return r
|
|
|
|
|
2019-09-03 22:44:48 +02:00
|
|
|
def possible_moves(self, sq):
|
2019-09-03 23:33:24 +02:00
|
|
|
board = self.board
|
|
|
|
piece = board[sq].piece
|
|
|
|
color = board[sq].color
|
2019-09-03 22:44:48 +02:00
|
|
|
|
2019-09-04 21:05:22 +02:00
|
|
|
def can_eat(target):
|
|
|
|
eaten = board[target]
|
2019-09-05 19:16:58 +02:00
|
|
|
return eaten != None and eaten.color != color
|
|
|
|
|
|
|
|
is_empty = self.is_empty
|
2019-09-04 21:05:22 +02:00
|
|
|
|
2019-09-05 19:16:58 +02:00
|
|
|
targets = [t for t in self.attacks(sq)
|
|
|
|
if can_eat(t) or is_empty(t)]
|
|
|
|
|
|
|
|
if piece == "pawn":
|
|
|
|
r = []
|
2019-09-09 14:10:55 +02:00
|
|
|
dir, back = up, down
|
2019-09-05 19:16:58 +02:00
|
|
|
if color == "black":
|
2019-09-09 14:10:55 +02:00
|
|
|
dir, back = down, up
|
2019-09-05 19:16:58 +02:00
|
|
|
frwd = move(sq, dir)
|
|
|
|
jump = move(frwd, dir)
|
|
|
|
|
|
|
|
if frwd in squares and is_empty(frwd):
|
|
|
|
r.append(frwd)
|
2019-09-06 19:43:35 +02:00
|
|
|
is_on_pawn_rank = pawn_ranks[color] == sq[1]
|
2019-09-05 19:16:58 +02:00
|
|
|
if is_on_pawn_rank and is_empty(jump):
|
|
|
|
r.append(jump)
|
2019-09-09 14:10:55 +02:00
|
|
|
for t in targets:
|
|
|
|
a, b = move(t, dir), move(t, back)
|
2019-09-10 21:36:49 +02:00
|
|
|
en_passant = can_eat(b) and self.index>0 and\
|
|
|
|
self.moves[self.index-1] == (a,b)
|
2019-09-09 14:10:55 +02:00
|
|
|
if can_eat(t) or en_passant:
|
|
|
|
r.append(t)
|
|
|
|
return r
|
2019-09-05 19:16:58 +02:00
|
|
|
elif piece == "king":
|
|
|
|
return [sq for sq in targets if not self.is_attacked(color, sq)]
|
|
|
|
else:
|
|
|
|
return targets
|
|
|
|
|
|
|
|
def attacks(self, sq):
|
|
|
|
if sq not in squares:
|
|
|
|
return []
|
|
|
|
|
|
|
|
board = self.board
|
|
|
|
piece = board[sq].piece
|
|
|
|
color = board[sq].color
|
2019-09-04 21:05:22 +02:00
|
|
|
|
|
|
|
def possible_line(dir):
|
|
|
|
target = move(sq, dir)
|
2019-09-05 19:16:58 +02:00
|
|
|
while target in board and self.is_empty(target):
|
2019-09-04 21:05:22 +02:00
|
|
|
yield target
|
|
|
|
target = move(target, dir)
|
2019-09-05 19:16:58 +02:00
|
|
|
if target in board:
|
2019-09-04 21:05:22 +02:00
|
|
|
yield target
|
|
|
|
|
|
|
|
def possible_lines(dirs):
|
|
|
|
return sum(list(map(list, map(possible_line, dirs))), [])
|
|
|
|
|
|
|
|
rook = possible_lines(rook_dirs)
|
|
|
|
bishop = possible_lines(bishop_dirs)
|
|
|
|
|
2019-09-03 22:44:48 +02:00
|
|
|
if piece == "pawn":
|
2019-09-05 19:16:58 +02:00
|
|
|
if color == "white":
|
|
|
|
dirs = upper_left, upper_right
|
|
|
|
else:
|
|
|
|
dirs = lower_left, lower_right
|
|
|
|
return moves(sq, dirs)
|
2019-09-03 22:44:48 +02:00
|
|
|
elif piece == "knight":
|
2019-09-04 21:05:22 +02:00
|
|
|
nums = [2, -2, 1, -1]
|
2019-09-05 19:16:58 +02:00
|
|
|
dirs = [(a, b) for a,b in product(nums, nums)
|
|
|
|
if abs(a) != abs(b)]
|
|
|
|
return moves(sq, dirs)
|
2019-09-03 22:44:48 +02:00
|
|
|
elif piece == "rook":
|
2019-09-04 21:05:22 +02:00
|
|
|
return rook
|
2019-09-03 22:44:48 +02:00
|
|
|
elif piece == "bishop":
|
2019-09-04 21:05:22 +02:00
|
|
|
return bishop
|
2019-09-03 22:44:48 +02:00
|
|
|
elif piece == "queen":
|
2019-09-04 21:05:22 +02:00
|
|
|
return rook + bishop
|
2019-09-03 23:33:24 +02:00
|
|
|
elif piece == "king":
|
2019-09-05 19:16:58 +02:00
|
|
|
dirs = rook_dirs + bishop_dirs
|
|
|
|
return moves(sq, dirs)
|
2019-09-03 23:43:32 +02:00
|
|
|
return []
|
2019-09-03 20:50:52 +02:00
|
|
|
|
2019-09-10 16:28:12 +02:00
|
|
|
def validate_move(self, source, dest, promotion):
|
|
|
|
new = deepcopy(self)
|
|
|
|
valid = new.move(source, dest, promotion)
|
|
|
|
new.turn = self.turn
|
|
|
|
return valid, new
|
|
|
|
|
|
|
|
def AN_move(self, move):
|
2019-09-10 21:36:49 +02:00
|
|
|
AN = move
|
2019-09-10 16:28:12 +02:00
|
|
|
info = parse_AN(move)
|
|
|
|
if info == False:
|
|
|
|
return False
|
|
|
|
if info.queenside:
|
|
|
|
return self.castle("queen")
|
|
|
|
if info.kingside:
|
|
|
|
return self.castle("king")
|
|
|
|
|
|
|
|
piece = info.piece
|
|
|
|
sqrs = self.occupying(Piece(self.turn, piece))
|
|
|
|
promotion = info.promotion
|
|
|
|
dest = info.dest
|
|
|
|
if info.file != None:
|
|
|
|
sqrs = [sq for sq in sqrs if sq[0]==info.file]
|
|
|
|
if info.rank != None:
|
|
|
|
sqrs = [sq for sq in sqrs if sq[1]==info.rank]
|
|
|
|
moves = []
|
|
|
|
for sq in sqrs:
|
|
|
|
valid, new = self.validate_move(sq, dest, promotion)
|
|
|
|
check = new.is_check()==info.check and new.is_mate()==info.mate
|
|
|
|
if valid and check:
|
|
|
|
moves.append((sq, dest, promotion))
|
|
|
|
if len(moves) != 1:
|
|
|
|
return False
|
|
|
|
move = moves[0]
|
|
|
|
self.move(*move)
|
2019-09-10 21:36:49 +02:00
|
|
|
self.AN_moves.append(AN)
|
2019-09-10 16:28:12 +02:00
|
|
|
return True
|
|
|
|
|
2019-09-01 21:29:25 +02:00
|
|
|
|
|
|
|
|
2019-09-01 16:52:51 +02:00
|
|
|
def test():
|
|
|
|
game = Game()
|
2019-09-10 21:36:49 +02:00
|
|
|
move = ""
|
|
|
|
while not game.is_stall() and move!='q':
|
2019-09-10 16:28:12 +02:00
|
|
|
move = input(">> ")
|
2019-09-10 21:36:49 +02:00
|
|
|
if move == "prev":
|
|
|
|
game.prev()
|
|
|
|
print(game)
|
|
|
|
continue
|
|
|
|
elif move == "next":
|
|
|
|
game.next()
|
|
|
|
print(game)
|
|
|
|
continue
|
2019-09-10 16:28:12 +02:00
|
|
|
v = game.AN_move(move)
|
|
|
|
if not v:
|
|
|
|
print("Invalid")
|
|
|
|
continue
|
|
|
|
print(game)
|
2019-09-10 21:36:49 +02:00
|
|
|
print (game.history)
|
2019-09-06 19:43:35 +02:00
|
|
|
|