예제 #1
0
    def test_first_moves_6_4(self):
        board = KalahBoard(6, 4)

        self.assertEqual(board.get_board(),
                         [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0])
        self.assertEqual(board.current_player(), 0)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [0, 0])
        self.assertEqual(board.allowed_moves(), [0, 1, 2, 3, 4, 5])

        self.assertEqual(board.move(6), False)
        self.assertEqual(board.move(7), False)
        self.assertEqual(board.move(13), False)
        self.assertEqual(board.move(123), False)

        self.assertEqual(board.move(0), True)

        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [0, 0])
        self.assertEqual(board.allowed_moves(), [7, 8, 9, 10, 11, 12])

        self.assertEqual(board.move(7), True)

        self.assertEqual(board.current_player(), 0)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [0, 0])
        self.assertEqual(board.allowed_moves(), [1, 2, 3, 4, 5])
예제 #2
0
    def test_move_into_house_6_4(self):
        board = KalahBoard(6, 4)

        self.assertEqual(board.get_board(),
                         [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0])
        self.assertEqual(board.current_player(), 0)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [0, 0])
        self.assertEqual(board.allowed_moves(), [0, 1, 2, 3, 4, 5])

        self.assertEqual(board.move(2), True)

        self.assertEqual(board.get_board(),
                         [4, 4, 0, 5, 5, 5, 1, 4, 4, 4, 4, 4, 4, 0])
        self.assertEqual(board.current_player(), 0)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [1, 0])
        self.assertEqual(board.allowed_moves(), [0, 1, 3, 4, 5])

        self.assertEqual(board.move(2), False)
        self.assertEqual(board.move(1), True)

        self.assertEqual(board.get_board(),
                         [4, 0, 1, 6, 6, 6, 1, 4, 4, 4, 4, 4, 4, 0])
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [1, 0])
        self.assertEqual(board.allowed_moves(), [7, 8, 9, 10, 11, 12])
예제 #3
0
    def test_end_game_collect_all_remaining_seeds_second_test_6_4(self):
        board = KalahBoard(6, 4)

        board.set_board([0, 0, 0, 1, 1, 0, 24, 0, 0, 0, 0, 0, 1, 21])

        self.assertEqual(board.get_board(),
                         [0, 0, 0, 1, 1, 0, 24, 0, 0, 0, 0, 0, 1, 21])
        self.assertEqual(board.current_player(), 0)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [24, 21])
        self.assertEqual(board.allowed_moves(), [3, 4])

        self.assertEqual(board.move(4), True)

        self.assertEqual(board.get_board(),
                         [0, 0, 0, 1, 0, 1, 24, 0, 0, 0, 0, 0, 1, 21])
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [24, 21])
        self.assertEqual(board.allowed_moves(), [12])

        self.assertEqual(board.move(12), True)

        self.assertEqual(board.get_board(),
                         [0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 22])
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), True)
        self.assertEqual(board.score(), [26, 22])
        self.assertEqual(board.allowed_moves(), [])
예제 #4
0
    def test_end_game_collect_all_remaining_seeds_6_4(self):
        board = KalahBoard(6, 4)

        board.set_board([0, 0, 1, 1, 0, 1, 30, 0, 0, 0, 0, 1, 0, 14])

        self.assertEqual(board.get_board(),
                         [0, 0, 1, 1, 0, 1, 30, 0, 0, 0, 0, 1, 0, 14])
        self.assertEqual(board.current_player(), 0)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [30, 14])
        self.assertEqual(board.allowed_moves(), [2, 3, 5])

        self.assertEqual(board.move(2), True)

        self.assertEqual(board.get_board(),
                         [0, 0, 0, 2, 0, 1, 30, 0, 0, 0, 0, 1, 0, 14])
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [30, 14])
        self.assertEqual(board.allowed_moves(), [11])

        self.assertEqual(board.move(11), True)

        self.assertEqual(board.get_board(),
                         [0, 0, 0, 2, 0, 1, 30, 0, 0, 0, 0, 0, 1, 14])
        self.assertEqual(board.current_player(), 0)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [30, 14])
        self.assertEqual(board.allowed_moves(), [3, 5])

        self.assertEqual(board.move(3), True)

        self.assertEqual(board.get_board(),
                         [0, 0, 0, 0, 1, 2, 30, 0, 0, 0, 0, 0, 1, 14])
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [30, 14])
        self.assertEqual(board.allowed_moves(), [12])

        self.assertEqual(board.move(12), True)

        self.assertEqual(board.get_board(),
                         [0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 0, 0, 0, 15])
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), True)
        self.assertEqual(board.score(), [33, 15])
        self.assertEqual(board.allowed_moves(), [])
