Ejemplo n.º 1
0
 def test_get_blank_board(self):
     board = get_blank_board()
     board[0] = (15, 0)
     board[1] = (0, 15)
     self.assertTrue(is_valid_board(board))
     board = get_blank_board()
     board[0] = (15, 15)
     self.assertFalse(is_valid_board(board))
Ejemplo n.º 2
0
 def test_move_prioritization(self):
     """
     Ensure we don't return:
         board[2] = (0,15)
         board[4] = (15,0)
     as a next board because you must use the max number of rolls possible.
     """
     board = get_blank_board()
     board[1] = (0, 1)
     board[2] = (0, 14)
     board[4] = (15, 0)
     self.assertTrue(is_valid_board(board))
     roll = [2, 1]
     next_boards = generate_next_boards(board, False, roll)
     self.assertEqual(len(next_boards), 2)
     board1 = get_blank_board()
     board1[2] = (0, 13)
     board1[3] = (0, 2)
     board1[4] = (15, 0)
     self.assertIn(board1, next_boards)
     board2 = get_blank_board()
     board2[1] = (0, 1)
     board2[2] = (0, 13)
     board2[4] = (15, 0)
     board2[5] = (0, 1)
     self.assertIn(board2, next_boards)
Ejemplo n.º 3
0
 def test_white_bear_off(self):
     board = get_blank_board()
     board[10] = (15, 0)
     board[15] = (0, 0)
     board[23] = (0, 15)
     self.assertTrue(is_valid_board(board))
     roll = [1]
     next_boards = generate_next_boards(board, False, roll)
     self.assertEqual(len(next_boards), 1)
     self.assertEqual(next_boards[0][23], (0, 14))
     self.assertEqual(next_boards[0][WHITE_OFF_INDEX], (0, 1))
     roll = [2]
     next_boards = generate_next_boards(board, False, roll)
     self.assertEqual(len(next_boards), 1)
     self.assertEqual(next_boards[0][23], (0, 14))
     self.assertEqual(next_boards[0][WHITE_OFF_INDEX], (0, 1))
     board = get_blank_board()
     board[10] = (15, 0)
     board[22] = (0, 15)
     roll = [3, 1]
     next_boards = generate_next_boards(board, False, roll)
     self.assertEqual(len(next_boards), 1)
     self.assertEqual(next_boards[0][23], (0, 1))
     self.assertEqual(next_boards[0][22], (0, 13))
     self.assertEqual(next_boards[0][WHITE_OFF_INDEX], (0, 1))
Ejemplo n.º 4
0
def _get_all_boards(board, roll):
    """
    Generates a list of all legal next boards given <board> and a roll. 
    Always assumes white is moving (swap_colors as necessary).
    """
    if board[WHITE_BAR_INDEX][WHITE_INDEX] > 0:
        return _handle_bar_pieces(board, roll)
    all_boards = []
    for position, (black_count, white_count) in enumerate(board):
        if position < 0 or position > 23:
            continue
        if white_count == 0:
            continue
        target = position + roll
        if (target > 23 and can_bear_off(board)
                and position_is_outer(board, position)):
            target = WHITE_OFF_INDEX
        elif target == 24 and can_bear_off(board):
            target = WHITE_OFF_INDEX
        elif target > 23:
            continue
        new_board = _move_to_board_position(board, position, target)
        if is_valid_board(new_board):
            all_boards.append(new_board)
    return all_boards
Ejemplo n.º 5
0
 def test_position_is_outer(self):
     board = get_blank_board()
     board[10] = (15, 0)
     board[15] = (0, 13)
     board[16] = (0, 2)
     self.assertTrue(is_valid_board(board))
     self.assertFalse(position_is_outer(board, 16))
     self.assertTrue(position_is_outer(board, 15))
Ejemplo n.º 6
0
 def test_can_bear_off(self):
     board = get_blank_board()
     board[10] = (15, 0)
     board[15] = (0, 15)
     self.assertTrue(is_valid_board(board))
     self.assertFalse(can_bear_off(board))
     board[15] = (0, 0)
     board[23] = (0, 15)
     self.assertTrue(can_bear_off(board))
Ejemplo n.º 7
0
def _handle_bar_pieces(board, roll):
    """
    Moves pieces off the bar.
    """
    target_index = -1 + roll
    board = _move_to_board_position(board, WHITE_BAR_INDEX, target_index)
    if is_valid_board(board):
        return [board]
    return []
