class MazeGenerator
Public Class Methods
new(width, height)
click to toggle source
# File lib/maze/game/generator/maze_generator.rb, line 4 def initialize(width, height) @width = width @height = height end
Public Instance Methods
create()
click to toggle source
# File lib/maze/game/generator/maze_generator.rb, line 9 def create fields = init_field start_position = create_exit(fields) create_way(fields, start_position) fields end
Private Instance Methods
create_exit(fields)
click to toggle source
# File lib/maze/game/generator/maze_generator.rb, line 17 def create_exit(fields) exit_orientation = [:top, :right, :bottom, :left].sample case exit_orientation when :top x = rand(1..@width) start_position = [x, 1] fields[[x, 0]] = :exit when :bottom x = rand(1..@width) start_position = [x, @height] fields[[x, @height + 1]] = :exit when :left y = rand(1..@height) start_position = [1, y] fields[[0, y]] = :exit when :right y = rand(1..@height) start_position = [@width, y] fields[[@width + 1, y]] = :exit else start_position = [1, 1] fields[[0, 1]] = :exit end start_position end
create_way(fields, start_position)
click to toggle source
# File lib/maze/game/generator/maze_generator.rb, line 43 def create_way(fields, start_position) fields[start_position] = :way MazeWalker.new(fields, start_position).create_maze_way 3.times do MazeWalker.new(fields, start_position, 4, 20).create_maze_way end 9.times do MazeWalker.new(fields, start_position, 10, 20).create_maze_way end end
init_field()
click to toggle source
# File lib/maze/game/generator/maze_generator.rb, line 54 def init_field fields = Hash.new fields[:width] = @width fields[:height] = @height @width.times do |x| @height.times do |y| fields[[x + 1, y + 1]] = :wall end end fields end