Beispiel #1
0
def test_next_player_action_after_perform_shift_should_be_move():
    """ Tests next_player_action and perform_action """
    player = Player(7, 0)
    turns = Turns(players=[player])
    turns.perform_action(player, PlayerAction.SHIFT_ACTION)
    assert turns.next_player_action() == PlayerAction(player,
                                                      PlayerAction.MOVE_ACTION)
Beispiel #2
0
def test_given_empty_game__when_player_is_added__initializes_turns():
    game = Game(identifier=0)

    player = Player(game.unused_player_id())
    game.add_player(player)

    game.turns.next_player_action() == PlayerAction(player,
                                                    PlayerAction.PREPARE_SHIFT)
Beispiel #3
0
def test_register_turn_listener__when_player_shifts__listener_is_notified():
    player = Player(0)
    game, listener = _turn_listener_test_setup([player])

    listener.reset_mock()
    game.shift(0, BoardLocation(0, 1), 0)
    listener.assert_called_once_with(game=game,
                                     next_player_action=PlayerAction(
                                         player, PlayerAction.MOVE_ACTION))
Beispiel #4
0
def given_empty_game__when_first_player_is_added__turn_listener_is_notified():
    player = Player(0)
    game, listener = _turn_listener_test_setup([])

    listener.reset_mock()
    game.add_player(player)
    listener.assert_called_once_with(game=game,
                                     next_player_action=PlayerAction(
                                         player, PlayerAction.SHIFT_ACTION))
Beispiel #5
0
def test_register_turn_listener__when_player_moves__listener_is_notified():
    players = [Player(0), Player(1)]
    game, listener = _turn_listener_test_setup(players)
    game.shift(0, BoardLocation(0, 1), 0)

    listener.reset_mock()
    game.move(0, BoardLocation(0, 0))
    listener.assert_called_once_with(game=game,
                                     next_player_action=PlayerAction(
                                         players[1],
                                         PlayerAction.SHIFT_ACTION))
Beispiel #6
0
def test_register_turn_listener__when_first_player_is_added__listener_is_notified(
):
    game, listener = _turn_listener_test_setup([Player(0)])
    game.remove_player(0)
    player = Player(1)

    listener.reset_mock()
    game.add_player(player)
    listener.assert_called_once_with(game=game,
                                     next_player_action=PlayerAction(
                                         player, PlayerAction.SHIFT_ACTION))
Beispiel #7
0
def test_register_turn_listener__when_current_player_is_deleted__listener_is_notified(
):
    players = [Player(0), Player(1)]
    game, listener = _turn_listener_test_setup(players)

    listener.reset_mock()
    game.remove_player(players[0].identifier)
    listener.assert_called_once_with(game=game,
                                     next_player_action=PlayerAction(
                                         players[1],
                                         PlayerAction.SHIFT_ACTION))
Beispiel #8
0
def _create_test_game(with_computer=False):
    """ Creates a Game instance by hand """
    card_factory = MazeCardFactory()
    board = Board(leftover_card=MazeCard(0, MazeCard.T_JUNCT, 0))
    for row in range(board.maze.maze_size):
        for column in range(board.maze.maze_size):
            if row == 0 and column == 0:
                board.maze[BoardLocation(
                    row, column)] = card_factory.create_instance(
                        MazeCard.STRAIGHT, 0)
            elif row == 1 and column == 1:
                board.maze[BoardLocation(
                    row, column)] = card_factory.create_instance(
                        MazeCard.CORNER, 0)
            elif row == 2 and column == 2:
                board.maze[BoardLocation(
                    row, column)] = card_factory.create_instance(
                        MazeCard.T_JUNCT, 270)
            else:
                board.maze[BoardLocation(
                    row, column)] = card_factory.create_instance(
                        MazeCard.T_JUNCT, 0)
    player_ids = [3, 4]
    players = [
        Player(identifier=player_id, game=None) for player_id in player_ids
    ]
    if with_computer:
        player_ids.append(42)
        players.append(
            create_computer_player(player_id=42,
                                   compute_method="dynamic-foo",
                                   shift_url="shift-url",
                                   move_url="move-url"))
    for player in players:
        player.set_board(board)
    players[0].piece.maze_card = board.maze[BoardLocation(3, 3)]
    players[1].piece.maze_card = board.maze[BoardLocation(5, 5)]
    players[0].piece.piece_index = 1
    players[1].piece.piece_index = 0
    players[0].score = 7
    players[1].score = 8
    board._objective_maze_card = board.maze[BoardLocation(1, 4)]
    turns = Turns(players,
                  next_action=PlayerAction(players[1],
                                           PlayerAction.MOVE_ACTION))
    game = Game(identifier=7, turns=turns, board=board, players=players)
    for player in players:
        player._game = game
    game.previous_shift_location = BoardLocation(0, 3)
    return game, player_ids
Beispiel #9
0
def test_is_action_possible_with_next_action():
    """ Tests is_action_possible and constructor parameter next_action """
    players = [Player(id, 0) for id in [9, 0, 3]]
    turns = Turns(players=players,
                  next_action=PlayerAction(players[2],
                                           PlayerAction.MOVE_ACTION))
    assert not turns.is_action_possible(players[0], PlayerAction.SHIFT_ACTION)
    assert not turns.is_action_possible(players[1], PlayerAction.SHIFT_ACTION)
    assert turns.is_action_possible(players[2], PlayerAction.MOVE_ACTION)
    turns.perform_action(players[2], PlayerAction.MOVE_ACTION)
    assert turns.is_action_possible(players[0], PlayerAction.SHIFT_ACTION)
    assert not turns.is_action_possible(players[1], PlayerAction.SHIFT_ACTION)
    turns.perform_action(players[0], PlayerAction.SHIFT_ACTION)
    turns.perform_action(players[0], PlayerAction.MOVE_ACTION)
    assert not turns.is_action_possible(players[0], PlayerAction.SHIFT_ACTION)
    assert turns.is_action_possible(players[1], PlayerAction.SHIFT_ACTION)
Beispiel #10
0
def _dto_to_turns(next_action_dto, players, prepare_delay=timedelta(0)):
    """ Maps a DTO to a Turns instance

    :param next_action_dto: a dictionary representing the next player action,
    as created by _turns_to_next_action_dto
    :param players: a list of players. The value of the PLAYER_ID field in the dto
    has to match one of the player's id
    :return: an instance of Turns
    """
    if not players:
        return Turns(prepare_delay=prepare_delay)
    player = next(player for player in players
                  if player.identifier == next_action_dto[PLAYER_ID])
    action = next_action_dto[ACTION]
    return Turns(players=players,
                 next_action=PlayerAction(player, action),
                 prepare_delay=prepare_delay)
Beispiel #11
0
def test_next_player_action_with_one_player_should_be_shift():
    """ Tests next_player_action """
    player = Player(7, 0)
    turns = Turns(players=[player])
    assert turns.next_player_action() == PlayerAction(
        player, PlayerAction.SHIFT_ACTION)
Beispiel #12
0
def with_next_action(game, action, player_id=0):
    player = game.get_player(player_id)
    game.turns.set_next(PlayerAction(player, action))