Пример #1
0
    def _json_to_game_config(self, json_data, save_id):
        data = json.loads(json_data)[save_id]
        first_player = self._setup_player(data[constants.CURRENT_PLAYER_KEY])
        second_player = self._setup_player(data[constants.OPPONENT_KEY])

        return Config(first_player, second_player,
                      Board(data[constants.BOARD_KEY]))
Пример #2
0
def test_displays_the_board_and_a_message_on_game_over(players):
    game_over_board = Board(['X', 'X', 'X', ' ', ' ', ' ', ' ', ' ', ' '])
    game = Game(players[0], players[1], game_over_board)
    console_io = MockConsoleIO()
    console = Console(console_io)
    game.game_over(console)

    assert console_io.print_output_call_count == 2
Пример #3
0
def test_a_completed_game_is_no_longer_in_progress(players):
    game_over_board = Board(['X', 'X', 'X', ' ', ' ', ' ', ' ', ' ', ' '])
    game = Game(players[0], players[1], game_over_board)
    console_io = MockConsoleIO()
    console = Console(console_io)
    game.game_over(console)

    assert not game.in_progress()
Пример #4
0
def test_game_ends_if_a_player_wins():
    io = MockConsoleIO([])
    game_runner = GameRunner(Console(io))
    player_1 = MockPlayer('player 1', 'O', [0, 2, 4, 6])
    player_2 = MockPlayer('player 2', 'X', [1, 3, 5])
    config = Config(player_1, player_2, Board())

    game_runner.run(config)

    assert "player 1 won!" in io.last_output
Пример #5
0
def test_can_play_a_full_game():
    io = MockConsoleIO([])
    game_runner = GameRunner(Console(io))
    player_1 = MockPlayer('player 1', 'O', [0, 1, 6, 5, 8])
    player_2 = MockPlayer('player 2', 'X', [4, 2, 3, 7])
    config = Config(player_1, player_2, Board())

    game_runner.run(config)

    assert io.last_output == game_tied_message()
Пример #6
0
 def __init__(self,
              first_player,
              second_player,
              game_board=Board(),
              rules=GameRules()):
     self._board = game_board
     self._current_player = first_player
     self._opponent = second_player
     self._in_progress = True
     self._rules = rules
     self._previous_move = None
Пример #7
0
def test_sends_a_json_representation_of_the_game_to_be_saved(save_game_json_data):
    persister_io = MockPersisterIO()
    persister = Persister(persister_io)
    board = Board(['X', 'O', 'X', 'O', '5', '6', '7', '8', '9'])
    player_1 = SimpleComputerPlayer('Player 1', 'O')
    player_2 = SimpleComputerPlayer('Player 2', 'X')
    game = Game(player_1, player_2, board)

    persister.save('myfile.txt', game)

    assert persister_io.saved_data == save_game_json_data
Пример #8
0
    def _configure_game(self, is_networked=False, board=Board()):
        player_1 = self._setup_player(constants.PLAYER_1_NAME,
                                      constants.PLAYER_1_MARKER)
        player_2 = self._setup_player(
            constants.PLAYER_2_NAME,
            self._game_config.select_default_marker(player_1.get_marker()),
            player_1.get_marker(), is_networked)

        player_order = self._select_player_order(player_1, player_2)
        return self._game_config.create_config_object(player_order[0],
                                                      player_order[1], board,
                                                      self._server)
Пример #9
0
def test_creates_a_config_object_with_passed_attributes():
    console = MockConsole()
    game_config = GameConfig(console)

    player_1 = MockPlayer('player 1', 'O')
    player_2 = MockPlayer('player 2', 'X')
    board = Board()

    config_obj = game_config.create_config_object(player_1, player_2, board)

    assert config_obj.first_player == player_1
    assert config_obj.second_player == player_2
    assert config_obj.board == board
Пример #10
0
def test_if_save_data_exists_new_save_is_added_with_an_incremented_save_id(save_game_json_data):
    persister_io = MockPersisterIO(save_game_json_data)
    persister = Persister(persister_io)
    board = Board(['X', 'O', 'X', 'O', '5', '6', '7', '8', '9'])
    player_1 = SimpleComputerPlayer('Player 1', 'O')
    player_2 = SimpleComputerPlayer('Player 2', 'X')
    game = Game(player_1, player_2, board)

    persister.save('myfile.txt', game)

    updated_data = json.loads(persister_io.saved_data)

    assert len(updated_data) == 2
    assert '2' in list(updated_data.keys())
