def test_wind(): grid = Grid() grid.step(Action.RIGHT) grid.step(Action.RIGHT) grid.step(Action.RIGHT) grid.step(Action.RIGHT) assert grid.position == Position(row=2, column=4) grid.step(Action.RIGHT) assert grid.position == Position(row=1, column=5)
def generate_new_room(generator: Random): width = generator.randint(MIN_SIZE, MAX_SIZE) height = generator.randint(MIN_SIZE, MAX_SIZE) room_grid = SquareGrid(width, height) entities = {} for y in range(0, height): for x in range(0, width): if y == 0 or x == 0 or y == height - 1 or x == width - 1: wall_pos = Position(x, y) entities[wall_pos] = [Entity(WALL)] room_grid.walls.append(wall_pos) else: entities[Position(x, y)] = [Entity(FLOOR)] return Room(width, height, entities, room_grid)
def to_room_position(self, room: Room, generator: Random): position = Position() if self == Wall.WEST: position.x = 0 position.y = generator.randint(1, room.height - 2) elif self == Wall.NORTH: position.x = generator.randint(1, room.width - 2) position.y = 0 elif self == Wall.EAST: position.x = room.width - 1 position.y = generator.randint(1, room.height - 2) elif self == Wall.SOUTH: position.x = generator.randint(1, room.width - 2) position.y = room.height - 1 return position
def print_greedy_policy(q: dict[Position, dict[int]]): for row in range(0, NUM_ROWS): for col in range(0, NUM_COLS): action_values = q[Position(row=row, column=col)] best_action = max(action_values, key=action_values.get) print(f'{best_action}', end=',') print()
def test_reward(): grid = Grid() reward = grid.step(Action.RIGHT) assert reward == -1 grid = Grid() grid.position = Position(row=4, column=8) reward = grid.step(Action.LEFT) assert reward == 1
def initialize_policy() -> dict[Position, dict[int]]: result = {} for row in range(0, NUM_ROWS): for column in range(0, NUM_COLS): action_values = {} for action in Action: action_values[action.value] = 0 result[Position(row=row, column=column)] = action_values return result
def test_border(): grid = Grid() grid.step(Action.LEFT) assert grid.position == Position(row=3, column=0) grid.step(Action.UP) grid.step(Action.UP) grid.step(Action.UP) grid.step(Action.UP) assert grid.position == Position(row=0, column=0) grid = Grid() grid.step(Action.DOWN) grid.step(Action.DOWN) grid.step(Action.DOWN) grid.step(Action.DOWN) assert grid.position == Position(row=6, column=0) grid = Grid() grid.position = Position(row=3, column=9) grid.step(Action.RIGHT) assert grid.position == Position(row=3, column=9)
def place_stairs(room: Room, generator: Random): room_half_width = int(room.width / 2) room_half_height = int(room.height / 2) stairs_x = generator.randint(room_half_width - 1, room_half_width + 1) stairs_y = generator.randint(room_half_height - 1, room_half_height + 1) room.entities[Position(stairs_x, stairs_y)].append(Entity(STAIRS_UP))
walls = [wall for wall in Wall] walls.remove(entrance) place_doors(2, walls, room, generator) return room def generate_path(room: Room, generator: Random): doors_list = find_entities(DOOR_CLOSED, room) the_door = generator.choice(doors_list) hero = find_entities(HERO, room)[0] (came_from, cost_so_far) = a_star_search(room.grid, hero, the_door) return reconstruct_path(came_from, hero, the_door) DIRECTIONS = { Position(-1, 0): 'west', Position(0, -1): 'north', Position(1, 0): 'east', Position(0, 1): 'south' } def move_hero(hero_path: List[Position], room: Room): current_position = hero_path[0] next_position = hero_path[1] hero = room.entities[current_position].pop(len(room.entities[current_position]) - 1) room.entities[next_position].append(hero) hero_path.pop(0) return DIRECTIONS[next_position - current_position]
def test_right(): grid = Grid() grid.step(Action.RIGHT) assert grid.position == Position(row=3, column=1)
def test_is_terminal(): grid = Grid() grid.position = Position(row=4, column=8) assert grid.is_terminal() is False grid.step(Action.LEFT) assert grid.is_terminal() is True
def test_up(): grid = Grid() grid.step(Action.UP) assert grid.position == Position(row=2, column=0)
def test_left(): grid = Grid() grid.step(Action.RIGHT) grid.step(Action.LEFT) assert grid.position == Position(row=3, column=0)
def test_down(): grid = Grid() grid.step(Action.DOWN) assert grid.position == Position(row=4, column=0)