def str(mv): if promotion(mv) == ivory_piece.KING: return 'O-O-O' if square.file(tosq(mv)) == 'c' else 'O-O' out = [] if piece(mv) == ivory_piece.PAWN: if is_capture(mv): out.append(square.file(frsq(mv))) out.append('x') else: out.append(ivory_piece.str(piece(mv)).upper()) if is_show_file(mv): out.append(square.file(frsq(mv))) if is_show_rank(mv): out.append(square.rank(frsq(mv))) if is_capture(mv): out.append('x') out.append(square.str(tosq(mv))) if promotion(mv): print square.str(frsq(mv)), square.str(tosq(mv)) out.append('=') out.append(ivory_piece.str(promotion(mv)).upper()) if is_check(mv): out.append('+') if is_mate(mv): out.append('#') return ''.join(out)
def _get_fen_board(self): pieces = [None] * 64 for sq, pc in self.squares.iteritems(): if pc: val = piece.str(pc) if self._get_square_color(sq) == 1: val = val.upper() pieces[square.index(sq)] = val ranks = [] for rank in xrange(8): count = 0 out = '' for file in xrange(8): pc = pieces[square.index(square.from_a8(rank, file))] if pc is None: count += 1 if file == 7: out += '%d' % count else: if count: out += '%d' % count count = 0 out += pc ranks.append(out) return '/'.join(ranks)
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 __str__(self): out = [] for rank in xrange(8): for file in xrange(8): sq = square.from_a8(rank, file) piece_str = piece.str(self.squares[sq]) if self.color_bbs[1] & sq: piece_str = piece_str.upper() out.append(piece_str) out.append('\n') return ''.join(out)
self.num_promotions += 1 if movegen.king_attacked(self, self.color): self.num_checks += 1 if self._game_over(): self.num_mates += 1 self.unmake_move() return nodes # NOTE(vish): insert constants into locals for sq in square.all(): locals()[square.str(sq)] = sq for pc in piece.all(): locals()[piece.str(pc)] = pc #fen = 'r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1' fen = '8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1' p1 = Position(fen) print piece.str(p1.squares[square.sq('a8')]) import time import prettytable pt = prettytable.PrettyTable() pt.field_names = [ 'Depth', 'Nodes', 'Captures', 'E.p.', 'Castles', 'Promotions', 'Checks', 'Mates', 'Time' ] pt.align = 'l' #import cProfile #cProfile.run('nodes = p1.perft(4)')
else: self.num_promotions += 1 if movegen.king_attacked(self, self.color): self.num_checks += 1 if self._game_over(): self.num_mates += 1 self.unmake_move() return nodes # NOTE(vish): insert constants into locals for sq in square.all(): locals()[square.str(sq)] = sq for pc in piece.all(): locals()[piece.str(pc)] = pc #fen = 'r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1' fen = '8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1' p1 = Position(fen) print piece.str(p1.squares[square.sq('a8')]) import time import prettytable pt = prettytable.PrettyTable() pt.field_names = ['Depth', 'Nodes', 'Captures', 'E.p.', 'Castles', 'Promotions', 'Checks', 'Mates', 'Time'] pt.align = 'l' #import cProfile #cProfile.run('nodes = p1.perft(4)') for i in xrange(1, 6): p1.zero_stats()