Пример #11
0
def test_a_game_which_is_stopped_but_not_in_progress_does_not_get_saved():
    player_1 = MockPlayer('player 1', 'O', [0, 1, 2])
    player_2 = MockPlayer('player 2', 'X', [3, 4])
    io = MockConsoleIO([])
    persister_io = MockPersisterIO()
    persister = Persister(persister_io)
    game_runner = GameRunner(Console(io), persister)

    config = Config(player_1, player_2, Board())

    game_runner.run(config)
    game_runner.stop()

    assert persister_io.saved_data is None
Пример #12
0
def test_creates_a_game_object_with_the_passed_config_parameters():
    runner = NetworkedGameRunner()
    player_1 = MockPlayer('Player 1', 'X')
    player_2 = MockPlayer('Player 2', 'O')
    board = Board(['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'])

    config = Config(first_player=player_1,
                    second_player=player_2,
                    board=board,
                    server=MockServer())

    runner.run(config)

    game = runner.get_game()

    assert game.get_board() == board
    assert game.get_current_player_name() == 'Player 1'
    assert game.get_opponent_name() == 'Player 2'
Пример #13
0
def test_creates_and_sends_a_string_representing_current_game_state_on_each_turn(
):
    expected_data_string = " X\x1b[0m  | X\x1b[0m  | 3\x1b[0m  \n--------------\n 4\x1b[0m  | 5\x1b[0m  | 6\x1b[0m  \n--------------\n 7\x1b[0m  | 8\x1b[0m  | 9\x1b[0m  \n\n\nPlayer 1's turn."
    console_io = MockConsoleIO()
    console = Console(console_io)
    runner = NetworkedGameRunner(console)

    player_1 = MockPlayer('Player 1', 'X', inputs=[2])
    player_2 = MockPlayer('Player 2', 'O')
    board = Board(['X', 'X', '3', '4', '5', '6', '7', '8', '9'])
    server = MockServer()

    config = Config(first_player=player_1,
                    second_player=player_2,
                    board=board,
                    server=server)

    runner.run(config)

    assert server.sent_data == expected_data_string
Пример #14
0
    def setup_game(self, board_state):
        board = Board(board_state)
        player_1 = SmartComputerPlayer('Player 1', 'X')
        player_2 = SmartComputerPlayer('Player 2', 'O')

        return Game(player_1, player_2, board)
Пример #15
0
def test_board_is_full_when_all_spaces_are_occupied():
    board = Board(['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'])

    assert board.is_full() is True
Пример #16
0
def test_the_game_is_over_if_a_player_has_won(rules):
    board = Board(['X', 'X', 'X', '4', '5', '6', '7', '8', '9'])

    assert rules.game_over(board, 'X') is True
Пример #17
0
def test_the_game_is_over_when_board_is_full(rules):
    board = Board(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])

    assert rules.game_over(board, 'X') is True
Пример #18
0
def test_game_is_not_over_when_board_is_not_full(rules):
    board = Board(['1', '2', '3', '4', '5', '6', '7', '8', '9'])

    assert rules.game_over(board, 'X') is False
Пример #19
0
def test_a_line_wins_if_all_spaces_contain_the_same_marker(rules):
    board = Board(['X', 'X', 'X', '4', '5', '6', '7', '8', '9'])

    assert rules.is_won(board, 'X') is True
Пример #20
0
def test_a_line_does_not_win_if_all_spaces_are_empty(rules):
    board = Board(['1', '2', '3', '4', '5', '6', '7', '8', '9'])

    assert rules.is_won(board, 'X') is False
Пример #21
0
def board():
    return Board()
Пример #22
0
def test_returns_no_spaces_when_all_have_been_occupied():
    board = Board(['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'])

    assert len(board.available_spaces()) == 0
Пример #23
0
def test_returns_the_contents_of_the_spaces_requested():
    board = Board(['A', 'B', 'C', '4', '5', '6', '7', '8', '9'])

    assert board.retrieve_line((0, 1, 2)) == ['A', 'B', 'C']
Пример #24
0
def test_board_is_not_full_when_only_some_spaces_are_occupied():
    board = Board(['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '9'])

    assert board.is_full() is False
Пример #25
0
def test_checks_for_game_over_state_each_turn_and_at_game_over_state(players):
    game_runner = GameRunner(MockConsole(), game_class=MockGame)
    game_runner.run(Config(players[0], players[1], Board()))

    assert game_runner.get_game().game_over_call_count == 2
Пример #26
0
def test_plays_a_turn_when_game_is_not_over(players):
    game_runner = GameRunner(MockConsole(), game_class=MockGame)
    game_runner.run(Config(players[0], players[1], Board()))

    assert game_runner.get_game().play_turn_call_count == 1