summaryrefslogtreecommitdiff
path: root/main.rb
blob: 57f48892d4b2d7cbffdaf7155a7d580a93f6b580 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require_relative './pos.rb'
require_relative './maze.rb'

class MazeGenerator
  attr_reader :maze

  def initialize(width, height)
    @maze = Maze.new(width, height)
  end

  def generate()
    @visitedCells = [@currentPos]
    @stack = [Pos.new(0, 0)]

    while !@stack.empty?
      step()
    end
  end

  def step()
    neighbors = @maze.neighbors(@stack.last)
    neighbors.select! do |neighbor|
      !@visitedCells.include? neighbor
    end

    if neighbors.empty?
      @stack.pop()
    else
      randomNeighbor = neighbors.sample
      print "Removing wall between ", @stack.last, " and ", randomNeighbor, "\n"
      @maze.set(@stack.last, randomNeighbor.dir_from(@stack.last), false)

      @stack.push(randomNeighbor)
      @visitedCells.push(randomNeighbor)
    end
  end
end

mazeGenerator = MazeGenerator.new(118, 30)
mazeGenerator.generate
puts mazeGenerator.maze