Sixth version now makes a maze, but no rooms.

master
Zed A. Shaw 1 week ago
parent e21ee55b9c
commit 04ed77494e
  1. 78
      phase_six.py

@ -2,27 +2,95 @@ import curses
import sys import sys
import random import random
WALL = 1
SPACE = 0
class Map: class Map:
def __init__(self, width, height): def __init__(self, width, height):
self.width = width self.width = width
self.height = height self.height = height
grid = self.make_grid() grid = self.make_grid()
self.hunt_and_kill(grid)
self.render_map(grid) self.render_map(grid)
def make_grid(self): def make_grid(self):
grid = [] grid = []
for y in range(0, self.height): for y in range(0, self.height):
grid.append([0] * self.width) grid.append([WALL] * self.width)
return grid return grid
def walk(self, grid, x, y): def find_coord(self, grid):
pass 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): 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): def render_map(self, grid):
self.map = [] self.map = []
@ -113,7 +181,7 @@ class GameEngine:
def __init__(self, ui): def __init__(self, ui):
self.ui = ui self.ui = ui
self.player = Player(4, 4) self.player = Player(4, 4)
self.map = Map(11,11) self.map = Map(77,19)
self.ui = ui self.ui = ui
ui.set_map(self.map) ui.set_map(self.map)

Loading…
Cancel
Save