def __init__(self, p1=None, p2=None):
     self._board = BoardDrop(ConnectFour._COLUMN, ConnectFour._ROW)
     self._p1 = p1 if not p1 is None else Player("Player 1", Token.A)
     self._p2 = p2 if not p2 is None else Player("Player 2", Token.B)
     self._current_player = self._p1
     self._winner_player = None
     self._is_over = False
     self._history = list()
     self._moves = [i for i in range(ConnectFour._COLUMN)]
     self._patterns = [np.full(4, token) for token in [Token.A, Token.B]]
    def test_pos(self):
        p1 = Player("AI_1", Token.A, True)
        p2 = Player("AI_2", Token.B, True)
        game = ConnectFour(p1=p1, p2=p2)

        moves = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 2, 1, 2]
        for m in moves:
            game.play(m)

        self.assertEqual(game.current_player, p1)

        depth = 2
        ai = Minmax_AB_Parallel(p1, depth)

        self.assertEqual(ai.compute(game), 3)
    def test_evaluate_d1(self):
        '''
        X|X|X
        O|O|-
        -|-|-
        '''
        ai_player = Player("AI_1", Token.A, True)
        game = TicTacToe(p1=ai_player)
        minmax = Minmax(ai_player, 7)
        depth = 10

        self.assertEqual(minmax._evaluate(game, depth, Token.A),
                         Minmax.DRAW_POINT)

        self.assertEqual(minmax._evaluate(game, depth, Token.B),
                         Minmax.DRAW_POINT)

        game.play(Point(0, 0))
        game.play(Point(0, 1))
        game.play(Point(1, 0))
        game.play(Point(1, 1))
        game.play(Point(2, 0))

        self.assertEqual(minmax._evaluate(game, depth, Token.A),
                         Minmax.WIN_POINT + depth)

        self.assertEqual(minmax._evaluate(game, depth, Token.B),
                         Minmax.LOOSE_POINT - depth)
    def test_minmax_d2(self):
        ''' 
        O|-|X
        X|X|O
        O|-|-
        '''
        ai_player = Player("AI_1", Token.A, True)
        game = TicTacToe(p1=ai_player)
        depth = 2
        minmax = Minmax(ai_player, depth)

        game.play(Point(2, 0))
        game.play(Point(0, 0))
        game.play(Point(0, 1))
        game.play(Point(0, 2))
        game.play(Point(1, 1))
        game.play(Point(2, 1))

        game.play(Point(1, 0))
        val, _ = minmax._min(game, depth)
        self.assertEqual(val, Minmax.DRAW_POINT)
        game.undo()

        game.play(Point(1, 2))
        val, _ = minmax._min(game, depth)
        self.assertEqual(val, Minmax.DRAW_POINT)
        game.undo()
Exemple #5
0
 def __init__(self, p1=None, p2=None, size=9, line_win_size=5):
     self._board = Board(size, size)
     self._line_win_size = line_win_size
     self._p1 = p1 if not p1 is None else Player("Player 1", Token.A)
     self._p2 = p2 if not p2 is None else Player("Player 2", Token.B)
     assert (not (self._p1.token == self._p2.token))
     self._current_player = self._p1
     self._winner_player = None
     self._is_over = False
     self._history = list()
     self._moves_remaining = set(
         [Point(x, y) for y in range(size) for x in range(size)])
     self._patterns = [
         np.full(self._line_win_size, token)
         for token in [Token.A, Token.B]
     ]
    def test_gomoku(self):
        from game.gomoku import Gomoku

        n = 2
        depth = 4
        p1 = Player("AI_1", Token.A, True)
        duration = 0
        for i in range(n):
            game = Gomoku(p1=p1, size=9)
            minmax = Minmax_AB_Parallel(p1, depth)

            moves = [
                Point(4, 4),
                Point(3, 3),
                Point(4, 3),
                Point(3, 4),
                Point(3, 2),
                Point(4, 5)
            ]
            for m in moves:
                game.play(m)

            start = time.time()
            minmax.compute(game)
            duration += time.time() - start

        print('Duration {}'.format(duration / n))
        r = expected['gomoku']
        delta = r * 3 / 100
        self.assertAlmostEqual(duration / n, r, delta=delta)
    def test_minmax_d1(self):
        '''
        XX-
        OO-
        XO-
        Simulate max loop and calling min evaluation
        '''
        ai_player = Player("AI_1", Token.A, True)
        game = TicTacToe(p1=ai_player)
        depth = 1
        minmax = Minmax(ai_player, depth)

        game.play(Point(0, 0))
        game.play(Point(0, 1))
        game.play(Point(1, 0))
        game.play(Point(1, 1))
        game.play(Point(0, 2))
        game.play(Point(1, 2))

        game.play(Point(2, 0))
        val, _ = minmax._min(game, depth)
        self.assertEqual(val, Minmax.WIN_POINT + depth)
        game.undo()

        game.play(Point(2, 1))
        val, _ = minmax._min(game, depth)
        self.assertEqual(val, Minmax.DRAW_POINT)
        game.undo()

        game.play(Point(2, 2))
        val, _ = minmax._min(game, depth)
        self.assertEqual(val, Minmax.LOOSE_POINT)
        game.undo()
