Example #1
0
    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()
Example #2
0
    def test_win_vertical_player1(self):
        game = TicTacToe()
        self.assertTrue(game.play(Point(0, 0)))
        self.assertTrue(game.play(Point(1, 0)))
        self.assertTrue(game.play(Point(0, 1)))
        self.assertTrue(game.play(Point(1, 1)))
        self.assertTrue(game.play(Point(0, 2)))

        self.assertTrue(game.is_over)
        self.assertEqual(game.winner, game._p1)
Example #3
0
    def test_win_diag_up_player2(self):
        game = TicTacToe()
        self.assertTrue(game.play(Point(0, 0)))
        self.assertTrue(game.play(Point(0, 2)))
        self.assertTrue(game.play(Point(1, 0)))
        self.assertTrue(game.play(Point(1, 1)))
        self.assertTrue(game.play(Point(0, 1)))
        self.assertTrue(game.play(Point(2, 0)))

        self.assertTrue(game.is_over)
        self.assertEqual(game.winner, game._p2)
Example #4
0
    def test_next_player(self):
        game = TicTacToe()

        self.assertEqual(game._current_player, game._p1)
        self.assertTrue(game.play(Point(0, 0)))
        self.assertEqual(game._current_player, game._p2)
        self.assertTrue(game.play(Point(1, 0)))
        self.assertEqual(game._current_player, game._p1)

        # Cell not free, do not change current player
        self.assertFalse(game.play(Point(1, 0)))
        self.assertEqual(game._current_player, game._p1)
Example #5
0
    def test_draw(self):
        game = TicTacToe()
        self.assertTrue(game.play(Point(0, 0)))
        self.assertTrue(game.play(Point(1, 0)))
        self.assertTrue(game.play(Point(2, 0)))
        self.assertTrue(game.play(Point(0, 1)))
        self.assertTrue(game.play(Point(1, 1)))
        self.assertTrue(game.play(Point(2, 2)))
        self.assertTrue(game.play(Point(1, 2)))
        self.assertTrue(game.play(Point(0, 2)))
        self.assertTrue(game.play(Point(2, 1)))

        self.assertTrue(game.is_over)
        self.assertEqual(game.winner, None)
Example #6
0
    def test_win_diag_up_p1(self):
        '''
          0 1 2
        0|X|O|X|
        1|O|X|O|
        2|X|-|-|
        '''
        game = TicTacToe()
        self.assertTrue(game.play(Point(0, 0)))
        self.assertTrue(game.play(Point(1, 0)))
        self.assertTrue(game.play(Point(2, 0)))
        self.assertTrue(game.play(Point(0, 1)))
        self.assertTrue(game.play(Point(1, 1)))
        self.assertTrue(game.play(Point(2, 1)))
        self.assertTrue(game.play(Point(0, 2)))

        self.assertTrue(game.is_over)
        self.assertEqual(game.winner, game._p1)
Example #7
0
    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)
Example #8
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))
Example #9
0
 def test_compute_ending(self):
     game = TicTacToe()
     self.assertEqual(game._board.cell_used_count, 0)
     game.play(Point(0, 0))
     self.assertEqual(game._board.cell_used_count, 1)
Example #10
0
    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()