Class: Amaze::Algorithm::Sidewinder

Inherits:
Amaze::Algorithm show all
Defined in:
lib/amaze/algorithm/sidewinder.rb

Instance Attribute Summary

Attributes inherited from Amaze::Algorithm

#duration

Instance Method Summary collapse

Methods inherited from Amaze::Algorithm

#on

Instance Method Details

#speedObject



35
36
37
# File 'lib/amaze/algorithm/sidewinder.rb', line 35

def speed
  0.1
end

#statusObject



39
40
41
# File 'lib/amaze/algorithm/sidewinder.rb', line 39

def status
  "Sidewinder algorithm: #{duration}s"
end

#work(grid) ⇒ Object



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
# File 'lib/amaze/algorithm/sidewinder.rb', line 4

def work grid
  run = []
  grid.each_cell do |cell|
    run << cell
    
    dig_north = cell.north && (!cell.east || rand(2) == 0)
    
    yield Stat.new(                                 # visualize
      current: run,                                 #
      pause: dig_north,                             #
      info: "Run set: #{run.size}") if block_given? #
    
    # dig north if there is a cell north AND
    #   you can't dig east OR
    #   you choose randomly
    if dig_north
      # dig north (choose one cell of the run set)
      run.sample.tap {|c| c.link c.north }
    
    # dig east if there is a cell east
    elsif cell.east
      # dig east (keep run set)
      cell.link cell.east
      next
    end

    run.clear
  end
  grid
end