예제 #5
0
    def test_end_game_collect_all_remaining_seeds_third_test_2_2(self):
        board = KalahBoard(2, 2)

        board.set_board([0, 3, 1, 2, 2, 0])

        self.assertEqual(board.get_board(), [0, 3, 1, 2, 2, 0])
        self.assertEqual(board.current_player(), 0)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [1, 0])
        self.assertEqual(board.allowed_moves(), [1])

        self.assertEqual(board.move(1), True)

        self.assertEqual(board.get_board(), [0, 0, 2, 0, 0, 6])
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), True)
        self.assertEqual(board.score(), [2, 6])
        self.assertEqual(board.allowed_moves(), [])
예제 #6
0
    def get_next_move(self, board: KalahBoard):
        house_id = board.get_house_id(board.current_player())

        for mv in sorted(board.allowed_moves(), reverse=True):
            seeds = board.get_board()[mv]
            if (house_id - mv) == seeds:
                return mv

        return self.random.choice(utils.get_best_moves(board))
예제 #7
0
    def test_empty_pit_opposite_no_empty_capture_4_4(self):
        # We do not have the "empty capture" rule
        board = KalahBoard(4, 4)

        board.set_board([1, 0, 4, 4, 7, 4, 0, 4, 4, 4])

        self.assertEqual(board.get_board(), [1, 0, 4, 4, 7, 4, 0, 4, 4, 4])
        self.assertEqual(board.current_player(), 0)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [7, 4])
        self.assertEqual(board.allowed_moves(), [0, 2, 3])

        self.assertEqual(board.move(0), True)

        self.assertEqual(board.get_board(), [0, 1, 4, 4, 7, 4, 0, 4, 4, 4])
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [7, 4])
        self.assertEqual(board.allowed_moves(), [5, 7, 8])
예제 #8
0
    def test_get_best_moves(self):
        board = KalahBoard(4, 4)
        self.assertEqual(board.get_board(), [4, 4, 4, 4, 0, 4, 4, 4, 4, 0])
        self.assertEqual(board.allowed_moves(), [0, 1, 2, 3])

        self.assertEqual(utils.get_best_moves(board), [0, 1, 2, 3])

        board.set_board([4, 0, 5, 5, 1, 5, 4, 4, 4, 0])
        self.assertEqual(board.allowed_moves(), [0, 2, 3])
        self.assertEqual(utils.get_best_moves(board), [0, 2, 3])

        board.set_board([4, 0, 5, 5, 1, 5, 4, 4, 4, 0])
        self.assertEqual(board.allowed_moves(), [0, 2, 3])
        self.assertEqual(utils.get_best_moves(board), [0, 2, 3])

        board.set_current_player(1)
        board.set_board([4, 0, 5, 5, 1, 5, 4, 4, 4, 0])
        self.assertEqual(board.allowed_moves(), [5, 6, 7, 8])
        self.assertEqual(utils.get_best_moves(board), [5, 6, 7, 8])