Ejemplo n.º 8
0
 def test_black_enter_from_bar(self):
     board = get_blank_board()
     board[BLACK_BAR_INDEX] = (15, 0)
     board[23] = (0, 15)
     self.assertTrue(is_valid_board(board))
     roll = [2, 1]
     next_boards = generate_next_boards(board, True, roll)
     self.assertEqual(len(next_boards), 1)
     self.assertEqual(next_boards[0][BLACK_BAR_INDEX], (14, 0))
     self.assertEqual(next_boards[0][23], (0, 15))
     self.assertEqual(next_boards[0][22], (1, 0))
     roll = [1, 1, 1, 1]
     next_boards = generate_next_boards(board, True, roll)
     self.assertEqual(len(next_boards), 1)
     self.assertEqual(next_boards[0][BLACK_BAR_INDEX], (15, 0))
     self.assertEqual(next_boards[0][23], (0, 15))
Ejemplo n.º 9
0
 def test_white_enter_from_bar(self):
     board = get_blank_board()
     board[WHITE_BAR_INDEX] = (0, 15)
     board[0] = (15, 0)
     self.assertTrue(is_valid_board(board))
     roll = [2, 1]
     next_boards = generate_next_boards(board, False, roll)
     self.assertEqual(len(next_boards), 1)
     self.assertEqual(next_boards[0][WHITE_BAR_INDEX], (0, 14))
     self.assertEqual(next_boards[0][0], (15, 0))
     self.assertEqual(next_boards[0][1], (0, 1))
     roll = [1, 1, 1, 1]
     next_boards = generate_next_boards(board, False, roll)
     self.assertEqual(len(next_boards), 1)
     self.assertEqual(next_boards[0][WHITE_BAR_INDEX], (0, 15))
     self.assertEqual(next_boards[0][0], (15, 0))
Ejemplo n.º 10
0
 def test_swap_colors(self):
     board = list(get_initial_board())
     board[0] = (0, 0)
     board[1] = (1, 0)
     board[5] = (4, 0)
     board[WHITE_BAR_INDEX] = (0, 2)
     board[23] = (0, 0)
     board[BLACK_OFF_INDEX] = (2, 0)
     self.assertTrue(is_valid_board(board))
     board = swap_colors(board)
     self.assertEqual(board[0], (0, 0))
     self.assertEqual(board[1], (0, 0))
     self.assertEqual(board[2], (0, 0))
     self.assertEqual(board[3], (0, 0))
     self.assertEqual(board[4], (0, 0))
     self.assertEqual(board[5], (5, 0))
     self.assertEqual(board[6], (0, 0))
     self.assertEqual(board[7], (3, 0))
     self.assertEqual(board[8], (0, 0))
     self.assertEqual(board[9], (0, 0))
     self.assertEqual(board[10], (0, 0))
     self.assertEqual(board[11], (0, 5))
     self.assertEqual(board[12], (5, 0))
     self.assertEqual(board[13], (0, 0))
     self.assertEqual(board[14], (0, 0))
     self.assertEqual(board[15], (0, 0))
     self.assertEqual(board[16], (0, 3))
     self.assertEqual(board[17], (0, 0))
     self.assertEqual(board[18], (0, 4))
     self.assertEqual(board[19], (0, 0))
     self.assertEqual(board[20], (0, 0))
     self.assertEqual(board[21], (0, 0))
     self.assertEqual(board[22], (0, 1))
     self.assertEqual(board[23], (0, 0))
     self.assertEqual(board[WHITE_BAR_INDEX], (0, 0))
     self.assertEqual(board[BLACK_BAR_INDEX], (2, 0))
     self.assertEqual(board[WHITE_OFF_INDEX], (0, 2))
     self.assertEqual(board[BLACK_OFF_INDEX], (0, 0))
Ejemplo n.º 11
0
 def test_black_bear_off(self):
     board = get_blank_board()
     board[1] = (0, 15)
     board[0] = (15, 0)
     self.assertTrue(is_valid_board(board))
     roll = [1]
     next_boards = generate_next_boards(board, True, roll)
     self.assertEqual(len(next_boards), 1)
     self.assertEqual(next_boards[0][0], (14, 0))
     self.assertEqual(next_boards[0][BLACK_OFF_INDEX], (1, 0))
     roll = [2]
     next_boards = generate_next_boards(board, True, roll)
     self.assertEqual(len(next_boards), 1)
     self.assertEqual(next_boards[0][0], (14, 0))
     self.assertEqual(next_boards[0][BLACK_OFF_INDEX], (1, 0))
     board = get_blank_board()
     board[10] = (0, 15)
     board[1] = (15, 0)
     roll = [3, 1]
     next_boards = generate_next_boards(board, True, roll)
     self.assertEqual(len(next_boards), 1)
     self.assertEqual(next_boards[0][0], (1, 0))
     self.assertEqual(next_boards[0][1], (13, 0))
     self.assertEqual(next_boards[0][BLACK_OFF_INDEX], (1, 0))
