Exemplo n.º 1
0
def confirm_moves(fen, depth):
    moves = BitBoardState.from_fen(fen).all_moves()
    random.shuffle(moves)
    expected_moves = all_moves_from_chess_package(fen)
    try:
        assert_equal(set(moves), set(expected_moves))
    except AssertionError:
        print(fen)
        print(BitBoardState.from_fen(fen).to_grid())
        raise
    if depth <= 1:
        return len(moves)
    total = 0
    for move in moves:
        board = BitBoardState.from_fen(fen)
        record = board.make_move(move)
        total += confirm_moves(board.to_fen(), depth - 1)
        board.unmake_move(record)
        try:
            assert_equal(board, BitBoardState.from_fen(fen))
        except AssertionError:
            print(board.to_fen())
            print(board.to_grid())
            print(BitBoardState.from_fen(fen).to_grid())
            raise
    return total
Exemplo n.º 2
0
def test_positions():
    positions = [
        'rnbq1bnr/pppkpppp/2P5/3p4/8/8/PP1PPPPP/RNBQKBNR b KQ - 0 3',
        'rnbq1bnr/pppkpppp/2P5/3p4/8/8/PP1PPPPP/RNBQKBNR b KQ - 0 3',
        'r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2NQ4/PPPBBPpP/R4RK1 b kq - 1 2',
        '8/8/3p4/1Pp4r/1K3p2/6k1/4P1P1/1R6 w - c6 0 3',
        'r3k2r/1ppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R1Q2R1K b k - 3 3'
    ]
    for fen in positions:
        board = BitBoardState.from_fen(fen)
        moves = board.all_moves()
        expected_moves = all_moves_from_chess_package(fen)
        try:
            assert_equal(set(moves), set(expected_moves))
        except AssertionError:
            print(board.to_grid())
            raise

    # The following two positions should have 218 moves each.
    # See https://chessprogramming.wikispaces.com/Encoding+Moves
    positions_218 = [
        'R6R/3Q4/1Q4Q1/4Q3/2Q4Q/Q4Q2/pp1Q4/kBNN1KB1 w - - 0 1',
        '3Q4/1Q4Q1/4Q3/2Q4R/Q4Q2/3Q4/1Q4Rp/1K1BBNNk w - - 0 1',
    ]
    for fen in positions_218:
        moves = BitBoardState.from_fen(fen).all_moves()
        expected_moves = all_moves_from_chess_package(fen)
        assert_equal(set(moves), set(expected_moves))
        assert_equal(len(moves), 218)
Exemplo n.º 3
0
def test_from_fen():
    fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    bb = BitBoardState.from_fen(fen)
    assert_equal(
        bb.get_k().to_str(),
        '0001000000000000000000000000000000000000000000000000000000010000')
    assert_equal(
        bb.get_q().to_str(),
        '0000100000000000000000000000000000000000000000000000000000001000')
    assert_equal(
        bb.get_b().to_str(),
        '0010010000000000000000000000000000000000000000000000000000100100')
    assert_equal(
        bb.get_n().to_str(),
        '0100001000000000000000000000000000000000000000000000000001000010')
    assert_equal(
        bb.get_r().to_str(),
        '1000000100000000000000000000000000000000000000000000000010000001')
    assert_equal(
        bb.get_p().to_str(),
        '0000000011111111000000000000000000000000000000001111111100000000')
    assert_equal(
        bb.get_white().to_str(),
        '0000000000000000000000000000000000000000000000001111111111111111')
    assert_equal(
        bb.get_black().to_str(),
        '1111111111111111000000000000000000000000000000000000000000000000')