Exemple #8
0
    def test_print_and_export(self):
        p1 = Player("AI_1", Token.A, True)
        minmax = Minmax_AlphaBeta_Tree(p1, 3)
        game = ConnectFour(p1=p1)
        minmax.compute(game)

        #         print(minmax)
        minmax.to_picture()
Exemple #9
0
    def test_export_json(self):

        depth = 4
        p1 = Player("AI_1", Token.A, True)
        minmax = Minmax_AlphaBeta_Tree(p1, depth)
        game = ConnectFour(p1=p1)
        minmax.compute(game)

        minmax.to_json("minmax" + str(depth) + ".json")
    def test_deepcopy(self):
        depth = 4
        p1 = Player("AI_1", Token.A, True)
        ai = Minmax_AB_Parallel(p1, depth)

        cai = copy.deepcopy(ai)

        self.assertFalse(ai is cai)
        self.assertFalse(ai._player is cai._player)
        ai._depth = 5
        self.assertFalse(ai._depth is cai._depth)
        self.assertNotEqual(ai._depth, cai._depth)
    def test_pos3(self):
        '''
          0 1 2 3 4 5 6
        0|X|X|-|-|-|-|-|
        1|X|O|-|-|-|-|-|
        2|O|X|-|-|-|-|-|
        3|X|O|-|-|-|-|-|
        4|O|X|O|-|-|-|-|
        5|X|O|O|-|-|-|-|
        Player_1(X) to play
        '''
        p1 = Player("AI_1", Token.A, True)
        p2 = Player("AI_2", Token.B, True)
        game = ConnectFour(p1=p1, p2=p2)

        moves = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 2, 1, 2]
        for m in moves:
            game.play(m)

        depth = 2
        ai = Minmax_AB_Parallel(p1, depth)

        self.assertEqual(ai.compute(game), 3)
Exemple #12
0
    def test_is_leaf(self):
        ai_player = Player("AI_1", Token.A, True)
        game = TicTacToe(p1=ai_player)
        minmax = Minmax(ai_player, 7)

        self.assertFalse(minmax._is_leaf(game, 1))
        self.assertTrue(minmax._is_leaf(game, 0))

        game.play(Point(0, 0))
        game.play(Point(0, 1))
        game.play(Point(1, 0))
        game.play(Point(1, 1))
        game.play(Point(2, 0))
        self.assertTrue(minmax._is_leaf(game, 1))
        self.assertTrue(minmax._is_leaf(game, 0))
Exemple #13
0
def make_player(mode):

    p1, p2 = None, None

    if mode == 'H_H':
        p1 = Player("Human_1", Token.A)
        p2 = Player("Human_2", Token.B)
    elif mode == 'AI_H':
        p1 = Player("AI_1", Token.A, True)
        p2 = Player("Human_2", Token.B)
    elif mode == 'H_AI':
        p1 = Player("Human_1", Token.A)
        p2 = Player("AI_2", Token.B, True)
    elif mode == 'AI_AI':
        p1 = Player("AI_1", Token.A, True)
        p2 = Player("AI_2", Token.B, True)
    else:
        assert (False)

    return p1, p2
