def unmake_move(self): mv, pc, self.halfmove_clock, self.castle, self.enp = self.moves.pop() opp_cl = self.color self.color = not self.color cl = self.color if cl == 0: self.move_num -= 1 tosq = move.tosq(mv) mvpc = move.piece(mv) prom = move.promotion(mv) self.set_square(move.frsq(mv), mvpc, cl) self.clear_square(tosq) if prom == piece.PAWN: back = square.n if opp_cl == 1 else square.s self.set_square(back(tosq), piece.PAWN, opp_cl) elif prom == piece.KING: rfrsq, rtosq = self.CASTLE_SQUARE_MAP[tosq] rook = self.clear_square(rtosq) self.set_square(rfrsq, rook, cl) if pc: self.set_square(tosq, pc, opp_cl) if self.squares[square.sq('h1')] == piece.PAWN: print move.str(mv), piece.str(pc) raise Exception()
def _disambiguate_moves(self, moves): files = {} ranks = {} unamb = list(moves) for mv in moves: if move.promotion(mv): continue ps = (move.piece(mv), move.tosq(mv)) files.setdefault(ps, []) ranks.setdefault(ps, []) files[ps].append(square.file(move.frsq(mv))) ranks[ps].append(square.rank(move.frsq(mv))) opp_cl = not self.color for i, mv in enumerate(moves): ps = (move.piece(mv), move.tosq(mv)) if files[ps].count(square.file(move.frsq(mv))) > 1: unamb[i] = moves[i].set_show_rank() if ranks[ps].count(square.rank(move.frsq(mv))) > 1: unamb[i] = moves[i].set_show_file() return unamb
def make_move(self, mv): cl = self.color half, cast, enp = self.halfmove_clock, self.castle, self.enp self.color = not self.color opp_cl = self.color if cl == 0: self.move_num += 1 self.enp = 0L frsq = move.frsq(mv) tosq = move.tosq(mv) prom = move.promotion(mv) pc = self.clear_square(tosq) mvpc = move.piece(mv) if pc or mvpc == piece.PAWN: self.halfmove_clock = 0 else: self.halfmove_clock += 1 if prom and prom != piece.PAWN and prom != piece.KING: mvpc = prom self.set_square(tosq, mvpc, cl) self.clear_square(frsq) if tosq == self.ROOK_SQUARES[opp_cl][0]: self.castle &= ~castle.QUEEN[opp_cl] elif tosq == self.ROOK_SQUARES[opp_cl][1]: self.castle &= ~castle.KING[opp_cl] if mvpc == piece.KING: self.castle &= ~castle.QUEEN[cl] self.castle &= ~castle.KING[cl] if prom == piece.KING: rfrsq, rtosq = self.CASTLE_SQUARE_MAP[tosq] rook = self.clear_square(rfrsq) self.set_square(rtosq, rook, cl) elif mvpc == piece.ROOK: if frsq == self.ROOK_SQUARES[cl][0]: self.castle &= ~castle.QUEEN[cl] elif frsq == self.ROOK_SQUARES[cl][1]: self.castle &= ~castle.KING[cl] elif mvpc == piece.PAWN: back = square.n(tosq) if opp_cl == 1 else square.s(tosq) if prom == piece.PAWN: self.clear_square(back) else: diff = square.rank(frsq) - square.rank(tosq) if diff == 2 or diff == -2: self.enp = back self.moves.append((mv, pc, half, cast, enp)) return pc