Exemplo n.º 4
0
def test_queen_captures():
    fen = 'rnbqkbnr/pppppppp/8/8/8/3P4/PPP1PPPP/RNBQKBNR w KQkq - 0 1'
    moves = set(BitBoardState.from_fen(fen).queen_captures())
    assert_equal(moves, set())

    fen = 'rnbqkbnr/pppppppp/8/8/8/3p4/PPP1PPPP/RNBQKBNR w KQkq - 0 1'
    moves = set(BitBoardState.from_fen(fen).queen_captures())
    assert_equal(moves, {Move(3, 19)})

    fen = 'rnbqkbnr/pppppppp/8/8/8/1pPP4/PP2PPPP/RNBQKBNR w KQkq - 0 1'
    moves = set(BitBoardState.from_fen(fen).queen_captures())
    assert_equal(moves, {Move(3, 17)})

    # Make sure a pinned queen doesn't move out of the pin ray
    fen = 'rnb1kbnr/4q3/8/8/8/8/PPPPRPPP/RNBQKBNR b KQkq - 0 1'
    moves = set(BitBoardState.from_fen(fen).queen_captures())
    assert_equal(moves, {Move(52, 52 - 40)})
Exemplo n.º 5
0
def test_promotion():
    fen = '4k3/P7/8/8/8/8/8/8 w - - 0 100'
    board = BitBoardState.from_fen(fen)
    move = Move(48, 56, 'Q')
    record = board.make_move(move)
    assert_equal(record.promoted_from, 'P')
    assert_equal(board.to_fen(), 'Q3k3/8/8/8/8/8/8/8 b - - 0 100')
    board.unmake_move(record)
    assert_equal(board.to_fen(), fen)
Exemplo n.º 6
0
def test_long_form_move_formats():
    starting_fen = 'rnbqkbnr/pppppppr/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    board = BitBoardState.from_fen(starting_fen)
    mv = Move.from_long_form(True, 'e2-e4')
    assert_equal(mv.to_long_form(), 'e2-e4')
    board.make_move(mv)
    assert_equal(
        board.to_fen(),
        'rnbqkbnr/pppppppr/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1')
Exemplo n.º 7
0
def test_all_captures():
    fens = [
        'rnbqkbnr/pppppppr/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
        '1k6/1b6/8/8/7R/8/8/4K2R b K - 0 1', '5k2/8/8/8/8/8/8/4K2R w K - 0 1',
        'rnbqkb1r/ppp1pppp/5n2/3p4/4P3/2N2P2/PPPP2PP/R1BQKBNR b KQkq - 0 3'
    ]
    for fen in fens:
        board = BitBoardState.from_fen(fen)
        moves = board.all_moves()
        captures = board.all_captures()
        assert_equal(set(captures), set(filter(move_is_capture(board), moves)))
Exemplo n.º 8
0
def test_boardstate_to_grid():
    starting_fen = 'rnbqkbnr/pppppppr/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    board = BitBoardState.from_fen(starting_fen)
    expected_grid = '''
                    rnbqkbnr
                    pppppppr
                    --------
                    --------
                    --------
                    --------
                    PPPPPPPP
                    RNBQKBNR
                    '''.replace(' ', '').strip()
    assert_equal(board.to_grid(), expected_grid)
Exemplo n.º 9
0
def test_to_fen():
    fens = [
        '8/5N2/4p2p/5p1k/1p4rP/1P2Q1P1/P4P1K/5q2 w - - 15 44',
        'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
        'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1',
        'rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2',
        'rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2',
        'R6R/3Q4/1Q4Q1/4Q3/2Q4Q/Q4Q2/pp1Q4/kBNN1KB1 w - - 0 1',
        '3Q4/1Q4Q1/4Q3/2Q4R/Q4Q2/3Q4/1Q4Rp/1K1BBNNk w - - 0 1',
    ]
    for fen in fens:
        bb = BitBoardState.from_fen(fen)
        new_fen = bb.to_fen()
        assert_equal(new_fen, fen)
