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))
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))
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)
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))
def do_white_turn(board): roll = roll_dice() next_boards = generate_next_boards(board, False, roll) for i, next_board in enumerate(next_boards): print print '{}.'.format(i) visualize_board(next_board) print print '--------------' print print 'White rolls {}'.format(roll) print print 'Current board:' visualize_board(board) choice = raw_input("Enter move (see choices above): ") if choice: try: choice = int(choice) except ValueError: choice = None if choice is not None and 0 <= choice < len(next_boards): next_board = next_boards[choice] else: next_board = random.choice(next_boards) print print visualize_board(next_board) print return next_board, (black_wins(next_board) or white_wins(next_board))
def test_no_move(self): board = get_blank_board() board[1] = (0, 15) board[2] = (15, 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][1], (0, 15)) self.assertEqual(next_boards[0][2], (15, 0))
def test_black_hit(self): board = get_blank_board() board[1] = (0, 1) board[2] = (15, 0) board[3] = (0, 14) roll = [1, 1, 1, 1] next_boards = generate_next_boards(board, True, roll) self.assertEqual(len(next_boards), 4) for next_board in next_boards: self.assertEqual(next_board[WHITE_BAR_INDEX], (0, 1))
def test_white_hit(self): board = get_blank_board() board[23] = (1, 0) board[22] = (0, 15) board[3] = (14, 0) roll = [1, 1, 1, 1] next_boards = generate_next_boards(board, False, roll) self.assertEqual(len(next_boards), 3) for next_board in next_boards: self.assertEqual(next_board[BLACK_BAR_INDEX], (1, 0))
def test_hit_from_bear_on_black(self): board = get_blank_board() board[BLACK_BAR_INDEX] = (15, 0) board[23] = (0, 1) board[0] = (0, 14) roll = [1, 1, 1, 1] next_boards = generate_next_boards(board, True, roll) board[BLACK_BAR_INDEX] = (11, 0) board[23] = (4, 0) board[WHITE_BAR_INDEX] = (0, 1) self.assertIn(board, next_boards)
def test_hit_from_bear_on_white(self): board = get_blank_board() board[WHITE_BAR_INDEX] = (0, 15) board[1] = (1, 0) board[3] = (14, 0) roll = [2, 2, 2, 2] next_boards = generate_next_boards(board, False, roll) board[WHITE_BAR_INDEX] = (0, 11) board[1] = (0, 4) board[BLACK_BAR_INDEX] = (1, 0) self.assertIn(board, next_boards)
def do_black_turn(board): roll = roll_dice() next_boards = generate_next_boards(board, True, roll) print '--------------' print print 'Black rolls {}'.format(roll) print print 'Current board:' next_board = random.choice(next_boards) visualize_board(next_board) raw_input('Press any key...') print return next_board, (black_wins(next_board) or white_wins(next_board))
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))
def test_use_larger(self): board = get_blank_board() board[11] = (2, 0) board[10] = (2, 0) board[9] = (2, 0) board[8] = (2, 0) board[7] = (2, 0) board[6] = (3, 0) board[5] = (0, 14) board[4] = (1, 0) board[3] = (1, 0) board[1] = (0, 1) roll = [2, 3] next_boards = generate_next_boards(board, False, roll) self.assertEqual(len(next_boards), 1) board[1] = (0, 0) board[4] = (0, 1) board[BLACK_BAR_INDEX] = (1, 0) self.assertIn(board, next_boards)