Ejemplo n.º 12
0
def visualize_board(board):
    """Prints ASCII visualization of the <board>."""
    assert is_valid_board(board)

    def _get_pos(board, position):
        tens = max(board[position][WHITE_INDEX] / 10,
                   board[position][BLACK_INDEX] / 10)
        zeros = max(board[position][WHITE_INDEX] % 10,
                    board[position][BLACK_INDEX] % 10)
        is_white = board[position][WHITE_INDEX] > board[position][BLACK_INDEX]
        color = 'W' if is_white else 'B'
        if (tens == 0) and (zeros == 0):
            zeros = ' '
            color = '_'
        if tens == 0:
            tens = ' '
        return tens, zeros, color

    tens0, ones0, color0 = _get_pos(board, 0)
    tens1, ones1, color1 = _get_pos(board, 1)
    tens2, ones2, color2 = _get_pos(board, 2)
    tens3, ones3, color3 = _get_pos(board, 3)
    tens4, ones4, color4 = _get_pos(board, 4)
    tens5, ones5, color5 = _get_pos(board, 5)
    tens6, ones6, color6 = _get_pos(board, 6)
    tens7, ones7, color7 = _get_pos(board, 7)
    tens8, ones8, color8 = _get_pos(board, 8)
    tens9, ones9, color9 = _get_pos(board, 9)
    tens10, ones10, color10 = _get_pos(board, 10)
    tens11, ones11, color11 = _get_pos(board, 11)
    tens12, ones12, color12 = _get_pos(board, 12)
    tens13, ones13, color13 = _get_pos(board, 13)
    tens14, ones14, color14 = _get_pos(board, 14)
    tens15, ones15, color15 = _get_pos(board, 15)
    tens16, ones16, color16 = _get_pos(board, 16)
    tens17, ones17, color17 = _get_pos(board, 17)
    tens18, ones18, color18 = _get_pos(board, 18)
    tens19, ones19, color19 = _get_pos(board, 19)
    tens20, ones20, color20 = _get_pos(board, 20)
    tens21, ones21, color21 = _get_pos(board, 21)
    tens22, ones22, color22 = _get_pos(board, 22)
    tens23, ones23, color23 = _get_pos(board, 23)
    tensbb, onesbb, colorbb = _get_pos(board, BLACK_BAR_INDEX)
    if tensbb == ' ' and onesbb == ' ':
        colorbb = ' '
    tenswb, oneswb, colorwb = _get_pos(board, WHITE_BAR_INDEX)
    if tenswb == ' ' and oneswb == ' ':
        colorwb = ' '
    tensbo, onesbo, colorbo = _get_pos(board, BLACK_OFF_INDEX)
    if tensbo == ' ' and onesbo == ' ':
        colorbo = ' '
    tenswo, oneswo, colorwo = _get_pos(board, WHITE_OFF_INDEX)
    if tenswo == ' ' and oneswo == ' ':
        colorwo = ' '
    print
    print '|  .  .  .  .  .  .  |   |  .  .  .  .  .  .  |'
    print '|  {}  {}  {}  {}  {}  {}  | {} |  {}  {}  {}  {}  {}  {}  | {}'.format(
        tens11, tens10, tens9, tens8, tens7, tens6, tensbb, tens5, tens4,
        tens3, tens2, tens1, tens0, tensbo)
    print '|  {}  {}  {}  {}  {}  {}  | {} |  {}  {}  {}  {}  {}  {}  | {}'.format(
        ones11, ones10, ones9, ones8, ones7, ones6, onesbb, ones5, ones4,
        ones3, ones2, ones1, ones0, onesbo)
    print '|  {}  {}  {}  {}  {}  {}  | {} |  {}  {}  {}  {}  {}  {}  | {}'.format(
        color11, color10, color9, color8, color7, color6, colorbb, color5,
        color4, color3, color2, color1, color0, colorbo)
    print '|                    |   |                    |'
    print '|                    |   |                    |'
    print '|  {}  {}  {}  {}  {}  {}  | {} |  {}  {}  {}  {}  {}  {}  | {}'.format(
        tens12, tens13, tens14, tens15, tens16, tens17, tenswb, tens18, tens19,
        tens20, tens21, tens22, tens23, tenswo)
    print '|  {}  {}  {}  {}  {}  {}  | {} |  {}  {}  {}  {}  {}  {}  | {}'.format(
        ones12, ones13, ones14, ones15, ones16, ones17, oneswb, ones18, ones19,
        ones20, ones21, ones22, ones23, oneswo)
    print '|  {}  {}  {}  {}  {}  {}  | {} |  {}  {}  {}  {}  {}  {}  | {}'.format(
        color12, color13, color14, color15, color16, color17, colorwb, color18,
        color19, color20, color21, color22, color23, colorwo)
    print '|  .  .  .  .  .  .  |   |  .  .  .  .  .  .  |'
    print
Ejemplo n.º 13
0
 def test_get_initial_board(self):
     self.assertTrue(is_valid_board(get_initial_board()))