Exemplo n.º 10
0
def verify_zobrist_update_on_position(fen):
    board = BitBoardState.from_fen(fen)
    initial = board.zobrist_hash()
    moves = board.all_moves()
    for i, move in enumerate(moves):
        rec = board.make_move(move)
        new_hash = board.zobrist_hash()
        assert_equal(board.get_zobrist_hash(), new_hash)
        updated_hash = initial.update(board, rec)
        assert_equal(new_hash, updated_hash)
        downdated_hash = updated_hash.update(board, rec)
        assert_equal(downdated_hash, initial)
        board.unmake_move(rec)
        assert_equal(initial, board.zobrist_hash())
        assert_equal(board.get_zobrist_hash(), initial)
    return True
Exemplo n.º 11
0
def test_quiescence_efficiency():
    starting_fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    board = BitBoardState.from_fen(starting_fen)
    quiescent = ThresholdPlayer(0, .0000001, True)
    mundane = ThresholdPlayer(0, .0000001, False)
    t0 = time.time()
    n = 1
    for _ in range(n):
        mundane.movesearch(board)
    t1 = time.time()
    print('Mundane player took %fs for %d search%s.' %
          (t1 - t0, n, 'es' if n > 1 else ''))
    for _ in range(n):
        quiescent.movesearch(board)
    t1 = time.time()
    print('Quiescent player took %fs for %d search%s.' %
          (t1 - t0, n, 'es' if n > 1 else ''))
Exemplo n.º 12
0
def test_movesearch():
    starting_fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    #     starting_fen = 'rn1q1bnr/pp1bk1pp/2p1pp2/8/3p4/BPP1PN2/P4PPP/RN2KB1R b KQ - 2 9'
    #     starting_fen = 'rnbqkb1r/ppp1pppp/5n2/3p4/4P3/2N5/PPPP1PPP/R1BQKBNR w KQkq d6 0 3'
    board = BitBoardState.from_fen(starting_fen)
    #     white = ThresholdPlayer(5000000, 1e-6)
    #     black = ThresholdPlayer(5000000, 1e-9)
    white = TimePlayer(5000000, 6, False)
    black = TimePlayer(5000000, 6, True)
    print('\n')
    print(board.to_grid())
    while (True):
        t0 = time.time()
        move = white.movesearch(board)
        t1 = time.time()
        print('%s: Search took %fs.' % (board.to_fen(), t1 - t0))
        if move:
            board.make_move(move)
        print('#' * 10)
        print(board.to_grid())
        #         break
        if board.checkmate():
            print 'White wins!'
            break
        if not move:
            print 'Stalemate!'
            break
        t0 = time.time()
        move = black.movesearch(board)
        t1 = time.time()
        print('%s: Search took %fs.' % (board.to_fen(), t1 - t0))
        if move:
            board.make_move(move)
        print('#' * 10)
        print(board.to_grid())
        if board.checkmate():
            print 'Black wins!'
            break
        if not move:
            print 'Stalemate!'
            break
Exemplo n.º 13
0
def compare_to_python(fen, depth):
    board = BitBoardState.from_fen(fen)
    pyboard = chess.Board(fen)

    def _compare_to_python(board, pyboard, depth):
        fen = board.to_fen()
        moves = board.all_moves()
        captures = board.all_captures()
        assert_equal(set(captures), set(filter(move_is_capture(board), moves)))
        pymoves_ = list(pyboard.legal_moves)
        if pyboard.turn:
            pymoves = list(map(chess_move_to_white_move, pymoves_))
        else:
            pymoves = list(map(chess_move_to_black_move, pymoves_))
        assert_equal(set(moves), set(pymoves))
        order = list(map(pymoves.index, moves))
        if depth <= 1:
            return
        for i, move in enumerate(moves):
            record = board.make_move(move)
            pyboard.push(pymoves_[order[i]])
            try:
                _compare_to_python(board, pyboard, depth - 1)
            except AssertionError:
                print('depth = %d' % depth)
                print(board.to_fen())
                print(board.to_grid())
                print(pyboard.fen())
                print(pyboard)
                print(move)
                print(pymoves_[order[i]])
                print(i)
                record = board.unmake_move(record)
                pyboard.pop()
                raise
            board.unmake_move(record)
            pyboard.pop()
            assert_equal(board.to_fen(), fen)

    _compare_to_python(board, pyboard, depth)
