diff --git a/phase_01.py b/phase_01.py index ec0656b..364b2c3 100644 --- a/phase_01.py +++ b/phase_01.py @@ -42,7 +42,7 @@ def create_ui(stdscr, width, height, status_height): return win, status def move_player(player_y, player_x, target_y, target_x): - if MAP[target_y][target_x] != '#': + if MAP[target_y, target_x] != '#': return target_y, target_x else: return player_y, player_x diff --git a/phase_02.py b/phase_02.py index 813c1c5..c77fc92 100644 --- a/phase_02.py +++ b/phase_02.py @@ -67,7 +67,7 @@ class UI: return y, x def move_player(player_y, player_x, target_y, target_x): - if MAP[target_y][target_x] != '#': + if MAP[target_y, target_x] != '#': return target_y, target_x else: return player_y, player_x diff --git a/phase_03.py b/phase_03.py index 50dfc55..179e065 100644 --- a/phase_03.py +++ b/phase_03.py @@ -4,7 +4,7 @@ import numpy as np class Map: def __init__(self): - self.map = [ + self.map = np.array([ list("####################"), list("###....#############"), list("###....#############"), @@ -12,10 +12,10 @@ class Map: list("#############.######"), list("#############....###"), list("#############....###"), - list("####################")] + list("####################")], dtype=str) def move_player(self, player_y, player_x, target_y, target_x): - if self.map[target_y][target_x] != '#': + if self.map[target_y, target_x] != '#': return target_y, target_x else: return player_y, player_x diff --git a/phase_04.py b/phase_04.py index cda758e..637bae2 100644 --- a/phase_04.py +++ b/phase_04.py @@ -4,7 +4,7 @@ import numpy as np class Map: def __init__(self): - self.map = [ + self.map = np.array([ list("####################"), list("###....#############"), list("###....#############"), @@ -12,10 +12,10 @@ class Map: list("#############.######"), list("#############....###"), list("#############....###"), - list("####################")] + list("####################")], dtype=str) def move_player(self, player, target_y, target_x): - if self.map[target_y][target_x] != '#': + if self.map[target_y, target_x] != '#': player.y = target_y player.x = target_x diff --git a/phase_05.py b/phase_05.py index 47675dc..e2c9f15 100644 --- a/phase_05.py +++ b/phase_05.py @@ -4,7 +4,7 @@ import numpy as np class Map: def __init__(self): - self.map = [ + self.map = np.array([ list("####################"), list("###....#############"), list("###....#############"), @@ -12,10 +12,10 @@ class Map: list("#############.######"), list("#############....###"), list("#############....###"), - list("####################")] + list("####################")], dtype=str) def move_player(self, player, target_y, target_x): - if self.map[target_y][target_x] != '#': + if self.map[target_y, target_x] != '#': player.y = target_y player.x = target_x diff --git a/phase_06.py b/phase_06.py index f0f1b68..129d0f5 100644 --- a/phase_06.py +++ b/phase_06.py @@ -3,8 +3,14 @@ import sys import random import numpy as np -WALL = 1 -SPACE = 0 +WALL = '#' +SPACE = '.' + +def compass(x, y, offset=1): + return [[x, y - offset], # North + [x, y + offset], # South + [x + offset, y], # East + [x - offset, y]] # West class Map: def __init__(self, width, height): @@ -24,25 +30,21 @@ class Map: return grid def make_grid(self): - grid = [] - for y in range(0, self.height): - grid.append([WALL] * self.width) - - return grid + return np.full((self.height, self.width), WALL, dtype=str) def make_room(self, grid, x, y, size): for row in range(y, y+size): for col in range(x, x+size): - grid[row][col] = SPACE + grid[row, col] = SPACE def find_coord(self, grid): for y in range(1, self.height, 2): for x in range(1, self.width, 2): - if grid[y][x] != WALL: continue + if grid[y, x] != WALL: continue - found = self.neighborsAB(grid, x, y) + found = self.neighbors(grid, x, y) for found_x, found_y in found: - if grid[found_y][found_x] == SPACE: + if grid[found_y, found_x] == SPACE: return [[x,y],[found_x, found_y]] return None @@ -50,11 +52,8 @@ class Map: def inbounds(self, x, y): return x >= 0 and x < self.width and y >= 0 and y < self.height - def neighborsAB(self, grid, x, y): - points = [[x, y - 2], - [x, y + 2], - [x - 2, y], - [x + 2, y]] + def neighbors(self, grid, x, y): + points = compass(x, y, 2) result = [] for x,y in points: @@ -62,15 +61,11 @@ class Map: result.append([x,y]) return result - def neighbors(self, grid, x, y): - points = [[x, y - 2], - [x, y + 2], - [x - 2, y], - [x + 2, y]] - + def neighbor_walls(self, grid, x, y): + neighbors = self.neighbors(grid, x, y) result = [] - for x,y in points: - if self.inbounds(x, y) and grid[y][x] == WALL: + for x,y in neighbors: + if grid[y, x] == WALL: result.append([x,y]) return result @@ -80,43 +75,36 @@ class Map: dead_ends = [] while True: - n = self.neighbors(grid, on_x, on_y) + n = self.neighbor_walls(grid, on_x, on_y) if len(n) == 0: dead_ends.append([on_x, on_y]) t = self.find_coord(grid) if t == None: break on_x, on_y = t[0] found_x, found_y = t[1] - grid[on_y][on_x] = SPACE + grid[on_y, on_x] = SPACE row = (on_y + found_y) // 2 col = (on_x + found_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE else: nb_x, nb_y = random.choice(n) - grid[nb_y][nb_x] = SPACE + grid[nb_y, nb_x] = SPACE row = (nb_y + on_y) // 2 col = (nb_x + on_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE on_x, on_y = nb_x, nb_y return dead_ends def render_map(self, grid): - self.map = [] + self.map = np.full((self.height, self.width), '#', dtype=str) for y, y_line in enumerate(grid): - cur_row = "" - for x, char in enumerate(y_line): - if char == 0: - cur_row += '.' - else: - cur_row += '#' - self.map.append(cur_row) - + self.map[y, x] = char def move_player(self, player, target_x, target_y): - if self.map[target_y][target_x] != '#': + if self.map[target_y, target_x] != '#': player.y = target_y player.x = target_x diff --git a/phase_07.py b/phase_07.py index 3ad8624..12fccf9 100644 --- a/phase_07.py +++ b/phase_07.py @@ -6,6 +6,12 @@ import numpy as np WALL = '#' SPACE = '.' +def compass(x, y, offset=1): + return [[x, y - offset], # North + [x, y + offset], # South + [x + offset, y], # East + [x - offset, y]] # West + class Map: def __init__(self, width, height): self.width = width @@ -20,7 +26,7 @@ class Map: while True: y = random.randrange(0, self.height) x = random.randrange(0, self.width) - if self.map[y][x] == SPACE: + if self.map[y, x] == SPACE: return x, y def sample_rooms(self, grid, dead_ends, size, count): @@ -36,16 +42,16 @@ class Map: def make_room(self, grid, x, y, size): for row in range(y, y+size): for col in range(x, x+size): - grid[row][col] = SPACE + grid[row, col] = SPACE def find_coord(self, grid): for y in range(1, self.height, 2): for x in range(1, self.width, 2): - if grid[y][x] != WALL: continue + if grid[y, x] != WALL: continue found = self.neighbors(grid, x, y) for found_x, found_y in found: - if grid[found_y][found_x] == SPACE: + if grid[found_y, found_x] == SPACE: return [[x,y],[found_x, found_y]] return None @@ -54,10 +60,7 @@ class Map: return x >= 0 and x < self.width and y >= 0 and y < self.height def neighbors(self, grid, x, y): - points = [[x, y - 2], - [x, y + 2], - [x - 2, y], - [x + 2, y]] + points = compass(x, y, 2) result = [] for x,y in points: @@ -69,7 +72,7 @@ class Map: neighbors = self.neighbors(grid, x, y) result = [] for x,y in neighbors: - if grid[y][x] == WALL: + if grid[y, x] == WALL: result.append([x,y]) return result @@ -86,33 +89,29 @@ class Map: if t == None: break on_x, on_y = t[0] found_x, found_y = t[1] - grid[on_y][on_x] = SPACE + grid[on_y, on_x] = SPACE row = (on_y + found_y) // 2 col = (on_x + found_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE else: nb_x, nb_y = random.choice(n) - grid[nb_y][nb_x] = SPACE + grid[nb_y, nb_x] = SPACE row = (nb_y + on_y) // 2 col = (nb_x + on_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE on_x, on_y = nb_x, nb_y return dead_ends def render_map(self, grid): - self.map = [] + self.map = np.full((self.height, self.width), '#', dtype=str) for y, y_line in enumerate(grid): - cur_row = "" - for x, char in enumerate(y_line): - cur_row += char - self.map.append(cur_row) - + self.map[y, x] = char def move_player(self, player, target_x, target_y): - if self.map[target_y][target_x] != '#': + if self.map[target_y, target_x] != '#': player.y = target_y player.x = target_x @@ -160,7 +159,7 @@ class UI: self.status.addstr(1, 1, f"PLAYER AT {player.x},{player.y}") def draw_actor(self, actor): - assert self.map.map[actor.y][actor.x] != '#', f"WHAT? actor at {actor.x},{actor.y} but that's a wall!" + assert self.map.map[actor.y, actor.x] != '#', f"WHAT? actor at {actor.x},{actor.y} but that's a wall!" # actor has to be moved in by 1 for the border self.win.addstr(actor.y, actor.x, actor.symbol, curses.A_BOLD) diff --git a/phase_08.py b/phase_08.py index 0123df9..44c2ea0 100644 --- a/phase_08.py +++ b/phase_08.py @@ -6,6 +6,12 @@ import numpy as np WALL = '#' SPACE = '.' +def compass(x, y, offset=1): + return [[x, y - offset], # North + [x, y + offset], # South + [x + offset, y], # East + [x - offset, y]] # West + class Map: def __init__(self, width, height): self.width = width @@ -36,16 +42,16 @@ class Map: def make_room(self, grid, x, y, size): for row in range(y, y+size): for col in range(x, x+size): - grid[row][col] = SPACE + grid[row, col] = SPACE def find_coord(self, grid): for y in range(1, self.height, 2): for x in range(1, self.width, 2): - if grid[y][x] != WALL: continue + if grid[y, x] != WALL: continue found = self.neighbors(grid, x, y) for found_x, found_y in found: - if grid[found_y][found_x] == SPACE: + if grid[found_y, found_x] == SPACE: return [[x,y],[found_x, found_y]] return None @@ -54,10 +60,7 @@ class Map: return x >= 0 and x < self.width and y >= 0 and y < self.height def neighbors(self, grid, x, y): - points = [[x, y - 2], - [x, y + 2], - [x - 2, y], - [x + 2, y]] + points = compass(x, y, 2) result = [] for x,y in points: @@ -69,7 +72,7 @@ class Map: neighbors = self.neighbors(grid, x, y) result = [] for x,y in neighbors: - if grid[y][x] == WALL: + if grid[y, x] == WALL: result.append([x,y]) return result @@ -86,33 +89,30 @@ class Map: if t == None: break on_x, on_y = t[0] found_x, found_y = t[1] - grid[on_y][on_x] = SPACE + grid[on_y, on_x] = SPACE row = (on_y + found_y) // 2 col = (on_x + found_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE else: nb_x, nb_y = random.choice(n) - grid[nb_y][nb_x] = SPACE + grid[nb_y, nb_x] = SPACE row = (nb_y + on_y) // 2 col = (nb_x + on_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE on_x, on_y = nb_x, nb_y return dead_ends def render_map(self, grid): - self.map = [] + self.map = np.full((self.height, self.width), '#', dtype=str) for y, y_line in enumerate(grid): - cur_row = "" - for x, char in enumerate(y_line): - cur_row += char - self.map.append(cur_row) + self.map[y, x] = char def collision(self, target_x, target_y): # remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE - return self.map[target_y][target_x] == '#' + return self.map[target_y, target_x] == '#' def draw(self, win): for y, row in enumerate(self.map): diff --git a/phase_09.py b/phase_09.py index dfbdfee..a2fdc9d 100644 --- a/phase_09.py +++ b/phase_09.py @@ -6,6 +6,12 @@ import numpy as np WALL = '#' SPACE = '.' +def compass(x, y, offset=1): + return [[x, y - offset], # North + [x, y + offset], # South + [x + offset, y], # East + [x - offset, y]] # West + class Player: def __init__(self, x, y): self.x = x @@ -54,12 +60,12 @@ class Map: def make_room(self, grid, x, y, size): for row in range(y, y+size): for col in range(x, x+size): - grid[row][col] = SPACE + grid[row, col] = SPACE def find_coord(self, grid): for y in range(1, self.height, 2): for x in range(1, self.width, 2): - if grid[y][x] != WALL: continue + if grid[y, x] != WALL: continue found = self.neighbors(grid, x, y) for found_x, found_y in found: @@ -72,10 +78,7 @@ class Map: return x >= 0 and x < self.width and y >= 0 and y < self.height def neighbors(self, grid, x, y): - points = [[x, y - 2], - [x, y + 2], - [x - 2, y], - [x + 2, y]] + points = compass(x, y, 2) result = [] for x,y in points: @@ -87,7 +90,7 @@ class Map: neighbors = self.neighbors(grid, x, y) result = [] for x,y in neighbors: - if grid[y][x] == WALL: + if grid[y, x] == WALL: result.append([x,y]) return result @@ -104,33 +107,30 @@ class Map: if t == None: break on_x, on_y = t[0] found_x, found_y = t[1] - grid[on_y][on_x] = SPACE + grid[on_y, on_x] = SPACE row = (on_y + found_y) // 2 col = (on_x + found_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE else: nb_x, nb_y = random.choice(n) - grid[nb_y][nb_x] = SPACE + grid[nb_y, nb_x] = SPACE row = (nb_y + on_y) // 2 col = (nb_x + on_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE on_x, on_y = nb_x, nb_y return dead_ends def render_map(self, grid): - self.map = [] + self.map = np.full((self.height, self.width), '#', dtype=str) for y, y_line in enumerate(grid): - cur_row = "" - for x, char in enumerate(y_line): - cur_row += char - self.map.append(cur_row) + self.map[y, x] = char def collision(self, target_x, target_y): # remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE - return self.map[target_y][target_x] == '#' + return self.map[target_y, target_x] == '#' def draw(self, win): for y, row in enumerate(self.map): diff --git a/phase_10.py b/phase_10.py index b71d5a5..c6ed8f0 100644 --- a/phase_10.py +++ b/phase_10.py @@ -6,6 +6,12 @@ import numpy as np WALL = '#' SPACE = '.' +def compass(x, y, offset=1): + return [[x, y - offset], # North + [x, y + offset], # South + [x + offset, y], # East + [x - offset, y]] # West + class Player: def __init__(self, x, y): self.x = x @@ -54,16 +60,16 @@ class Map: def make_room(self, grid, x, y, size): for row in range(y, y+size): for col in range(x, x+size): - grid[row][col] = SPACE + grid[row, col] = SPACE def find_coord(self, grid): for y in range(1, self.height, 2): for x in range(1, self.width, 2): - if grid[y][x] != WALL: continue + if grid[y, x] != WALL: continue found = self.neighbors(grid, x, y) for found_x, found_y in found: - if grid[found_y][found_x] == SPACE: + if grid[found_y, found_x] == SPACE: return [[x,y],[found_x, found_y]] return None @@ -72,10 +78,7 @@ class Map: return x >= 0 and x < self.width and y >= 0 and y < self.height def neighbors(self, grid, x, y): - points = [[x, y - 2], - [x, y + 2], - [x - 2, y], - [x + 2, y]] + points = compass(x, y, 2) result = [] for x,y in points: @@ -87,7 +90,7 @@ class Map: neighbors = self.neighbors(grid, x, y) result = [] for x,y in neighbors: - if grid[y][x] == WALL: + if grid[y, x] == WALL: result.append([x,y]) return result @@ -104,33 +107,30 @@ class Map: if t == None: break on_x, on_y = t[0] found_x, found_y = t[1] - grid[on_y][on_x] = SPACE + grid[on_y, on_x] = SPACE row = (on_y + found_y) // 2 col = (on_x + found_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE else: nb_x, nb_y = random.choice(n) - grid[nb_y][nb_x] = SPACE + grid[nb_y, nb_x] = SPACE row = (nb_y + on_y) // 2 col = (nb_x + on_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE on_x, on_y = nb_x, nb_y return dead_ends def render_map(self, grid): - self.map = [] + self.map = np.full((self.height, self.width), '#', dtype=str) for y, y_line in enumerate(grid): - cur_row = "" - for x, char in enumerate(y_line): - cur_row += char - self.map.append(cur_row) + self.map[y, x] = char def collision(self, target_x, target_y): # remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE - return self.map[target_y][target_x] == '#' + return self.map[target_y, target_x] == '#' def draw(self, win): for y, row in enumerate(self.map): diff --git a/phase_11.py b/phase_11.py index 7c94bc2..baf72ee 100644 --- a/phase_11.py +++ b/phase_11.py @@ -61,16 +61,16 @@ class Map: def make_room(self, grid, x, y, size): for row in range(y, y+size): for col in range(x, x+size): - grid[row][col] = SPACE + grid[row, col] = SPACE def find_coord(self, grid): for y in range(1, self.height, 2): for x in range(1, self.width, 2): - if grid[y][x] != WALL: continue + if grid[y, x] != WALL: continue found = self.neighbors(grid, x, y) for found_x, found_y in found: - if grid[found_y][found_x] == SPACE: + if grid[found_y, found_x] == SPACE: return [[x,y],[found_x, found_y]] return None @@ -91,7 +91,7 @@ class Map: neighbors = self.neighbors(grid, x, y) result = [] for x,y in neighbors: - if grid[y][x] == WALL: + if grid[y, x] == WALL: result.append([x,y]) return result @@ -108,33 +108,30 @@ class Map: if t == None: break on_x, on_y = t[0] found_x, found_y = t[1] - grid[on_y][on_x] = SPACE + grid[on_y, on_x] = SPACE row = (on_y + found_y) // 2 col = (on_x + found_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE else: nb_x, nb_y = random.choice(n) - grid[nb_y][nb_x] = SPACE + grid[nb_y, nb_x] = SPACE row = (nb_y + on_y) // 2 col = (nb_x + on_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE on_x, on_y = nb_x, nb_y return dead_ends def render_map(self, grid): - self.map = [] + self.map = np.full((self.height, self.width), '#', dtype=str) for y, y_line in enumerate(grid): - cur_row = "" - for x, char in enumerate(y_line): - cur_row += char - self.map.append(cur_row) + self.map[y, x] = char def collision(self, target_x, target_y): # remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE - return self.map[target_y][target_x] == WALL + return self.map[target_y, target_x] == WALL def draw(self, win): for y, row in enumerate(self.map): diff --git a/phase_12.py b/phase_12.py index 93914f9..ff494a7 100644 --- a/phase_12.py +++ b/phase_12.py @@ -62,16 +62,16 @@ class Map: def make_room(self, grid, x, y, size): for row in range(y, y+size): for col in range(x, x+size): - grid[row][col] = SPACE + grid[row, col] = SPACE def find_coord(self, grid): for y in range(1, self.height, 2): for x in range(1, self.width, 2): - if grid[y][x] != WALL: continue + if grid[y, x] != WALL: continue found = self.neighbors(grid, x, y) for found_x, found_y in found: - if grid[found_y][found_x] == SPACE: + if grid[found_y, found_x] == SPACE: return [[x,y],[found_x, found_y]] return None @@ -92,7 +92,7 @@ class Map: neighbors = self.neighbors(grid, x, y) result = [] for x,y in neighbors: - if grid[y][x] == WALL: + if grid[y, x] == WALL: result.append([x,y]) return result @@ -109,29 +109,25 @@ class Map: if t == None: break on_x, on_y = t[0] found_x, found_y = t[1] - grid[on_y][on_x] = SPACE + grid[on_y, on_x] = SPACE row = (on_y + found_y) // 2 col = (on_x + found_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE else: nb_x, nb_y = random.choice(n) - grid[nb_y][nb_x] = SPACE + grid[nb_y, nb_x] = SPACE row = (nb_y + on_y) // 2 col = (nb_x + on_x) // 2 - grid[row][col] = SPACE + grid[row, col] = SPACE on_x, on_y = nb_x, nb_y return dead_ends - def render_map(self, grid): - self.map = [] + self.map = np.full((self.height, self.width), '#', dtype=str) for y, y_line in enumerate(grid): - cur_row = "" - for x, char in enumerate(y_line): - cur_row += char - self.map.append(cur_row) + self.map[y, x] = char def collision(self, target_x, target_y): # remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE