Пример #1
0
def dto_to_game(game_dto):
    """ maps a DTO to a game
    to deserialize a persisted instance.

    :param game_dto: a dictionary representing the structure of the game,
    created by game_to_dto
    :return: a Game instance whose state is equal to the DTO
    """
    maze, leftover_card, maze_card_by_id = _dto_to_maze_cards_and_dictionary(
        game_dto[MAZE])
    objective_maze_card = maze_card_by_id[game_dto[OBJECTIVE]]
    board = Board(maze, leftover_card, objective_maze_card=objective_maze_card)
    players = [
        _dto_to_player(player_dto, board, maze_card_by_id)
        for player_dto in game_dto[PLAYERS]
    ]
    board._pieces = [player.piece for player in players]
    turns_prepare_delay = _dto_to_timedelta(game_dto[TURN_PREPARE_DELAY])
    turns = _dto_to_turns(game_dto[NEXT_ACTION],
                          players=players,
                          prepare_delay=turns_prepare_delay)
    identifier = game_dto[ID]
    game = Game(identifier, board=board, players=players, turns=turns)
    for player in players:
        player.set_game(game)
    game.previous_shift_location = _dto_to_board_location(
        game_dto[PREVIOUS_SHIFT_LOCATION])
    return game
Пример #2
0
def test_get_enabled_shift_locations_with_previous_shift():
    """ Tests get_enabled_shift_locations where the previous shift is (3, 0) """
    board = Board()
    game = Game(identifier=0, board=board)
    game.previous_shift_location = BoardLocation(3, 0)
    expected_disabled = BoardLocation(3, board.maze.maze_size - 1)
    enabled_shift_locations = game.get_enabled_shift_locations()
    assert expected_disabled not in enabled_shift_locations
Пример #3
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
Пример #4
0
def test_random_actions_should_respect_no_pushback_rule():
    """ Runs computation 50 times and checks that none of the computed shifts reverts the previous shift action """

    card_factory = MazeCardFactory()
    orig_board = Board(create_maze(MAZE_STRING, card_factory), leftover_card=card_factory.create_instance("NE", 0))
    for _ in range(50):
        board = copy.deepcopy(orig_board)
        maze = board.maze
        piece = board.create_piece()
        piece.maze_card = maze[BoardLocation(0, 0)]
        game = Game(0, board=orig_board)
        game.previous_shift_location = BoardLocation(0, 3)
        computer_player = ComputerPlayer(library_binding_factory=Mock(), move_url="move-url", shift_url="shift-url",
                                         game=game, identifier=9, board=board, piece=piece)
        shift_action, _ = computer_player.random_actions()
        shift_location, _ = shift_action
        assert shift_location != BoardLocation(6, 3)
Пример #5
0
def test_random_actions_should_respect_no_pushback_rule():
    """ Runs computation 50 times and checks that none of the computed shifts reverts the previous shift action """
    orig_board = create_board()
    for _ in range(50):
        board = copy.deepcopy(orig_board)
        maze = board.maze
        piece = board.create_piece()
        piece.maze_card = maze[BoardLocation(0, 0)]
        game = Game(0, board=board, turns=Turns())
        game.previous_shift_location = BoardLocation(0, 3)
        bot = Bot(library_binding_factory=Mock(),
                  move_url="move-url",
                  shift_url="shift-url",
                  identifier=9,
                  piece=piece)
        bot.set_game(game)
        shift_action, _ = bot.random_actions()
        shift_location, _ = shift_action
        assert shift_location != BoardLocation(6, 3)