Exemplo n.º 14
0
def test_quiet_queen_moves():
    fen = 'rnbqkbnr/pppppppp/8/8/8/3P4/PPP1PPPP/RNBQKBNR w KQkq - 0 1'
    moves = set(BitBoardState.from_fen(fen).quiet_queen_moves())
    assert_equal(moves, {Move(3, 11)})

    fen = 'rnbqkbnr/pppppppp/8/8/8/2PP4/PP2PPPP/RNBQKBNR w KQkq - 0 1'
    moves = set(BitBoardState.from_fen(fen).quiet_queen_moves())
    assert_equal(moves, {Move(3, 11), Move(3, 10), Move(3, 17), Move(3, 24)})

    fen = 'rnbqkbnr/pppppppp/8/8/8/1pPP4/PP2PPPP/RNBQKBNR w KQkq - 0 1'
    moves = set(BitBoardState.from_fen(fen).quiet_queen_moves())
    assert_equal(moves, {Move(3, 11), Move(3, 10)})

    fen = 'rnbqkbnQ/ppppppp1/8/8/8/1pPP4/PP2PPPP/RNBQKBNR w KQkq - 0 1'
    moves = set(BitBoardState.from_fen(fen).quiet_queen_moves())
    assert_equal(
        moves, {
            Move(3, 11),
            Move(3, 10),
            Move(63, 63 - 8),
            Move(63, 63 - 16),
            Move(63, 63 - 24),
            Move(63, 63 - 32),
            Move(63, 63 - 40)
        })

    fen = 'rnbqkbnr/ppp1pppp/8/8/8/3P4/PPP1PPPP/RNBQKBNR b KQkq - 0 1'
    moves = set(BitBoardState.from_fen(fen).quiet_queen_moves())
    assert_equal(moves, {
        Move(59, 59 - 8),
        Move(59, 59 - 16),
        Move(59, 59 - 24),
        Move(59, 59 - 32)
    })

    # Make sure a pinned queen doesn't move out of the pin ray
    fen = 'rnb1kbnr/4q3/8/8/8/8/PPPPRPPP/RNBQKBNR b KQkq - 0 1'
    moves = set(BitBoardState.from_fen(fen).quiet_queen_moves())
    assert_equal(moves, {
        Move(52, 52 - 8),
        Move(52, 52 - 16),
        Move(52, 52 - 24),
        Move(52, 52 - 32)
    })
Exemplo n.º 15
0
def test_make_move():
    starting_fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'

    # Try moving king's pawn one space
    board = BitBoardState.from_fen(starting_fen)
    record = board.make_move(Move(12, 20))
    assert_equal(board.to_fen(),
                 'rnbqkbnr/pppppppp/8/8/8/4P3/PPPP1PPP/RNBQKBNR b KQkq - 0 1')
    board.unmake_move(record)
    assert_equal(board.to_fen(), starting_fen)

    # Try moving king's pawn two spaces, thus invoking en passant
    board = BitBoardState.from_fen(starting_fen)
    record = board.make_move(Move(12, 28))
    assert_equal(board.get_enpassant(), 20)
    assert_equal(
        board.to_fen(),
        'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1')
    board.unmake_move(record)
    assert_equal(board.to_fen(), starting_fen)

    # Attempt an en passant capture
    record_stack = []
    fen_stack = [starting_fen]
    board = BitBoardState.from_fen(starting_fen)
    move = Move(12, 28)
    record = board.make_move(move)
    record_stack.append(record)
    assert_equal(record.from_square, move.from_square)
    assert_equal(record.to_square, move.to_square)
    fen_stack.append(board.to_fen())
    assert_equal(
        '''
                 rnbqkbnr
                 pppppppp
                 --------
                 --------
                 ----P---
                 ----*---
                 PPPP-PPP
                 RNBQKBNR
                 '''.replace(' ', '').strip(), board.to_grid())
    move = Move(48, 40)
    record = board.make_move(move)
    record_stack.append(record)
    assert_equal(record.from_square, move.from_square)
    assert_equal(record.to_square, move.to_square)
    fen_stack.append(board.to_fen())
    assert_equal(
        '''
                 rnbqkbnr
                 -ppppppp
                 p-------
                 --------
                 ----P---
                 --------
                 PPPP-PPP
                 RNBQKBNR
                 '''.replace(' ', '').strip(), board.to_grid())
    move = Move(28, 36)
    record = board.make_move(move)
    record_stack.append(record)
    assert_equal(record.from_square, move.from_square)
    assert_equal(record.to_square, move.to_square)
    fen_stack.append(board.to_fen())
    assert_equal(
        '''
                 rnbqkbnr
                 -ppppppp
                 p-------
                 ----P---
                 --------
                 --------
                 PPPP-PPP
                 RNBQKBNR
                 '''.replace(' ', '').strip(), board.to_grid())
    move = Move(51, 35)
    record = board.make_move(move)
    record_stack.append(record)
    assert_equal(record.from_square, move.from_square)
    assert_equal(record.to_square, move.to_square)
    fen_stack.append(board.to_fen())
    assert_equal(
        '''
                 rnbqkbnr
                 -pp-pppp
                 p--*----
                 ---pP---
                 --------
                 --------
                 PPPP-PPP
                 RNBQKBNR
                 '''.replace(' ', '').strip(), board.to_grid())
    assert_equal(board.get_enpassant(), 43)
    move = Move(36, 43)
    record = board.make_move(move)
    record_stack.append(record)
    assert_equal(record.from_square, move.from_square)
    assert_equal(record.to_square, move.to_square)
    fen_stack.append(board.to_fen())
    assert_equal(
        '''
                 rnbqkbnr
                 -pp-pppp
                 p--P----
                 --------
                 --------
                 --------
                 PPPP-PPP
                 RNBQKBNR
                 '''.replace(' ', '').strip(), board.to_grid())
    record_stack.append(board.make_move(Move(52, 36)))
    fen_stack.append(board.to_fen())
    assert_equal(
        '''
                 rnbqkbnr
                 -pp--ppp
                 p--P*---
                 ----p---
                 --------
                 --------
                 PPPP-PPP
                 RNBQKBNR
                 '''.replace(' ', '').strip(), board.to_grid())
    fen = fen_stack.pop(-1)
    assert_equal(fen, board.to_fen())
    while record_stack:
        record = record_stack.pop(-1)
        board.unmake_move(record)
        fen = fen_stack.pop(-1)
        assert_equal(fen, board.to_fen())
    assert_equal(board.to_fen(), starting_fen)

    # Try white castle king side
    board = BitBoardState.from_fen(
        'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQK2R w KQkq - 0 1')
    record = board.make_move(Move(4, 6))
    assert_equal(board.to_fen(),
                 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQ1RK1 b kq - 1 1')
    board.unmake_move(record)
    assert_equal(board.to_fen(),
                 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQK2R w KQkq - 0 1')

    # Try white castle queen side
    board = BitBoardState.from_fen(
        'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/R3KBNR w KQkq - 0 1')
    record = board.make_move(Move(4, 2))
    assert_equal(board.to_fen(),
                 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/2KR1BNR b kq - 1 1')
    board.unmake_move(record)
    assert_equal(board.to_fen(),
                 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/R3KBNR w KQkq - 0 1')

    # Try black castle king side
    board = BitBoardState.from_fen(
        'rnbqk2r/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1')
    record = board.make_move(Move(60, 62))
    assert_equal(board.to_fen(),
                 'rnbq1rk1/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQ - 1 2')
    board.unmake_move(record)
    assert_equal(board.to_fen(),
                 'rnbqk2r/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1')

    # Try black castle queen side
    board = BitBoardState.from_fen(
        'r3kbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1')
    record = board.make_move(Move(60, 58))
    assert_equal(board.to_fen(),
                 '2kr1bnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQ - 1 2')
    board.unmake_move(record)
    assert_equal(board.to_fen(),
                 'r3kbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1')
Exemplo n.º 16
0
 def choose_move(self, board):
     fen = board.fen()
     bitboard = BitBoardState.from_fen(fen)
     mv = self.player.movesearch(bitboard)
     return chess.Move.from_uci(mv.to_uci())
Exemplo n.º 17
0
def test_black_castling():
    fen = 'r3k2r/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1'
    moves = BitBoardState.from_fen(fen).all_moves()
    expected_moves = all_moves_from_chess_package(fen)
    assert_equal(set(moves), set(expected_moves))
Exemplo n.º 18
0
def test_white_castling():
    fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1'
    moves = BitBoardState.from_fen(fen).all_moves()
    expected_moves = all_moves_from_chess_package(fen)
    assert_equal(set(moves), set(expected_moves))
Exemplo n.º 19
0
 def start(self, fen):
     self.board = BitBoardState.from_fen(fen)
     self.player = TimePlayer(self.tt_size, self.time_limit, self.use_quiescence)
Exemplo n.º 20
0
def test_perft():
    # https://www.chessprogramming.net/perfect-perft/
    print('')
    print('Running perft tests...')
    fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    depth = 3
    state = BitBoardState.from_fen(fen)
    t0 = time.time()
    assert_equal(state.perft(depth), 8902)
    t1 = time.time()
    print('%s perft(%d) passed in %f seconds' % (fen, depth, t1 - t0))
    fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    depth = 5
    state = BitBoardState.from_fen(fen)
    t0 = time.time()
    assert_equal(state.perft(depth), 4865609)
    t1 = time.time()
    print('%s perft(%d) passed in %f seconds' % (fen, depth, t1 - t0))
    fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    depth = 6
    state = BitBoardState.from_fen(fen)
    t0 = time.time()
    assert_equal(state.perft(depth), 119060324)
    t1 = time.time()
    print('%s perft(%d) passed in %f seconds' % (fen, depth, t1 - t0))
    fen = 'r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(5), 193690690)
    print('%s perft passed' % fen)
    fen = '8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(7), 178633661)
    print('%s perft passed' % fen)
    fen = 'r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(6), 706045033)
    print('%s perft passed' % fen)
    fen = '1k6/1b6/8/8/7R/8/8/4K2R b K - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(5), 1063513)
    print('%s perft passed' % fen)
    fen = '3k4/3p4/8/K1P4r/8/8/8/8 b - - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(6), 1134888)
    print('%s perft passed' % fen)
    fen = '8/8/4k3/8/2p5/8/B2P2K1/8 w - - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(6), 1015133)
    print('%s perft passed' % fen)
    fen = '8/8/1k6/2b5/2pP4/8/5K2/8 b - d3 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(6), 1440467)
    print('%s perft passed' % fen)
    fen = '5k2/8/8/8/8/8/8/4K2R w K - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(6), 661072)
    print('%s perft passed' % fen)
    fen = '3k4/8/8/8/8/8/8/R3K3 w Q - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(6), 803711)
    print('%s perft passed' % fen)
    fen = 'r3k2r/1b4bq/8/8/8/8/7B/R3K2R w KQkq - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(4), 1274206)
    print('%s perft passed' % fen)
    fen = 'r3k2r/8/3Q4/8/8/5q2/8/R3K2R b KQkq - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(4), 1720476)
    print('%s perft passed' % fen)
    fen = '2K2r2/4P3/8/8/8/8/8/3k4 w - - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(6), 3821001)
    print('%s perft passed' % fen)
    fen = '8/8/1P2K3/8/2n5/1q6/8/5k2 b - - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(5), 1004658)
    print('%s perft passed' % fen)
    fen = '4k3/1P6/8/8/8/8/K7/8 w - - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(6), 217342)
    print('%s perft passed' % fen)
    fen = '8/P1k5/K7/8/8/8/8/8 w - - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(6), 92683)
    print('%s perft passed' % fen)
    fen = 'K1k5/8/P7/8/8/8/8/8 w - - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(6), 2217)
    print('%s perft passed' % fen)
    fen = '8/k1P5/8/1K6/8/8/8/8 w - - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(7), 567584)
    print('%s perft passed' % fen)
    fen = '8/8/2k5/5q2/5n2/8/5K2/8 b - - 0 1'
    assert_equal(BitBoardState.from_fen(fen).perft(4), 23527)
    print('%s perft passed' % fen)
    print('All perft tests complete.')
Exemplo n.º 21
0
def test_to_grid():
    fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    grid = '''
           rnbqkbnr
           pppppppp
           --------
           --------
           --------
           --------
           PPPPPPPP
           RNBQKBNR
           '''.strip().replace(' ', '')
    bb = BitBoardState.from_fen(fen)
    assert_equal(bb.to_grid(), grid)

    fen = '8/5N2/4p2p/5p1k/1p4rP/1P2Q1P1/P4P1K/5q2 w - - 15 44'
    grid = '''
           --------
           -----N--
           ----p--p
           -----p-k
           -p----rP
           -P--Q-P-
           P----P-K
           -----q--
           '''.strip().replace(' ', '')
    bb = BitBoardState.from_fen(fen)
    assert_equal(bb.to_grid(), grid)

    fen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1'
    grid = '''
           rnbqkbnr
           pppppppp
           --------
           --------
           ----P---
           ----*---
           PPPP-PPP
           RNBQKBNR
           '''.strip().replace(' ', '')
    bb = BitBoardState.from_fen(fen)
    assert_equal(bb.to_grid(), grid)

    fen = 'rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2'
    grid = '''
           rnbqkbnr
           pp-ppppp
           --*-----
           --p-----
           ----P---
           --------
           PPPP-PPP
           RNBQKBNR
           '''.strip().replace(' ', '')
    bb = BitBoardState.from_fen(fen)
    assert_equal(bb.to_grid(), grid)

    fen = 'rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2'
    grid = '''
           rnbqkbnr
           pp-ppppp
           --------
           --p-----
           ----P---
           -----N--
           PPPP-PPP
           RNBQKB-R
           '''.strip().replace(' ', '')
    bb = BitBoardState.from_fen(fen)
    assert_equal(bb.to_grid(), grid)

    fen = 'R6R/3Q4/1Q4Q1/4Q3/2Q4Q/Q4Q2/pp1Q4/kBNN1KB1 w - - 0 1'
    grid = '''
           R------R
           ---Q----
           -Q----Q-
           ----Q---
           --Q----Q
           Q----Q--
           pp-Q----
           kBNN-KB-
           '''.strip().replace(' ', '')
    bb = BitBoardState.from_fen(fen)
    assert_equal(bb.to_grid(), grid)

    fen = '3Q4/1Q4Q1/4Q3/2Q4R/Q4Q2/3Q4/1Q4Rp/1K1BBNNk w - - 0 1'
    grid = '''
           ---Q----
           -Q----Q-
           ----Q---
           --Q----R
           Q----Q--
           ---Q----
           -Q----Rp
           -K-BBNNk
           '''.strip().replace(' ', '')
    bb = BitBoardState.from_fen(fen)
    assert_equal(bb.to_grid(), grid)
Exemplo n.º 22
0
def test_all_moves():
    fen = 'rnbqkbnr/pppppppr/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    moves = BitBoardState.from_fen(fen).all_moves()
    expected_moves = all_moves_from_chess_package(fen)
    assert_equal(set(moves), set(expected_moves))
Exemplo n.º 23
0
def test_place_piece():
    fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    bb = BitBoardState.from_fen(fen)
    bb.place_piece(0, 'p')
    new_fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/pNBQKBNR w KQkq - 0 1'
    assert_equal(bb.to_fen(), new_fen)