Class: Knossos::Algorithm::HuntAndKill

Inherits:
Object
  • Object
show all
Defined in:
lib/knossos/algorithm/hunt_and_kill.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ HuntAndKill

Returns a new instance of HuntAndKill.



6
7
8
# File 'lib/knossos/algorithm/hunt_and_kill.rb', line 6

def initialize(**options)
  options = defaults.merge(options)
end

Instance Attribute Details

#biasObject (readonly)

Returns the value of attribute bias.



4
5
6
# File 'lib/knossos/algorithm/hunt_and_kill.rb', line 4

def bias
  @bias
end

Instance Method Details

#carve(grid:, seed: nil) ⇒ Object



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
# File 'lib/knossos/algorithm/hunt_and_kill.rb', line 10

def carve(grid:, seed: nil)
  srand(seed || Kernel.srand)

  current = grid.random_cell

  while current
    unvisited_neighbors = grid.neighborhood(current).select { |n| n.links.empty? }

    if unvisited_neighbors.any?
      neighbor = unvisited_neighbors.sample
      grid.build_passage(current, neighbor)
      current = neighbor
    else
      current = nil

      grid.each_cell do |cell|
        visited_neighbors = grid.neighborhood(cell).select { |n| n.links.any? }
        if cell.links.empty? && visited_neighbors.any?
          current = cell

          neighbor = visited_neighbors.sample
          grid.build_passage(current, neighbor)

          break
        end
      end
    end
  end

  grid
end