Ejemplo n.º 1
0
    def test_that_it_can_check_for_a_win(self):
        board = [
            ["@", "x", "x"],
            ["x", "@", "x"],
            ["@", "x", "@"],
        ]
        test_board = Board(board)
        test_rules = Rules(test_board)
        test_game = Game(test_board, test_rules)
        test_messages = CliMessages()
        test_game_config = GameConfig()
        test_game_over = GameOver()

        player_settings_dict = {
            'name': 'Player 1',
            'icon': 'x',
            'turn_order': '1',
            'computer_name': 'Computer',
            'computer_icon': '@'
        }

        result = test_game_config.create_players(player_settings_dict,
                                                 test_game)

        assert test_game_over.check_for_win(
            result, test_messages) == "\nComputer wins!"
Ejemplo n.º 2
0
    def test_that_run_returns_a_game_over_message(self, capsys):
        board = [
            ["@", "x", "x"],
            ["x", "@", "x"],
            ["@", "x", "@"],
        ]
        test_board = Board(board)
        test_rules = Rules(test_board)
        test_game = Game(test_board, test_rules)
        test_game_over = GameOver()

        game_params = {
            'messages': FakeCliMessages(),
            'game_input': Input(),
            'cli_output': Output(),
            'validator': Validator()
        }

        human_player = Player("Player 1", "x", "foo")
        computer_player = Player("Computer", "@", "foo")
        test_game.add_player(human_player)
        test_game.add_player(computer_player)

        test_game.set_current_player(computer_player)

        test_game_over.run(test_game, game_params)
        captured = capsys.readouterr()
        assert captured.out.strip().split("\n")[-1] == "game has ended"
Ejemplo n.º 3
0
    def test_that_create_game_returns_a_game(self, test_game_config):
        board = Board()
        rules = Rules(board)

        result = test_game_config.create_game(board, rules)

        assert result.get_board() == board
Ejemplo n.º 4
0
 def test_rules_filled(self):
     board_array = [
         ["x", "x", "x"],
         ["o", "o", "x"],
         ["o", "x", "o"],
     ]
     board = Board(board_array)
     return Rules(board)
Ejemplo n.º 5
0
    def test_it_can_return_a_player_score_value(self):
        board_array = [["x", "x", "o"], ["x", "o", "o"], ["x", "o", "x"]]

        board = Board(board_array)
        rules = Rules(board)
        game = Game(board, rules)
        test_strategy = MinimaxStrategy()

        assert test_strategy.player_score_value(game) == -1
Ejemplo n.º 6
0
 def test_game(self):
     board = [
         ["o", "x", "x"],
         ["x", "o", "x"],
         ["o", "x", "o"],
     ]
     test_board = Board(board)
     test_rules = Rules(test_board)
     return Game(test_board, test_rules)
Ejemplo n.º 7
0
 def test_has_winning_diagonal(self):
     board_array = [
         ["o", "x", "x"],
         ["x", "o", "x"],
         ["o", "x", "o"],
     ]
     board = Board(board_array)
     test_rules = Rules(board)
     assert test_rules.has_winning_diagonal() == True
Ejemplo n.º 8
0
 def test_that_intro_prints_a_welcome_message(self, capsys,
                                              test_valid_game_config,
                                              test_game_config):
     board = Board()
     rules = Rules(board)
     game = Game(board, rules)
     test_game_config.intro(game, test_valid_game_config)
     captured = capsys.readouterr()
     assert captured.out.split("\n\n")[0].strip(
     ) == "Welcome to Tictactoe, human vs computer version"
Ejemplo n.º 9
0
 def test_can_return_a_winning_icon_if_column_wins(self, test_rules_filled):
     board_array = [
         ["x", "o", "x"],
         ["o", "o", "x"],
         ["o", "x", "x"],
     ]
     board = Board(board_array)
     test_rules = Rules(board)
     rows = test_rules.board.rows()
     assert test_rules.winning_icon(rows) == "x"
Ejemplo n.º 10
0
    def test_it_can_return_available_squares_in_a_flattened_board(self):
        board_array = [["x", None, "o"], [None, "o", "o"], ["x", "o", None]]
        board = Board(board_array)
        rules = Rules(board)
        game = Game(board, rules)

        flattened_board = game.get_board().flattened_board()
        test_strategy = MinimaxStrategy()

        assert test_strategy.available_squares(flattened_board) == [2, 4, 9]