예제 #9
0
    def test_randomagent(self):
        board = KalahBoard(6,4)
        self.assertEqual(board.get_board(), [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0])
        self.assertEqual(board.allowed_moves(), [0, 1, 2, 3, 4, 5])

        # We set a fixed random seed and take three known results as a test
        agent = RandomAgent(5)

        self.assertEqual(agent.get_next_move(board), 4)
        self.assertEqual(agent.get_next_move(board), 2)
        self.assertEqual(agent.get_next_move(board), 5)
예제 #10
0
    def test_moves_6_4(self):
        board = KalahBoard(6, 4)
        board.set_board([0, 0, 0, 0, 0, 1, 24, 0, 0, 0, 2, 0, 0, 21])
        board.set_current_player(1)

        self.assertEqual(board.get_board(),
                         [0, 0, 0, 0, 0, 1, 24, 0, 0, 0, 2, 0, 0, 21])
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [24, 21])
        self.assertEqual(board.allowed_moves(), [10])

        self.assertEqual(board.move(10), True)

        self.assertEqual(board.get_board(),
                         [0, 0, 0, 0, 0, 1, 24, 0, 0, 0, 0, 1, 1, 21])
        self.assertEqual(board.current_player(), 0)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [24, 21])
        self.assertEqual(board.allowed_moves(), [5])

        initial_board = [4, 4, 4, 4, 4, 0, 1, 5, 5, 5, 4, 4, 4, 0]
        board = KalahBoard(6, 4)
        board.set_board(initial_board)
        board.set_current_player(1)

        self.assertEqual(board.get_board(), initial_board)
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [1, 0])
        self.assertEqual(board.allowed_moves(), [7, 8, 9, 10, 11, 12])

        self.assertEqual(board.move(8), True)

        self.assertEqual(board.get_board(),
                         [4, 4, 4, 4, 4, 0, 1, 5, 0, 6, 5, 5, 5, 1])
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [1, 1])
        self.assertEqual(board.allowed_moves(), [7, 9, 10, 11, 12])
예제 #11
0
def get_best_moves(board: KalahBoard):
    best_moves = []
    best_score_gain = -1

    for mv in board.allowed_moves():
        score_gain = _get_score_gain(board, board.current_player(), mv)
        if score_gain > best_score_gain:
            best_moves = [mv]
            best_score_gain = score_gain
        elif score_gain == best_score_gain:
            best_moves.append(mv)

    return best_moves
예제 #12
0
    def test_empty_pit_capture_4_4(self):
        # Test for player 1
        board = KalahBoard(4, 4)
        board.set_current_player(0)
        board.set_board([1, 0, 4, 4, 7, 4, 4, 4, 4, 0])

        self.assertEqual(board.get_board(), [1, 0, 4, 4, 7, 4, 4, 4, 4, 0])
        self.assertEqual(board.current_player(), 0)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [7, 0])
        self.assertEqual(board.allowed_moves(), [0, 2, 3])

        self.assertEqual(board.move(0), True)

        self.assertEqual(board.get_board(), [0, 0, 4, 4, 12, 4, 0, 4, 4, 0])
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [12, 0])
        self.assertEqual(board.allowed_moves(), [5, 7, 8])

        # Test for player 2
        board = KalahBoard(4, 4)
        board.set_current_player(1)
        board.set_board([4, 0, 5, 5, 1, 5, 4, 4, 4, 0])

        self.assertEqual(board.get_board(), [4, 0, 5, 5, 1, 5, 4, 4, 4, 0])
        self.assertEqual(board.current_player(), 1)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [1, 0])
        self.assertEqual(board.allowed_moves(), [5, 6, 7, 8])

        self.assertEqual(board.move(7), True)

        self.assertEqual(board.get_board(), [5, 1, 5, 5, 1, 5, 4, 0, 5, 1])
        self.assertEqual(board.current_player(), 0)
        self.assertEqual(board.game_over(), False)
        self.assertEqual(board.score(), [1, 1])
        self.assertEqual(board.allowed_moves(), [0, 1, 2, 3])
예제 #13
0
 def get_next_move(self, board: KalahBoard):
     return self.random.choice(board.allowed_moves())