コード例 #1
0
    def test_example_3(self):
        """"
        0 0 0 0 0 0 0
        0 0 0 0 0 0 0
        0 0 1 0 0 0 0
        0 0 1 2 2 0 0
        0 0 2 2 1 0 0
        0 2 2 2 2 1 0
        """

        board = Board()
        _fill_board(board, [(2, 2), (3, 2), (4, 4), (5, 5)], Player.AI)
        _fill_board(board, [(3, 4), (3, 4), (4, 2), (4, 3), (5, 2), (5, 3),
                            (5, 4), (5, 1)], Player.HUMAN)

        self.assertFalse(board.check_board_state(Player.AI))

        self.assertTrue(board.check_board_state(Player.HUMAN))
コード例 #2
0
    def test_example_5(self):
        """"
        0 0 0 0 0 0 0
        0 0 0 1 0 0 0
        0 0 1 2 0 2 0
        0 1 1 2 0 0 0
        1 2 2 2 0 0 0
        2 2 1 2 0 1 0
        """

        board = Board()
        _fill_board(board, [(1, 3), (2, 2), (3, 1), (4, 0), (3, 2), (5, 2),
                            (5, 5), (2, 3), (4, 5)], Player.AI)
        _fill_board(board, [(2, 5), (3, 3), (4, 1), (4, 2), (4, 3), (5, 0),
                            (5, 1), (5, 3), (3, 5)], Player.HUMAN)

        self.assertTrue(board.check_board_state(Player.AI))

        self.assertFalse(board.check_board_state(Player.HUMAN))
コード例 #3
0
class Game:
    def __init__(self,
                 algorithm_params=dict(),
                 mode='Human vs AI',
                 algorithm='Minmax'):
        self._board = Board()
        if algorithm == 'Minmax':
            self._algorithm = MiniMax(Player.AI, Player.HUMAN, heuristic,
                                      **algorithm_params)
        elif algorithm == 'Alpha-beta pruning':
            self._algorithm = MiniMax(Player.AI,
                                      Player.HUMAN,
                                      heuristic,
                                      alpha_beta=True,
                                      **algorithm_params)
        elif algorithm == 'MCTS':
            self._algorithm = MCTS(1000, 2)
        else:
            raise ValueError(f"Algorithm {algorithm} not known")

        self._last_move = None
        if mode == 'AI vs AI':
            self._second_algo = MiniMax(Player.HUMAN, Player.AI, heuristic,
                                        **algorithm_params)

        self._start_time = None
        self._total_time = None

    def ai_move(self):
        column = self._algorithm(self._board)
        self._last_move = self._board.drop_coin(column, Player.AI)

    def player_move(self, column):
        if self._start_time is None:
            self._start_time = time()

        if column is None:
            column = self._second_algo(self._board)

        self._last_move = self._board.drop_coin(column, Player.HUMAN)

    def check_end(self):
        if self._last_move is None:
            return False

        end = self._board.check_board_state(*self._last_move)
        return end
コード例 #4
0
    def test_not_win_first_player(self):
        board = Board()
        _fill_board(board, [(3, 2), (3, 3), (3, 4), (3, 6)], Player.AI)

        self.assertFalse(board.check_board_state(Player.AI))
コード例 #5
0
    def test_win_diagonal(self):
        board = Board()
        _fill_board(board, [(1, 1), (2, 2), (3, 3), (4, 4)], Player.AI)

        self.assertTrue(board.check_board_state(Player.AI))
コード例 #6
0
    def test_win_col(self):
        board = Board()
        _fill_board(board, [(0, 2), (1, 2), (2, 2), (3, 2)], Player.AI)

        self.assertTrue(board.check_board_state(Player.AI))
コード例 #7
0
    def test_win_row(self):
        board = Board()
        _fill_board(board, [(3, 2), (3, 3), (3, 4), (3, 5)], Player.AI)

        self.assertTrue(board.check_board_state(5, 5))
コード例 #8
0
 def test_empty(self):
     board = Board()
     self.assertFalse(board.check_board_state(0, 0))