Skip to content

Instantly share code, notes, and snippets.

@ijunaid8989
Created October 15, 2018 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijunaid8989/bc93939b15f53ea6820906343c91dba7 to your computer and use it in GitHub Desktop.
Save ijunaid8989/bc93939b15f53ea6820906343c91dba7 to your computer and use it in GitHub Desktop.
class Maze
# Generate a maze.
def generate(width, height)
@width = width
@height = height
@maze = Array.new
for y in 0 ... @height
for x in 0 ... @width
@maze[y * @width + x] = 1
end
end
@maze[1 * @width + 1] = 0
carve(1, 1)
@maze[0 * @width + 1] = 0
@maze[(@height - 1) * @width + @width - 2] = 0
end
# Show the maze.
def show
for y in 0 ... @height
for x in 0 ... @width
chars = if @maze[y * @width + x] == 1 then
[ "[", "]" ]
else
[ " ", " " ]
end
chars.each { |c| putc(c) }
end
puts
end
end
private
# Start carving at x, y.
def carve(x, y)
def update_pos(dir, x, y)
case dir
when 0
return x + 1, y + 0
when 1
return x + 0, y + 1
when 2
return x - 1, y + 0
when 3
return x + 0, y - 1
end
end
dir = rand(4)
count = 0
while count < 4
x1, y1 = update_pos(dir, x, y)
x2, y2 = update_pos(dir, x1, y1)
if x2 > 0 and x2 < @width and y2 > 0 and y2 < @height
if @maze[y1 * @width + x1] == 1 and @maze[y2 * @width + x2] == 1
@maze[y1 * @width + x1] = 0
@maze[y2 * @width + x2] = 0
carve(x2, y2)
end
end
count += 1
dir = (dir + 1) % 4
end
end
end
mz = Maze.new
mz.generate(10, 10)
mz.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment