예제 #1
0
파일: test.py 프로젝트: cbw36/Tic-Tac-Toe
    def test_minimax(self):
        # Check if chooses move to win
        player = ComputerPlayer(0, "cpu", "X")
        board = [["X", "-", "O"], ["X", "-", "O"], ["-", "-", "-"]]
        depth = 5
        best_move = player.minimax(board=board,
                                   depth=depth,
                                   maximizing=True,
                                   letter=player.letter,
                                   alpha=-100,
                                   beta=100)
        self.assertEqual(best_move, [2, 0, WINNER],
                         "Doesn't chose move that will win the game")

        # Checks if chooses move to prevent loss and win
        player = ComputerPlayer(0, "cpu", "X")
        board = [["-", "-", "X"], ["-", "O", "-"], ["X", "-", "O"]]
        depth = 5
        best_move = player.minimax(board=board,
                                   depth=depth,
                                   maximizing=True,
                                   letter=player.letter,
                                   alpha=-100,
                                   beta=100)
        row, col = best_move[0], best_move[1]
        self.assertEqual([row, col], [0, 0],
                         "Doesn't chose move that prevent a loss and win")

        # Checks if chooses move to prevent loss and tie
        player = ComputerPlayer(0, "cpu", "X")
        board = [["-", "-", "-"], ["-", "O", "X"], ["-", "X", "O"]]
        depth = 5
        best_move = player.minimax(board=board,
                                   depth=depth,
                                   maximizing=True,
                                   letter=player.letter,
                                   alpha=-100,
                                   beta=100)
        row, col = best_move[0], best_move[1]
        self.assertEqual([row, col], [0, 0],
                         "Doesn't chose move that prevent a loss")

        # Check if can see a few moves ahead and prevent opponent from forcing a victory
        player = ComputerPlayer(1, "cpu", "O")
        board = [["-", "-", "X"], ["-", "O", "-"], ["X", "-", "-"]]
        depth = 5
        best_move = player.minimax(board=board,
                                   depth=depth,
                                   maximizing=True,
                                   letter=player.letter,
                                   alpha=-100,
                                   beta=100)
        row, col = best_move[0], best_move[1]
        self.assertNotEqual(
            [row, col], [0, 0],
            "Chose move that allows opponent to force a victory")
        self.assertNotEqual(
            [row, col], [2, 2],
            "Chose move that allows opponent to force a victory")