def test_equal(self): loc_1 = Location(3, 3) loc_2 = Location(3, 3) self.assertEqual(loc_1, loc_2) self.assertFalse(loc_1 != loc_2)
def __init__(self): self._cell_cache = {} [self.get_cell(Location(x, y)) for x in range(5) for y in range(5)]
def test_can_move_to(): map = WorldMapCreator.generate_world_map_from_cells_data(_generate_cells()) target = Location(1, 1) assert map.can_move_to(target) == True
def get_map(self): world_map = WorldMap.generate_empty_map(1, 5, self.settings) world_map = WorldMapStaticSpawnDecorator(world_map, Location(-2, 0)) world_map.get_cell(Location(2, 0)).generates_score = True return world_map
def test_successful_move_east_twice_action(self): game_state = GameState(InfiniteMap(), self.avatar_manager) action.MoveAction(self.avatar, {'x': 1, 'y': 0}).process(game_state.world_map) action.MoveAction(self.avatar, {'x': 1, 'y': 0}).process(game_state.world_map) self.assertEqual(self.avatar.location, Location(2, 0))
def test_spawn_is_static(self): decorated_map = WorldMapStaticSpawnDecorator(WorldMap({}, {}), Location(3, 7)) for _ in range(5): self.assertEqual(decorated_map.get_random_spawn_location(), Location(3, 7))
from simulation.world_map import WorldMap from .dummy_avatar import ( DeadDummy, DummyAvatar, DummyAvatarManager, MoveEastDummy, MoveNorthDummy, MoveSouthDummy, MoveWestDummy, WaitDummy, ) from .maps import InfiniteMap, MockCell, MockPickup from .mock_communicator import MockCommunicator from .mock_game_state import MockGameState ORIGIN = Location(0, 0) RIGHT_OF_ORIGIN = Location(1, 0) FIVE_RIGHT_OF_ORIGIN = Location(5, 0) ABOVE_ORIGIN = Location(0, 1) FIVE_RIGHT_OF_ORIGIN_AND_ONE_ABOVE = Location(5, 1) SETTINGS = { "TARGET_NUM_CELLS_PER_AVATAR": 0, "TARGET_NUM_PICKUPS_PER_AVATAR": 0, "TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR": 0, "SCORE_DESPAWN_CHANCE": 0, "PICKUP_SPAWN_CHANCE": 0, }
def test_y_off_map(self): world_map = WorldMap(self._generate_grid(), self.settings) for x in (0, 1): self.assertFalse(world_map.is_on_map(Location(x, -1))) self.assertFalse(world_map.is_on_map(Location(x, 2)))
def test_random_spawn_location_successful(self): cell = MockCell() world_map = WorldMap({Location(0, 0): cell}, self.settings) self.assertEqual(world_map.get_random_spawn_location(), cell.location)
def __iter__(self): return ((self.get_cell(Location(x, y)) for y in xrange(self.min_y(), self.max_y() + 1)) for x in xrange(self.min_x(), self.max_x() + 1))
def test_x_off_map(self): world_map = WorldMap(self._generate_grid(), self.settings) for y in (0, 1): self.assertFalse(world_map.is_on_map(Location(-1, y))) self.assertFalse(world_map.is_on_map(Location(2, y)))
def _add_horizontal_layer(self, y): for x in xrange(self.min_x(), self.max_x() + 1): self._grid[Location(x, y)] = Cell(Location(x, y))
def _add_vertical_layer(self, x): for y in xrange(self.min_y(), self.max_y() + 1): self._grid[Location(x, y)] = Cell(Location(x, y))
def get_cell_by_coords(self, x, y): return self.get_cell(Location(x, y))
def test_attackable_avatar_returns_avatar(self): avatar = DummyAvatar() world_map = AvatarMap(avatar) self.assertEqual(world_map.attackable_avatar(Location(0, 0)), avatar)
def test_random_spawn_location_with_no_candidates(self): grid = self._generate_grid(1, 1) world_map = WorldMap(grid, self.settings) grid[Location(0, 0)].avatar = True with self.assertRaises(IndexError): world_map.get_random_spawn_location()
def test_retrieve_negative(self): world_map = WorldMap(self._generate_grid(3, 3), self.settings) self.assertTrue(world_map.is_on_map(Location(-1, -1)))
def test_can_move_to(self): world_map = WorldMap(self._generate_grid(), self.settings) target = Location(1, 1) self.assertTrue(world_map.can_move_to(target))
def _grid_from_list(self, in_list): out = {} for x, column in enumerate(in_list): for y, cell in enumerate(column): out[Location(x, y)] = cell return out
def test_cannot_move_to_cell_off_grid(self): world_map = WorldMap(self._generate_grid(), self.settings) target = Location(4, 1) self.assertFalse(world_map.can_move_to(target))
class DummyAvatarManager(AvatarManager): avatars = [MoveEastDummy(1, Location(0, -1))]
def test_cannot_move_to_uninhabitable_cell(self): target = Location(0, 0) cell = MockCell(target, habitable=False) world_map = WorldMap({target: cell}, self.settings) self.assertFalse(world_map.can_move_to(target))
from __future__ import absolute_import import unittest from simulation import action from simulation import event from simulation.avatar.avatar_manager import AvatarManager from simulation.direction import EAST from simulation.game_state import GameState from simulation.location import Location from .dummy_avatar import MoveDummy from .maps import InfiniteMap, EmptyMap, AvatarMap ORIGIN = Location(x=0, y=0) EAST_OF_ORIGIN = Location(x=1, y=0) NORTH_OF_ORIGIN = Location(x=0, y=1) class TestAction(unittest.TestCase): def setUp(self): self.avatar = MoveDummy(1, ORIGIN, EAST) self.other_avatar = MoveDummy(2, EAST_OF_ORIGIN, EAST) self.avatar_manager = AvatarManager() def test_successful_move_action(self): # Move north game_state = GameState(InfiniteMap(), self.avatar_manager) action.MoveAction(self.avatar, {'x': 0, 'y': 1}).process(game_state.world_map) target_cell = game_state.world_map.get_cell(NORTH_OF_ORIGIN) self.assertEqual(self.avatar.location, NORTH_OF_ORIGIN)
def test_cannot_move_to_habited_cell(self): target = Location(0, 0) cell = MockCell(target, avatar=DummyAvatar(target, 0)) world_map = WorldMap({target: cell}, self.settings) target = Location(0, 0) self.assertFalse(world_map.can_move_to(target))
def test_calculate_orientation(self): # East movement self.avatar.location = Location(0, 0) self.avatar.previous_location = Location(-1, 0) self.assertEqual(self.avatar.calculate_orientation(), "east")
def test_empty_grid(self): world_map = WorldMap({}, self.settings) self.assertFalse(world_map.is_on_map(Location(0, 0)))
def get_random_spawn_location(self): return Location(10, 10)
def test_attackable_avatar_returns_none(self): world_map = WorldMap(self._generate_grid(), self.settings) for x in (0, 1): for y in (0, 1): self.assertIsNone(world_map.attackable_avatar(Location(x, y)))
def test_cannot_move_to_cell_off_grid(): map = WorldMapCreator.generate_world_map_from_cells_data(_generate_cells()) target = Location(4, 1) assert map.can_move_to(target) == False
def test_serialise(self): loc = Location(3, 9) expected = {'x': 3, 'y': 9} self.assertEqual(loc.serialise(), expected)