예제 #1
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"
예제 #2
0
 def test_valid_game_config(self):
     game_params = {
         'messages': CliMessages(),
         'cli_input': Input(),
         'cli_output': Output(),
         'validator': Validator()
     }
     return game_params
예제 #3
0
 def test_game_params(self):
     game_params = {
         'messages': CliMessages(),
         'cli_input': FakeInput("7", "5"),
         'cli_output': Output(),
         'validator': Validator()
     }
     return game_params
예제 #4
0
 def test_that_it_can_check_if_the_user_move_is_valid(self, monkeypatch, capsys):
     game_params = {
         'messages': CliMessages(),
         'cli_input': FakeInput("363", "7"),
         'cli_output': Output(),
         'validator': Validator() 
     }
     name = "foo"
     test_input_strategy = InputStrategy()
     result = test_input_strategy.get_move(name, game_params)
     assert result == "7"
예제 #5
0
 def test_that_it_can_return_a_user_move(self, monkeypatch):
     game_params = {
         'messages': CliMessages(),
         'cli_input': Input(),
         'cli_output': Output(),
         'validator': Validator() 
     }
     test_input_strategy = InputStrategy()
     monkeypatch.setattr('builtins.input', lambda x: " 5  ")
     i = test_input_strategy.get_move("player name", game_params)
     assert i == "5"
예제 #6
0
 def test_that_it_can_check_if_the_user_icon_is_invalid(
         self, test_game_config, monkeypatch, capsys):
     game_params = {
         'messages': CliMessages(),
         'cli_input': FakeInput("test", "x"),
         'cli_output': Output(),
         'validator': Validator()
     }
     game_params['cli_input'].reset_count()
     test_game_config.get_icon(game_params)
     captured = capsys.readouterr()
     assert captured.out == "\nThat was an invalid selection. Icon should be one alpha character. Please try again.\n\nYour selected icon is x\n"
예제 #7
0
 def test_that_it_can_check_if_the_user_name_is_invalid(
         self, test_game_config, monkeypatch, capsys):
     game_params = {
         'messages': CliMessages(),
         'cli_input': FakeInput("p", "Player 1"),
         'cli_output': Output(),
         'validator': Validator()
     }
     game_params['cli_input'].reset_count()
     test_game_config.get_name(game_params)
     captured = capsys.readouterr()
     assert captured.out == "\nName should not be empty, please try again\n\nYour selected player name is Player 1\n"
예제 #8
0
 def test_that_it_can_check_if_the_user_turn_is_invalid(
         self, test_game_config, monkeypatch, capsys):
     game_params = {
         'messages': CliMessages(),
         'cli_input': FakeInput("345", "2"),
         'cli_output': Output(),
         'validator': Validator()
     }
     game_params['cli_input'].reset_count()
     test_game_config.get_turn_order(game_params)
     captured = capsys.readouterr()
     assert captured.out == "\nThat was an invalid selection. Please enter 1 if you would like to go first or 2 if second.\n\nYour selected turn order is 2\n"
예제 #9
0
 def test_that_it_can_check_if_the_user_move_is_invalid(self, monkeypatch, capsys):
     game_params = {
         'messages': CliMessages(),
         'cli_input': FakeInput("568", "4"),
         'cli_output': Output(),
         'validator': Validator() 
     }
     game_params['cli_input'].reset_count()
     name = "foo"
     test_input_strategy = InputStrategy()
     test_input_strategy.get_move(name, game_params)
     captured = capsys.readouterr()
     assert captured.out == "That was an invalid move, please try again. Please select a move between 1 - 9:\nfoo selects square 4. Placing foo's move.\n"
예제 #10
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
예제 #11
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"
예제 #12
0
    def test_that_it_returns_a_user_config_hash(self, test_game_config):
        game_params = {
            'messages': FakeCliMessages(),
            'cli_input': FakePlayerConfigInput(),
            'cli_output': Output(),
            'validator': FakeValidator()
        }
        expected_result = {
            'name': 'foo',
            'icon': 'foo',
            'turn_order': 'foo',
            'computer_name': 'foo',
            'computer_icon': 'foo'
        }

        assert test_game_config.player_config(game_params) == expected_result
예제 #13
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]]
예제 #14
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)
예제 #15
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"
예제 #16
0
    def test_that_it_prints_the_output(self, capsys):
        output = Output()
        output.display("test")

        captured = capsys.readouterr()
        assert captured.out == "test\n"