Ejemplo n.º 1
0
def test_robot_change_direction(current_direction, towards, new_direction):
    """
    Assert that robot rotates correctly according to given rotation.
    """
    robot = Robot(current_direction, None, "bender")
    robot.rotate(towards, State.get_start_state("maps/test_maps/test_3.json"))
    assert robot.direction == new_direction
Ejemplo n.º 2
0
def test_card_priorities():
    """
    Check that robots are sorted according to their cards on hand.
    Assert first and last robot's on the list priority.
    """
    state = State.get_start_state("maps/test_maps/test_effects.json")
    cards = [[MovementCard(100, 1), MovementCard(100, 1)],
             [RotationCard(120, Rotation.U_TURN), MovementCard(200, 2)],
             [MovementCard(150, -1), MovementCard(110, 1)],
             [MovementCard(110, 1), MovementCard(140, 1)],
             [RotationCard(55, Rotation.LEFT), MovementCard(250, 2)],
             [MovementCard(300, 3), MovementCard(350, 3)],
             [MovementCard(230, 2), RotationCard(80, Rotation.RIGHT)],
             [MovementCard(150, 1), MovementCard(140, 1)],
             ]
    # create robot's programs
    i = 0
    for robot in state.get_active_robots():
        robot.program = cards[i]
        i += 1

    robot_cards = state.get_robots_ordered_by_cards_priority(0)
    print(robot_cards[0])
    assert robot_cards[0][0].program[0].priority == 300
    assert robot_cards[7][0].program[0].priority == 55

    robot_cards = state.get_robots_ordered_by_cards_priority(1)
    assert robot_cards[0][0].program[1].priority == 350
    assert robot_cards[7][0].program[1].priority == 80
Ejemplo n.º 3
0
def test_check_winner_2():
    """
    Assert there in no winner when no one has collected enough flags.
    """
    state = State.get_start_state("maps/test_maps/test_3.json")
    state.robots[0].flags = state.flag_count - 1
    assert state.check_winner() == []
    assert state.robots[0].winner is False
Ejemplo n.º 4
0
def test_check_winner():
    """
    Assert the winner when there is only one.
    """
    state = State.get_start_state("maps/test_maps/test_3.json")
    state.robots[0].flags = state.flag_count
    assert state.check_winner() == ['Bender']
    assert state.robots[0].winner
Ejemplo n.º 5
0
def test_confirmed_count_2():
    """
    Assert the count when all robots confirm choice selection.
    """
    state = State.get_start_state("maps/test_maps/test_3.json")
    for robot in state.robots:
        robot.selection_confirmed = True
    assert state.count_confirmed_selections() == len(state.robots)
Ejemplo n.º 6
0
def test_confirmed_count():
    """
    Assert the count when 2 robots confirm choice selection.
    """
    state = State.get_start_state("maps/test_maps/test_3.json")
    state.robots[0].selection_confirmed = True
    state.robots[1].selection_confirmed = True
    assert state.count_confirmed_selections() == 2
Ejemplo n.º 7
0
def test_robot_walk(input_coordinates, input_direction, distance, output_coordinates):
    """
    Take robot's coordinates, direction and distance and assert robot walked
    to correct coordinates.
    """
    state = State.get_start_state("maps/test_maps/test_3.json")
    robot = Robot(input_direction, input_coordinates, "bender")
    robot.walk(distance, state, input_direction)
    assert robot.coordinates == output_coordinates
Ejemplo n.º 8
0
def test_robot_move(input_coordinates, input_direction, distance, output_coordinates):
    """
    Take robot's coordinates, move's direction and distance and assert robot
    was moved to correct coordinates.
    """
    state = State.get_start_state("maps/test_maps/test_3.json")
    robot = Robot(Direction.N, input_coordinates, "bender")
    robot.move(input_direction, distance, state)
    assert robot.coordinates == output_coordinates
Ejemplo n.º 9
0
def test_check_winner_3():
    """
    Assert more winners when there is more who collected flags.
    """
    state = State.get_start_state("maps/test_maps/test_3.json")
    state.robots[0].flags = state.flag_count
    state.robots[1].flags = state.flag_count
    assert state.check_winner() == ['Bender', "Bishop"]
    assert state.robots[0].winner
    assert state.robots[1].winner
Ejemplo n.º 10
0
def test_find_last_start_coordinates():
    """
    Change the coordinates to the last in the list
    if no one is standing on them.
    """
    state = State.get_start_state("maps/test_maps/test_3.json")
    state.robots[0].start_coordinates = [(1, 0), (5, 5)]
    state.robots[1].coordinates = (1, 0)
    state.robots[0].find_free_start(state)
    assert state.robots[0].coordinates == (5, 5)
Ejemplo n.º 11
0
def test_robot_on_start_has_the_correct_direction():
    """
    When robot is created, his direction shoud be the same as the direction
    of start tile he stands on.
    Assert the direction is correcly initiated.
    """
    state = State.get_start_state("maps/test_maps/test_start_direction.json")
    for robot in state.robots:
        tile_direction = state.get_tiles(robot.coordinates)[0].direction
        assert robot.direction == tile_direction
Ejemplo n.º 12
0
def test_robots_order_on_start(robot_index, expected_coordinates):
    """
    The order of robots list should reflect their starting positions.
    First robot from the list stands on first start tile and so on.
    Assert the list is correcly created.
    Test to check the behaviour in Python 3.5.
    """
    state = State.get_start_state("maps/test_maps/test_start_direction.json")
    current_robot = state.robots[robot_index]
    assert current_robot.coordinates == expected_coordinates
