def test_reenter_maze(self): test_cases = [] test_cases.append((NORTH, SOUTH)) test_cases.append((EAST, WEST)) test_cases.append((SOUTH, NORTH)) test_cases.append((WEST, EAST)) test_case_second_entrances = { NORTH: Room((0, 2), EXITS[NORTH]), EAST: Room((1, -2), EXITS[EAST]), SOUTH: Room((2, 2), EXITS[SOUTH]), WEST: Room((1, 2), EXITS[WEST]) } for (original_direction, expected_direction) in test_cases: maze = Maze() second_entrance = test_case_second_entrances[original_direction] maze.add_room(second_entrance) maze.entrances.second = second_entrance.vector walker = Walker(None, original_direction) movement = Reenter(walker, maze) movement.move() self.assertEqual(walker.direction, expected_direction) self.assertEqual(walker.room, second_entrance)
def test_walk_north_into_new_room(self): maze = Maze() start_room = Room((1, 2), EXITS[SOUTH]) maze.add_room(start_room) end_room = Room((0, 2), EXITS[SOUTH]) walker = Walker(start_room, NORTH) walk = Walk(walker, maze) walk.move() self.assertEqual(walker.direction, NORTH) self.assertEqual(walker.room, end_room) self.assertEqual(maze.has_room(end_room.vector), True) self.assertEqual(maze.rooms[end_room.vector], end_room) self.assertEqual(maze.rooms[start_room.vector].exits, 3)
def test_walk_west_into_new_room(self): maze = Maze() start_room = Room((0, 0), EXITS[NORTH]) maze.add_room(start_room) end_room = Room((0, 1), EXITS[EAST]) walker = Walker(start_room, WEST) walk = Walk(walker, maze) walk.move() self.assertEqual(walker.direction, WEST) self.assertEqual(walker.room, end_room) self.assertEqual(maze.has_room(end_room.vector), True) self.assertEqual(maze.rooms[end_room.vector], end_room) self.assertEqual(maze.rooms[start_room.vector].exits, 5)
def test_walk_back_north_into_known_room_through_known_entrance(self): known_exits = 3 start_room = Room((1, 0), EXITS[NORTH]) end_room = Room((0, 0), known_exits) maze = Maze() maze.add_room(start_room) maze.add_room(end_room) walker = Walker(start_room, NORTH) walk = Walk(walker, maze) walk.move() self.assertEqual(walker.direction, NORTH) self.assertEqual(walker.room, end_room) self.assertEqual(maze.rooms[start_room.vector].exits, EXITS[NORTH]) self.assertEqual(maze.rooms[end_room.vector].exits, known_exits)
def test_walk_back_west_into_known_room_through_known_entrance(self): known_exits = 12 start_room = Room((1, 1), EXITS[WEST]) end_room = Room((1, 2), known_exits) maze = Maze() maze.add_room(start_room) maze.add_room(end_room) walker = Walker(start_room, WEST) walk = Walk(walker, maze) walk.move() self.assertEqual(walker.direction, WEST) self.assertEqual(walker.room, end_room) self.assertEqual(maze.rooms[start_room.vector].exits, EXITS[WEST]) self.assertEqual(maze.rooms[end_room.vector].exits, known_exits)
def test_walk_back_east_into_known_room_through_new_entrance(self): previously_known_exits = EXITS[EAST] now_known_exits = 12 start_room = Room((1, 1), EXITS[EAST]) end_room = Room((1, 0), previously_known_exits) maze = Maze() maze.add_room(start_room) maze.add_room(end_room) walker = Walker(start_room, EAST) walk = Walk(walker, maze) walk.move() self.assertEqual(walker.direction, EAST) self.assertEqual(walker.room, end_room) self.assertEqual(maze.rooms[start_room.vector].exits, EXITS[EAST]) self.assertEqual(maze.rooms[end_room.vector].exits, now_known_exits)
def test_exit_maze(self): maze = Maze() exit_room = Room((2, 2), 0) maze.add_room(exit_room) walker = Walker(exit_room, WEST) movement = Exit(walker, maze) movement.move() self.assertEqual(walker.room, None) self.assertEqual(maze.rooms[exit_room.vector].exits, WEST) self.assertEqual(maze.entrances.second, (2, 2))
def test_enter_maze(self): maze = Maze() start_room = Room((0, 0), EXITS[NORTH]) walker = Walker(None, SOUTH) enter = Enter(walker, maze) enter.move() self.assertEqual(walker.direction, SOUTH) self.assertEqual(walker.room, start_room) self.assertEqual(maze.has_room(start_room.vector), True) self.assertEqual(maze.rooms[start_room.vector], start_room) self.assertEqual(maze.entrances.first, (0, 0))
def move(self): start_room = self.__walker.room (row, col) = start_room.vector (delta_row, delta_col) = Walk.vector[self.__walker.direction] next_room_vector = (row + delta_row, col + delta_col) next_room_entrance = Walk.entrance_exit_map[self.__walker.direction] if self.__maze.has_room(next_room_vector): # Walker has returned to a known room. next_room = self.__maze.rooms[next_room_vector] if self.__hasDiscoveredExit(next_room.exits, next_room_entrance): # Walker has found a new entrance\exit to the room next_room.exits = next_room.exits | next_room_entrance else: # Walker has moved into a newly-discovered room. new_room = Room(next_room_vector, next_room_entrance) self.__maze.add_room(new_room) # Update exits of room that walker has just left to show newly # discovered exit. start_room.exits = start_room.exits | EXITS[ self.__walker.direction] self.__walker.room = self.__maze.rooms[next_room_vector]
def make_room(room_number): return Room(room_number)
def move(self): entrance_room = Room((0, 0), EXITS[NORTH]) self.__maze.add_room(entrance_room) self.__maze.entrances.first = (0, 0) self.__walker.room = entrance_room
def create_maze(): """Series of operations which create our Maze.""" maze = Maze() room1 = Room(1) room2 = Room(2) thedoor = Door(room1, room2) maze.add_room(room1) maze.add_room(room2) room1.set_side(Direction.NORTH, Wall()) room1.set_side(Direction.EAST, thedoor) room1.set_side(Direction.SOUTH, Wall()) room1.set_side(Direction.WEST, Wall()) room2.set_side(Direction.NORTH, Wall()) room2.set_side(Direction.EAST, Wall()) room2.set_side(Direction.SOUTH, Wall()) room2.set_side(Direction.WEST, thedoor) return maze
def create_maze(): """Series of operations which create our Maze.""" maze = Maze() room1 = Room(1) room2 = Room(2) thedoor = Door(room1, room2) maze.add_room(room1) maze.add_room(room2) room1.set_side(Direction.NORTH, Wall()) room1.set_side(Direction.EAST, thedoor) room1.set_side(Direction.SOUTH, Wall()) room1.set_side(Direction.WEST, Wall()) room2.set_side(Direction.NORTH, Wall()) room2.set_side(Direction.EAST, Wall()) room2.set_side(Direction.SOUTH, Wall()) room2.set_side(Direction.WEST, thedoor) return maze