Ejemplo n.º 11
0
    def test_input_move(self):
        game_params = {
            'messages': CliMessages(),
            'cli_input': Input(),
            'cli_output': Output(),
            'validator': Validator()
        }
        board = Board()
        rules = Rules(board)
        game = Game(board, rules)

        strategy = FakeInputStrategy()
        human_player = Player("player 1", "x", strategy)
        assert human_player.select_move(game, game_params) == 5
Ejemplo n.º 12
0
    def test_that_run_returns_a_game_object(self, test_game_params):
        board_array = [["x", "x", "o"], ["o", "x", "o"], ["x", "o", "x"]]
        board = Board(board_array)
        rules = Rules(board)
        game = Game(board, rules)

        human_player = Player("Player 1", "x", InputStrategy())
        computer_player = Player("Computer", "o", "foo")
        game.add_player(human_player)
        game.add_player(computer_player)
        game.set_current_player(human_player)

        test_game_loop = GameLoop()
        result = test_game_loop.run(game, test_game_params)
        assert game == result
Ejemplo n.º 13
0
    def test_minimax_move(self):
        game_params = {
            'messages': CliMessages(),
            'cli_input': Input(),
            'cli_output': Output(),
            'validator': Validator()
        }
        board = Board()
        rules = Rules(board)
        game = Game(board, rules)

        strategy = FakeMinimaxStrategy()
        computer_player = Player("computer", "o", strategy)

        assert computer_player.select_move(game, game_params) == "5"
Ejemplo n.º 14
0
    def test_that_it_picks_a_win(self):
        board_array = [["x", "o", "x"], ["x", "o", "x"], ["o", None, None]]

        board = Board(board_array)
        rules = Rules(board)
        game = Game(board, rules)

        human_player = Player("Player 1", "x", "foo")
        computer_player = Player("Computer", "o", MinimaxStrategy())
        game.add_player(computer_player)
        game.add_player(human_player)
        game.set_current_player(computer_player)
        test_strategy = MinimaxStrategy()

        result = test_strategy.minimax(game)
        assert result == 8
Ejemplo n.º 15
0
    def run(self):
        messages = CliMessages()
        
        game_params = {
            'messages': messages,
            'cli_input': Input(),
            'cli_output': Output(),
            'validator': Validator()
        }

        board = Board()
        rules = Rules(board)
        game = Game(board, rules)

        game = GameConfig().run(game, game_params)
        game = GameLoop().run(game, game_params)
        GameOver().run(game, game_params)
Ejemplo n.º 16
0
    def test_that_run_returns_a_game_object_with_an_empty_board(
            self, test_game_config):
        game_params = {
            'messages': FakeCliMessages(),
            'cli_input': FakePlayerConfigInput(),
            'cli_output': Output(),
            'validator': FakeValidator()
        }

        board = Board()
        rules = Rules(board)
        game = Game(board, rules)

        result = test_game_config.run(game, game_params)
        assert result.get_board().board() == [[None, None, None],
                                              [None, None, None],
                                              [None, None, None]]
Ejemplo n.º 17
0
    def test_that_it_can_mark_a_square_on_an_empty_board_correctly(
            self, test_game_2):
        board = Board()
        rules = Rules(board)
        game = Game(board, rules)
        square = 9
        icon = "x"

        game.mark_square(square, icon)

        expected_board = [
            [None, None, None],
            [None, None, None],
            [None, None, "x"],
        ]

        assert game.get_board().board() == expected_board
Ejemplo n.º 18
0
    def test_that_it_can_unmark_a_square(self, test_game_2):
        board = [[None, None, None], ["x", "o", "x"], ["o", "x", "x"]]
        test_board = Board(board)
        test_rules = Rules(test_board)
        test_game = Game(test_board, test_rules)

        square = 9

        test_game.unmark_square(square)

        expected_board = [
            [None, None, None],
            ["x", "o", "x"],
            ["o", "x", None],
        ]

        assert test_game.get_board().board() == expected_board