Ejemplo n.º 13
0
def test_find_another_robots_start_coordinates():
    """
    Don't change coordinates to the last ones when another robot
    stands on them, pick the previous ones.
    """
    state = State.get_start_state("maps/test_maps/test_3.json")
    state.robots[0].start_coordinates = [(3, 2), (1, 0), (5, 5)]
    state.robots[1].coordinates = (5, 5)
    state.robots[0].find_free_start(state)
    assert state.robots[0].coordinates == (1, 0)
Ejemplo n.º 14
0
def test_start_state():
    """
    Assert that created start state (board and robots) contains
    the correct instances of objects.
    """
    ss = State.get_start_state("maps/test_maps/test_3.json")
    assert isinstance(ss, State)
    assert isinstance(ss.robots, list)
    assert isinstance(ss.robots[0], Robot)
    assert isinstance(ss._board, dict)
    assert isinstance(ss._board[0, 0], list)
    assert isinstance(ss._board[0, 0][0], Tile)
Ejemplo n.º 15
0
def test_state_from_dict():
    """
    Check if state.from_dict can load State from JSON.
    """
    state = State.get_start_state("maps/test_maps/test_3.json")
    data = state.whole_as_dict("maps/test_maps/test_3.json")
    state_recovered = State.whole_from_dict(data)

    assert state_recovered.robots[0].coordinates == (0, 1)
    assert state_recovered.robots[1].damages == 0
    assert state_recovered._board[0, 11][0].direction == Direction.N
    assert state_recovered._board[2, 5][0].name == "ground"
Ejemplo n.º 16
0
    def __init__(self, map_name):
        self.map_name = map_name
        self.state = State.get_start_state(map_name)

        self.available_robots = list(self.state.robots)
        # Dictionary {robot_name: ws_interface}
        self.assigned_robots = {}

        # A list of connected clients
        self.ws_receivers = []
        self.ws_interfaces = []
        # Routing table for aiohttp.web
        self.routes = []
Ejemplo n.º 17
0
    def __init__(self, map_name, players):
        # Attributes related to game logic
        self.map_name = map_name
        self.state = State.get_start_state(map_name, players)
        self.available_robots = list(self.state.robots)
        # Dictionary {robot_name: ws_interface}
        self.assigned_robots = {}

        # Attributes related to network connections
        # List of connected clients
        self.ws_receivers = []

        self.last_sent_log_position = 0
Ejemplo n.º 18
0
def test_robot_clears_attributes():
    """
    Assert robot has his attributes cleared and program emptied.
    """
    state = State.get_start_state("maps/test_maps/test_3.json")
    joe = state.robots[0]
    # Set imaginary program on robot's hand
    joe.program = [1, 2, 3, 4, 5]
    # Let's make less unblocked cards - should be 4
    joe.damages = 5
    joe.power_down = True
    joe.selection_confirmed = True
    joe.clear_robot_attributes(state)
    assert joe.selection_confirmed is False
    assert joe.power_down is False
    assert joe.program == [None, None, None, None, 5]
Ejemplo n.º 19
0
def test_play_game(test_name):
    """
    Play the game with given map.
    """
    map_file = Path("tests/") / test_name / "map.json"
    commands_file = Path("tests/") / test_name / "commands.yaml"
    if not commands_file.exists():
        commands_file = None

    state = State.get_start_state(map_file)
    stop_fields = get_start_tiles(state._board, "stop")

    commands = read_commands(commands_file)

    assign_cards_to_robots(commands, state)
    assign_prerequisites_to_robots(commands, state)

    # Leave it here for debugging purpose :)
    # for robot in state.robots:
    #     print(robot.program)
    #     print(robot.damages)
    #     print(robot.flags)
    #     print(robot.lives)
    #     print(robot.power_down)
    #     print(robot.start_coordinates)

    state.apply_all_effects(registers=get_registers(commands))
    compare_results_with_robots(commands, state)

    # for robot in state.robots:
    #     print(robot.program)
    #     print(robot.damages)
    #     print(robot.flags)
    #     print(robot.lives)
    #     print(robot.power_down)
    #     print(robot.start_coordinates)

    for robot in state.robots:
        robot_field = state.robots.index(robot) + 1
        assert robot.coordinates == stop_fields[robot_field]["coordinates"]
        assert robot.direction == stop_fields[robot_field]["tile_direction"]
Ejemplo n.º 20
0
from backend import State
from frontend import create_window, draw_state

# load JSON map data from the backend module
if len(sys.argv) == 1:
    map_name = "maps/test_effects.json"

# if other map should be loaded, use extra argument "maps/MAP_NAME.json"
# when calling game.py by Python
# for example: python game.py maps/test_2.json
else:
    map_name = sys.argv[1]

# Get start state of the game from the backend module.
state = State.get_start_state(map_name)

# Load pyglet graphic window from the frontend module.
window = create_window(state)


@window.event
def on_draw():
    """
    Draw the game state (board and robots).
    """

    window.clear()
    draw_state(state, window)

Ejemplo n.º 21
0
def test_confirmed_count_3():
    """
    Assert the count when none of robots confirms choice selection.
    """
    state = State.get_start_state("maps/test_maps/test_3.json")
    assert state.count_confirmed_selections() == 0