Example #1
0
def test_has_out_path_for_corner_with_rotation_90():
    """ Tests has_rotated_out_path """
    maze_card = MazeCard(0, MazeCard.CORNER, 90)
    assert not maze_card.has_rotated_out_path((-1, 0))
    assert maze_card.has_rotated_out_path((0, 1))
    assert maze_card.has_rotated_out_path((1, 0))
    assert not maze_card.has_rotated_out_path((0, -1))
Example #2
0
def create_random_maze(maze_card_factory=None):
    """ Generates a random maze state.
    Corners of the maze are fixed as corners
    """
    fixed_cards = {
        BoardLocation(0, 0): MazeCard(out_paths=MazeCard.CORNER, rotation=90),
        BoardLocation(0, 6): MazeCard(out_paths=MazeCard.CORNER, rotation=180),
        BoardLocation(6, 6): MazeCard(out_paths=MazeCard.CORNER, rotation=270),
        BoardLocation(6, 0): MazeCard(out_paths=MazeCard.CORNER, rotation=0)
    }

    if not maze_card_factory:
        maze_card_factory = MazeCardFactory()
    maze = Maze()

    def card_at(location):
        if location in fixed_cards:
            return maze_card_factory.create_instance(
                fixed_cards[location].out_paths,
                fixed_cards[location].rotation)
        return maze_card_factory.create_random_maze_card()

    for location in maze.maze_locations:
        maze[location] = card_at(location)
    return maze
Example #3
0
def test_set_board():
    """ Tests that set_board asks the board for a piece """
    board = MagicMock()
    piece = Piece(0, MazeCard())
    board.create_piece.return_value = piece
    player = Player(1, 0)
    player.set_board(board)
    board.create_piece.assert_called_once_with()
    assert player.piece is piece
Example #4
0
def _dto_to_maze_card(maze_card_dto):
    """ Maps a DTO to a maze card and a board location

    :param maze_card_dto: a dictionary representing the game (sub-)structure of a maze card,
    as created by _maze_card_to_dto
    :return: a MazeCard instance and a BoardLocation instance (or None, for the leftover card)
    """
    maze_card = MazeCard(maze_card_dto[ID], maze_card_dto[OUT_PATHS],
                         maze_card_dto[ROTATION])
    location = _dto_to_board_location(maze_card_dto[LOCATION])
    return maze_card, location
Example #5
0
def test_rotated_out_paths_for_corner_with_rotation_0():
    """ Tests out_paths """
    maze_card = MazeCard(0, MazeCard.CORNER, 0)
    assert (-1, 0) in maze_card.rotated_out_paths()
    assert (0, 1) in maze_card.rotated_out_paths()
    assert (1, 0) not in maze_card.rotated_out_paths()
    assert (0, -1) not in maze_card.rotated_out_paths()
Example #6
0
def test_rotated_out_paths_for_t_junct_with_rotation_180():
    """ Tests out_paths """
    maze_card = MazeCard(0, MazeCard.T_JUNCT, 180)
    assert (-1, 0) in maze_card.rotated_out_paths()
    assert (0, 1) not in maze_card.rotated_out_paths()
    assert (1, 0) in maze_card.rotated_out_paths()
    assert (0, -1) in maze_card.rotated_out_paths()
Example #7
0
def test_has_out_path_for_straight_with_rotation_270():
    """ Tests has_rotated_out_path """
    maze_card = MazeCard(0, MazeCard.STRAIGHT, 270)
    assert not maze_card.has_rotated_out_path((-1, 0))
    assert maze_card.has_rotated_out_path((0, 1))
    assert not maze_card.has_rotated_out_path((1, 0))
    assert maze_card.has_rotated_out_path((0, -1))
Example #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
Example #9
0
def _determine_fixed_cards(size):
    """ Determines the locations, out paths and rotations for the fixed cards,
    according to the layout of the original game. The layout is generalized to arbitrary
    sizes.
    Returns a map from location to MazeCard. """
    maze = Maze(maze_size=size)
    border = size - 1
    center = border // 2
    fixed_corners = {
        BoardLocation(0, 0):
        MazeCard(out_paths=MazeCard.CORNER, rotation=90),
        BoardLocation(0, border):
        MazeCard(out_paths=MazeCard.CORNER, rotation=180),
        BoardLocation(border, border):
        MazeCard(out_paths=MazeCard.CORNER, rotation=270),
        BoardLocation(border, 0):
        MazeCard(out_paths=MazeCard.CORNER, rotation=0)
    }
    fixed_center = {}
    if border % 4 == 0:
        fixed_center[BoardLocation(
            center, center)] = MazeCard(out_paths=MazeCard.CROSS)
    fixed_locations = [
        location for location in maze.maze_locations
        if _even(location.column) and _even(location.row)
    ]
    fixed_t_juncts_locations = [
        location for location in fixed_locations
        if location not in fixed_corners and location not in fixed_center
    ]

    fixed_t_juncts = {
        location: MazeCard(out_paths=MazeCard.T_JUNCT,
                           rotation=_rotation_of_fixed_t_junct(location, size))
        for location in fixed_t_juncts_locations
    }
    fixed_cards = {**fixed_corners, **fixed_t_juncts, **fixed_center}
    return fixed_cards
Example #10
0
 def create_instance(self, out_paths, rotation):
     """Generates a new instance, with autoincreasing ID.
     """
     maze_card = MazeCard(self._next_id, out_paths, rotation)
     self._next_id = self._next_id + 1
     return maze_card
Example #11
0
def test_getter_returns_set_card():
    """ Tests setter and getter """
    maze = Maze()
    maze_card = MazeCard()
    maze[BoardLocation(1, 1)] = maze_card
    assert maze[BoardLocation(1, 1)] == maze_card
Example #12
0
def test_rotation_should_be_settable():
    """ Tests rotation getter and setter """
    maze_card = MazeCard()
    maze_card.rotation = 90
    assert maze_card.rotation == 90
Example #13
0
def test_rotation_setter_should_throw_for_invalid_rotation():
    """ Tests rotation setter """
    maze_card = MazeCard()
    with pytest.raises(ValueError):
        maze_card.rotation = 80