Ruby Maze Generator
this is a straight forward of the wikipedia example of a maze generator, the example was written using python, this code is a port of this code in ruby, then i compacted the syntax a little bit to fit in one window :D, that was the hardest part, i had to remove and join some lines and put the algorithm directly in teh draw method of the window to save a couple more lines.
this generator will generate a random maze every one second, you can play with dimensions, colors, resolution of a maze to get some other variants.
Gist : https://gist.github.com/9809196
#!/usr/bin/env ruby
# Author : Emad Elsaid (https://github.com/blazeeboy)
require 'gosu'
include Gosu
DIMENSION, SPLITS, COLOR = 200, 50, Color::GREEN
# credits to: http://en.wikipedia.org/wiki/Maze_generation_algorithm
class GameWindow < Window
def initialize
super DIMENSION, DIMENSION, false, 1000
self.caption = "Maze"
end
def draw
complexity, density = 0.75, 0.75
shape = [(SPLITS / 2) * 2 , (SPLITS / 2) * 2 ] # Only odd shapes
# Adjust complexity and density relative to maze size
complexity = (complexity * (5 * (shape[0] + shape[1])))
density = (density * (shape[0] / 2 * shape[1] / 2))
z = Array.new(shape[0]){ Array.new(shape[1], 0) } # Build actual maze
# Fill borders
z[0] = z[-1].fill 1
z.each { |l| l[0] = l[-1] = 1 }
for i in 0...density # Make isles
x, y = rand(shape[1] / 2) * 2, rand(shape[0] / 2) * 2
z[y][x] = 1
for j in 0...complexity
neighbours = []
neighbours << [y, x - 2] if x > 1
neighbours << [y, x + 2] if x < shape[1] - 2
neighbours << [y - 2, x] if y > 1
neighbours << [y + 2, x] if y < shape[0] - 2
unless neighbours.empty?
y_,x_ = neighbours[rand(neighbours.size - 1)]
if z[y_][x_] == 0
z[y_ + (y - y_) / 2][x_ + (x - x_) / 2] = z[y_][x_] = 1
x, y = x_, y_
end
end
end
end
size = DIMENSION/SPLITS
z.each_with_index do |row, x|
row.each_with_index do |value, y|
draw_quad x*size, y*size, COLOR,
x*size+size, y*size, COLOR,
x*size+size, y*size+size, COLOR,
x*size, y*size+size, COLOR if value==1
end
end
end
end
GameWindow.new.show
Written by Emad Elsaid
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#