Ejemplo n.º 19
0
    def test_that_it_runs_the_loop_again_if_selected_move_is_a_filled_position(
            self, capsys, test_game_params):
        board_array = [["x", "x", "o"], ["o", None, "o"], ["x", "o", "x"]]
        board = Board(board_array)
        rules = Rules(board)
        game = Game(board, rules)

        human_player = Player("Player 1", "x", InputStrategy())
        computer_player = Player("Computer", "o", "foo")
        game.add_player(human_player)
        game.add_player(computer_player)
        game.set_current_player(human_player)

        test_game_loop = GameLoop()
        test_game_loop.place_move(game, test_game_params)
        captured = capsys.readouterr()
        expected_output = "Player 1 selects square 7. Placing Player 1's move.\nThat was an invalid move, please try again. Please select a move between 1 - 9:\nPlayer 1 selects square 5. Placing Player 1's move."
        assert captured.out.split("\n\n")[0] == expected_output
Ejemplo n.º 20
0
    def test_that_it_can_place_a_move(self):
        board_array = [["x", "x", "o"], ["x", None, "o"], ["x", "o", "x"]]
        expected_board_array = [["x", "x", "o"], ["x", "o", "o"],
                                ["x", "o", "x"]]
        board = Board(board_array)
        rules = Rules(board)
        game = Game(board, rules)

        human_player = Player("Player 1", "x", "foo")
        computer_player = Player("Computer", "o", "foo")
        game.add_player(human_player)
        game.add_player(computer_player)
        game.set_current_player(computer_player)

        test_strategy = MinimaxStrategy()
        test_strategy.place_move(game, 5)
        assert game.get_board().empty_squares() == []
        assert game.get_board().board() == expected_board_array
        assert game.get_current_player().icon == "x"
Ejemplo n.º 21
0
    def test_that_best_move_returns_a_move(self):
        game_params = {
            'messages': CliMessages(),
            'cli_input': FakeInput("363", "7"),
            'cli_output': Output(),
            'validator': Validator() 
        }
        board = Board()
        rules = Rules(board)
        game = Game(board, rules)
        
        human_player = Player("Player 1", "x", "foo")
        computer_player = Player("Computer", "o", "foo")
        game.add_player(human_player)
        game.add_player(computer_player)
        game.set_current_player(computer_player)

        test_input_strategy = InputStrategy()
        assert test_input_strategy.best_move(game, game_params) == "7"
Ejemplo n.º 22
0
    def test_that_game_loop_can_mark_the_board(self, test_game_params):
        board_array = [["x", "x", "o"], ["o", None, "o"], ["x", "o", "x"]]

        expected_board_array = [["x", "x", "o"], ["o", "x", "o"],
                                ["x", "o", "x"]]
        board = Board(board_array)
        rules = Rules(board)
        game = Game(board, rules)

        human_player = Player("Player 1", "x", InputStrategy())
        computer_player = Player("Computer", "o", "foo")
        game.add_player(human_player)
        game.add_player(computer_player)
        game.set_current_player(human_player)

        test_game_loop = GameLoop()
        test_game_loop.mark_board(5, game, test_game_params)

        assert game.get_board().board() == expected_board_array
Ejemplo n.º 23
0
    def test_that_create_players_creates_the_players_and_returns_an_updated_game(
            self, test_game_config):
        board = Board()
        rules = Rules(board)
        game = Game(board, rules)

        player_settings_dict = {
            'name': 'Player 1',
            'icon': 'x',
            'turn_order': '1',
            'computer_name': 'Computer',
            'computer_icon': '@'
        }

        result = test_game_config.create_players(player_settings_dict, game)

        assert len(result.get_players()) == 2
        assert result.get_players()[0] == result.get_current_player()
        assert result.get_players()[0].name == 'Player 1'
        assert result.get_players()[1].name == 'Computer'
Ejemplo n.º 24
0
 def test_rules(self):
     board = Board()
     return Rules(board)
Ejemplo n.º 25
0
 def test_game(self):
     test_board = Board()
     test_rules = Rules(test_board)
     test_game = Game(test_board, test_rules)
Ejemplo n.º 26
0
 def test_game_2(self):
     board = [[None, None, None], ["x", "o", "x"], ["o", "x", None]]
     test_board = Board(board)
     test_rules = Rules(test_board)
     return Game(test_board, test_rules)