Class: Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-doom.rb

Instance Method Summary collapse

Constructor Details

#initialize(points, max_radius = 5, debug = false) ⇒ Finder

Returns a new instance of Finder.



17
18
19
20
21
# File 'lib/ruby-doom.rb', line 17

def initialize(points, max_radius=5, debug=false)
  @debug=debug
  @max_radius = max_radius
  @points = points
end

Instance Method Details

#good(candidate, sofar) ⇒ Object



57
58
59
60
# File 'lib/ruby-doom.rb', line 57

def good(candidate, sofar)  
  puts "Testing " + candidate.to_s unless !@debug
  @points.include?(candidate) && !sofar.include?(candidate)
end

#next(current, sofar) ⇒ Object



22
23
24
25
26
27
# File 'lib/ruby-doom.rb', line 22

def next(current, sofar)
  1.upto(@max_radius) do |x|
    points_at_radius(current, x).each {|p| return p if good(p, sofar) }
  end
  raise "Couldn't find next point!"
end

#points_at_radius(p, r) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby-doom.rb', line 28

def points_at_radius(p, r)
  res = []
  puts "checking for points around " + p.to_s + " at radius " + r.to_s unless !@debug
  # move up and then west to the upper left hand corner of the search box
  p = p.translate(-r, r)
  res << p
  # move east
  1.upto(r*2) {
    p = p.translate(1,0)
    res << p
  }
  # move south
  1.upto(r*2) {
    p = p.translate(0,-1)
    res << p
  }
  # move west
  1.upto(r*2) {
    p = p.translate(-1,0)
    res << p
  }
  # move north
  1.upto((r*2)-1) {
    p = p.translate(0,1)
    res << p
  }
  puts "points array = " + res.to_s unless !@debug
  res
end