コード例 #1
0
    def test_create_a_game_and_public_interface(self):
        board = Board()
        game = Game(board=board, player_1='X', player_2='O')

        self.assertFalse(game.is_finished())
        self.assertFalse(game.has_winner())
        self.assertFalse(game.is_tied())
        self.assertIsNone(game.get_winner())

        self.assertEqual(game.next_turn(), 'X')

        self.assertEqual(game.board, board)
コード例 #2
0
    def test_game_is_tied(self):
        board = Board([
            ['O', 'X', 'X'],
            ['X', 'O', 'O'],
            ['X', 'O', 'X'],
        ])

        game = Game(board=board, player_1='X', player_2='O')

        self.assertTrue(game.is_finished())
        self.assertFalse(game.has_winner())
        self.assertTrue(game.is_tied())
        self.assertIsNone(game.get_winner())
コード例 #3
0
    def test_check_winner_returns_true_second_diagonal(self):
        board = Board([
            [None, None, 'X'],
            [None, 'X', None],
            ['X', 'O', 'O'],
        ])

        game = Game(board=board, player_1='X', player_2='O')

        self.assertTrue(game.is_finished())
        self.assertTrue(game.has_winner())
        self.assertFalse(game.is_tied())
        self.assertEqual(game.get_winner(), 'X')
コード例 #4
0
    def test_check_winner_returns_false(self):
        board = Board([
            [None, None, None],
            ['X', None, 'O'],
            [None, 'X', None],
        ])

        game = Game(board=board, player_1='X', player_2='O')

        self.assertFalse(game.is_finished())
        self.assertFalse(game.has_winner())
        self.assertFalse(game.is_tied())
        self.assertIsNone(game.get_winner())
コード例 #5
0
    def test_invalid_move_game_over_game_is_tied(self):
        board = Board([
            ['O', 'X', 'X'],
            ['X', 'O', 'O'],
            ['X', 'O', 'X'],
        ])
        game = Game(board=board, player_1='X', player_2='O')

        self.assertTrue(game.is_finished())
        self.assertFalse(game.has_winner())
        self.assertTrue(game.is_tied())
        self.assertIsNone(game.get_winner())

        with self.assertRaises(InvalidMovementException):
            game.move('X', row=0, col=0)
コード例 #6
0
    def test_invalid_move_game_has_winner(self):
        board = Board([
            ['X', None, None],
            ['O', 'X', None],
            [None, 'O', 'X'],
        ])

        game = Game(board=board, player_1='X', player_2='O')

        self.assertTrue(game.is_finished())
        self.assertTrue(game.has_winner())
        self.assertFalse(game.is_tied())
        self.assertEqual(game.get_winner(), 'X')

        self.assertEqual(game.next_turn(), 'X')
        with self.assertRaises(InvalidMovementException):
            game.move('X', row=0, col=2)
