Пример #1
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
Пример #2
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()
Пример #3
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
Пример #4
0
def test_a_game_which_is_stopped_while_in_progress_gets_saved():
    player_1 = MockPlayer('player 1', 'O', [])
    player_2 = MockPlayer('player 2', 'X', [])
    io = MockConsoleIO([])
    persister_io = MockPersisterIO()
    persister = Persister(persister_io)
    game_runner = GameRunner(Console(io), persister)
    game_runner._game = Game(player_1, player_2)


    game_runner.stop()

    assert persister_io.saved_data is not None
Пример #5
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
Пример #6
0
def test_sets_the_colour_of_a_players_marker():
    console = MockConsole()
    game_config = GameConfig(console)
    player = MockPlayer('Player 1', 'O')

    expected_colour_code = constants.COLOURS['1']
    game_config.set_marker_colour(player, '1')

    assert player.get_marker_colour() == expected_colour_code
Пример #7
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'
Пример #8
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
Пример #9
0
def players():
    return [MockPlayer('player 1', 'O', [0]), MockPlayer('player 2', 'X', [1])]
Пример #10
0
def players():
    return [MockPlayer('player 1', 'O'), MockPlayer('player 2', 'X')]