Exemple #14
0
    def test_deepcopy(self):
        name = "Player 1"
        token = "X"
        p = Player(name, token)
        pp = p
        cp = copy.deepcopy(p)

        self.assertEqual(p.name, cp.name)
        self.assertEqual(p.token, cp.token)
        self.assertTrue(isinstance(cp, Player))
        self.assertFalse(p is cp)
        self.assertTrue(p is pp)

        # Update attributes values so 'is' return False
        p._name = "Human"
        p._token = 'o'

        self.assertFalse(p.name is cp.name)
        self.assertFalse(p.token is cp.token)
    def test_pos2(self):
        '''
          0 1 2 3 4 5 6 
        0|X|-|-|-|-|-|-|
        1|O|-|O|-|-|-|-|
        2|X|-|X|O|-|-|-|
        3|O|O|O|X|X|-|-|
        4|O|X|X|X|O|-|-|
        5|O|X|O|X|X|-|-|
        '''
        p2 = Player("AI_2", Token.B, True)
        game = ConnectFour(p2=p2)

        moves = [
            3, 0, 4, 2, 3, 0, 2, 0, 0, 4, 1, 2, 3, 3, 1, 1, 2, 0, 0, 2, 4, 0,
            2, 4
        ]
        for move in moves:
            game.play(move)

        # test p1 win position
        import numpy as np
        self.assertTrue(
            np.array_equal(game._board.get_diag_up(4, 2),
                           [1, 1, 1, 1, None, None]))
        self.assertTrue(game.is_over)
        game.undo()
        game.undo()

        # test p1 win in (1,1)
        self.assertEqual(game.current_player, p2)
        game.play(1)  # O
        game.play(1)  # X
        self.assertTrue(game.is_over)

        # p2 turn, should block (4,3)
        game.undo()
        game.undo()
        self.assertEqual(game.current_player, p2)
        depth = 2
        minmax = Minmax_AB(p2, depth)
        self.assertEqual(minmax.compute(game), 4)
    def test_tictactoe(self):
        from game.tictactoe import TicTacToe

        n = 10
        depth = 9
        duration = 0
        p1 = Player("AI_1", Token.A, True)
        for i in range(n):
            minmax = Minmax_AB_Parallel(p1, depth)
            game = TicTacToe(p1=p1)

            start = time.time()
            minmax.compute(game)
            duration += time.time() - start

        print("Duration {}".format(duration / n))

        r = expected['tictactoe']
        delta = r * 3 / 100
        self.assertAlmostEqual(duration / n, r, delta=delta)
    def test_connect_four(self):

        from game.connect_four import ConnectFour

        n = 20
        depth = 9
        p1 = Player("AI_1", Token.A, True)
        duration = 0
        for i in range(n):
            minmax = Minmax_AB_Parallel(p1, depth)
            game = ConnectFour(p1=p1)
            for m in [3, 4, 3, 4, 3, 4, 0]:
                game.play(m)

            start = time.time()
            minmax.compute(game)
            duration += time.time() - start

        print("Duration {}".format(duration / n))
        r = expected['connect_four']
        delta = r * 3 / 100
        self.assertAlmostEqual(duration / n, r, delta=delta)
def main():
    pr = cProfile.Profile()

    depth = 8
    p1 = Player("AI_1", Token.A, True)
    game = ConnectFour(p1=p1)
#     minmax = Minmax_AB(p1, depth)
    minmax = Minmax_AB_Parallel(p1, depth)

    moves = [0, 1, 0, 1, 5, 6, 5, 6]
    for m in moves:
        game.play(m)

    pr.enable()
    minmax.compute(game)
    pr.disable()

    # Construct stats
    ps = pstats.Stats(pr)
    ps.strip_dirs()
#     ps.sort_stats(SortKey.CUMULATIVE)
    ps.sort_stats('tottime')
    ps.print_stats()
    def test_pos1(self):
        '''
          0 1 2 3 4 5 6 
        0|-|-|-|-|-|-|-|
        1|-|-|-|-|-|-|-|
        2|X|-|O|-|O|-|-|
        3|O|-|X|X|X|-|-|
        4|O|-|O|X|X|X|O|
        5|O|-|O|X|X|X|O|
        '''
        p2 = Player("AI_2", Token.B, True)
        game = ConnectFour(p2=p2)

        moves = [3, 0, 4, 2, 5, 6, 4, 0, 3, 0, 0, 2, 2, 2, 5, 6, 4, 4, 3]
        for move in moves:
            game.play(move)

        depth = 1
        minmax = Minmax_AB(p2, depth)
        self.assertEqual(minmax.compute(game), 0)

        depth = 2
        minmax = Minmax_AB(p2, depth)
        self.assertEqual(minmax.compute(game), 0)
 def test_tictactoe(self):
     ai_player = Player("AI_1", Token.A, True)
     game = TicTacToe(p1=ai_player)
     depth = 2
     minmax = Minmax_AB_Parallel(ai_player, depth)
     minmax.compute(game)
 def test_connect_four(self):
     ai_player = Player("AI_1", Token.A, True)
     game = ConnectFour(p1=ai_player)
     depth = 2
     minmax = Minmax_AB_Parallel(ai_player, depth)
     minmax.compute(game)
 def test_gomoku(self):
     ai_player = Player("AI_1", Token.A, True)
     game = Gomoku(p1=ai_player)
     depth = 2
     minmax = Minmax_AB_Parallel(ai_player, depth)
     minmax.compute(game)
Exemple #23
0
 def test_name(self):
     name = "Player 1"
     token = "X"
     player = Player(name, token)
     self.assertEqual(player.name, name)
     self.assertEqual(player.token, token)