コード例 #7
0
ファイル: tests.py プロジェクト: carymrobbins/Tic-Tac-Toe
class TestGame(unittest.TestCase):
    def setUp(self):
        self.game = Game(Player.X)

    def test_initial_game(self):
        game = Game(Player.X)
        self.assertIs(game.human, Player.X)
        self.assertIs(game.computer, Player.O)
        self.assertEqual(game.spaces, [None] * 9)
        game = Game(Player.O)
        self.assertIs(game.human, Player.O)
        self.assertIs(game.computer, Player.X)
        self.assertEqual(game.spaces, [None] * 9)

    def test_move(self):
        self.game.move(Player.X, 0)
        self.assertIs(self.game.spaces[0], Player.X)

    def test_invalid_move(self):
        self.assertRaises(Game.InvalidMove, self.game.move, Player.O, 9)

    def test_already_occupied(self):
        self.game.move(Player.X, 0)
        self.assertRaises(Game.AlreadyOccupied, self.game.move, Player.O, 0)

    def test_get_player_spaces(self):
        self.game.move(Player.X, 0)
        self.assertEqual(self.game.get_player_spaces(Player.X),
                         frozenset([0]))
        self.game.move(Player.O, 1)
        self.assertEqual(self.game.get_player_spaces(Player.O),
                         frozenset([1]))
        self.game.move(Player.X, 2)
        self.assertEqual(self.game.get_player_spaces(Player.X),
                         frozenset([0, 2]))
        self.game.move(Player.O, 3)
        self.assertEqual(self.game.get_player_spaces(Player.O),
                         frozenset([1, 3]))

    def test_get_empty_spaces(self):
        self.game.move(Player.X, 0)
        self.assertEqual(self.game.get_empty_spaces(),
                         frozenset(i for i in range(9) if i not in range(1)))
        self.game.move(Player.O, 1)
        self.assertEqual(self.game.get_empty_spaces(),
                         frozenset(i for i in range(9) if i not in range(2)))
        self.game.move(Player.X, 2)
        self.assertEqual(self.game.get_empty_spaces(),
                         frozenset(i for i in range(9) if i not in range(3)))
        self.game.move(Player.O, 3)
        self.assertEqual(self.game.get_empty_spaces(),
                         frozenset(i for i in range(9) if i not in range(4)))

    def test_get_winner(self):
        self.game.move(Player.X, 0)
        self.assertIsNone(self.game.get_winner())
        self.game.move(Player.O, 1)
        self.assertIsNone(self.game.get_winner())
        self.game.move(Player.X, 3)
        self.assertIsNone(self.game.get_winner())
        self.game.move(Player.O, 4)
        self.assertIsNone(self.game.get_winner())
        self.game.move(Player.X, 6)
        self.assertIs(self.game.get_winner(), Player.X)
        self.game = Game(Player.X)
        self.game.move(Player.X, 0)
        self.game.move(Player.O, 2)
        self.game.move(Player.X, 3)
        self.game.move(Player.O, 4)
        self.game.move(Player.X, 5)
        self.game.move(Player.O, 6)
        self.assertIs(self.game.get_winner(), Player.O)

    def test_get_state(self):
        # Ensure state is incomplete until human wins.
        self.assertIs(self.game.get_state(), Game.State.Incomplete)
        self.game.move(Player.X, 0)
        self.assertIs(self.game.get_state(), Game.State.Incomplete)
        self.game.move(Player.O, 1)
        self.assertIs(self.game.get_state(), Game.State.Incomplete)
        self.game.move(Player.X, 3)
        self.assertIs(self.game.get_state(), Game.State.Incomplete)
        self.game.move(Player.O, 4)
        self.assertIs(self.game.get_state(), Game.State.Incomplete)
        self.game.move(Player.X, 6)
        self.assertIs(self.game.get_state(), Game.State.HumanWins)
        # Reset the game and check for computer wins.
        self.game = Game(Player.X)
        self.game.move(Player.X, 0)
        self.game.move(Player.O, 2)
        self.game.move(Player.X, 3)
        self.game.move(Player.O, 4)
        self.game.move(Player.X, 5)
        self.game.move(Player.O, 6)
        self.assertIs(self.game.get_state(), Game.State.ComputerWins)
        # Reset the game and check for tie.
        self.game = Game(Player.X)
        self.game.move(Player.X, 0)
        self.game.move(Player.O, 1)
        self.game.move(Player.X, 2)
        self.game.move(Player.X, 3)
        self.game.move(Player.O, 4)
        self.game.move(Player.X, 5)
        self.game.move(Player.O, 6)
        self.game.move(Player.X, 7)
        self.game.move(Player.O, 8)
        self.assertIs(self.game.get_state(), Game.State.Tie)

    def test_state_value(self):
        self.assertLess(Game.State.HumanWins.value, Game.State.Tie.value)
        self.assertLess(Game.State.Tie.value, Game.State.ComputerWins.value)

    def test_minimax(self):
        x = Player.X
        o = Player.O
        _ = None
        # Test out various game states to ensure the computer moves correctly.
        self.game.spaces = [
            x, _, o,
            x, _, _,
            _, o, _,
        ]
        self.game.computer_move()
        self.assert_outcome(self.game, [
            x, _, o,
            x, _, _,
            o, o, _,
        ])
        self.game.spaces = [
            x, x, _,
            _, _, _,
            _, o, _,
        ]
        self.game.computer_move()
        self.assert_outcome(self.game, [
            x, x, o,
            _, _, _,
            _, o, _,
        ])

    def test_show(self):
        self.assertEqual(self.game.show(), "\n".join((
            " 0 | 1 | 2 ",
            "---+---+---",
            " 3 | 4 | 5 ",
            "---+---+---",
            " 6 | 7 | 8 ",
        )))
        self.game.move(Player.X, 0)
        self.assertEqual(self.game.show(), "\n".join((
            " X | 1 | 2 ",
            "---+---+---",
            " 3 | 4 | 5 ",
            "---+---+---",
            " 6 | 7 | 8 ",
        )))
        self.game.move(Player.O, 4)
        self.assertEqual(self.game.show(), "\n".join((
            " X | 1 | 2 ",
            "---+---+---",
            " 3 | O | 5 ",
            "---+---+---",
            " 6 | 7 | 8 ",
        )))

    def assert_outcome(self, game, outcome: [Player]):
        if game.spaces != outcome:
            actual = Game.format_board(*(x or ' ' for x in game.spaces))
            expected = Game.format_board(*(x or ' ' for x in outcome))
            raise AssertionError('\nExpected:\n{}\nActual:\n{}'
                                 .format(expected, actual))