From 04ed77494e66a990e266188e951d8aa37449eb9b Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 19 Jul 2025 23:45:23 -0400 Subject: [PATCH] Sixth version now makes a maze, but no rooms. --- phase_six.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/phase_six.py b/phase_six.py index d95eb32..aad8167 100644 --- a/phase_six.py +++ b/phase_six.py @@ -2,27 +2,95 @@ import curses import sys import random +WALL = 1 +SPACE = 0 class Map: def __init__(self, width, height): self.width = width self.height = height grid = self.make_grid() + self.hunt_and_kill(grid) self.render_map(grid) def make_grid(self): grid = [] for y in range(0, self.height): - grid.append([0] * self.width) + grid.append([WALL] * self.width) return grid - def walk(self, grid, x, y): - pass + 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 + + found = self.neighborsAB(grid, x, y) + for found_x, found_y in found: + if grid[found_y][found_x] == SPACE: + return [[x,y],[found_x, found_y]] + return None + 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]] + + result = [] + for x,y in points: + if self.inbounds(x, y): + 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]] + + result = [] + for x,y in points: + if self.inbounds(x, y) and grid[y][x] == WALL: + result.append([x,y]) + return result + + def complete(self, grid): + for y in range(1, self.height, 2): + for x in range(1, self.width, 2): + if(grid[y][x] != SPACE): + return False + return True + def hunt_and_kill(self, grid): - return None + on_x = 1 + on_y = 1 + + while True: + n = self.neighbors(grid, on_x, on_y) + if len(n) == 0: + 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 + row = (on_y + found_y) // 2 + col = (on_x + found_x) // 2 + grid[row][col] = SPACE + else: + nb_x, nb_y = random.choice(n) + grid[nb_y][nb_x] = SPACE + row = (nb_y + on_y) // 2 + col = (nb_x + on_x) // 2 + grid[row][col] = SPACE + on_x, on_y = nb_x, nb_y + + assert self.complete(grid), "Maze is not complete." + def render_map(self, grid): self.map = [] @@ -113,7 +181,7 @@ class GameEngine: def __init__(self, ui): self.ui = ui self.player = Player(4, 4) - self.map = Map(11,11) + self.map = Map(77,19) self.ui = ui ui.set_map(self.map)