Class: Swint::Map
- Inherits:
-
Object
- Object
- Swint::Map
- Defined in:
- lib/swint/map.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#start ⇒ Object
readonly
Returns the value of attribute start.
Instance Method Summary collapse
- #draw_map(robots, flags) ⇒ Object
-
#initialize ⇒ Map
constructor
A new instance of Map.
- #load_from_xml(xml) ⇒ Object
- #print_map(robots, flags) ⇒ Object
- #robotkey_at(robots, pos) ⇒ Object
- #state_of_field(pos) ⇒ Object
Constructor Details
#initialize ⇒ Map
Returns a new instance of Map.
12 13 14 15 |
# File 'lib/swint/map.rb', line 12 def initialize @min, @max = [0, 0], [0, 0] # THIS IS AN ASUMPTION! @fields = Hash.new(:outside) end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
10 11 12 |
# File 'lib/swint/map.rb', line 10 def fields @fields end |
#start ⇒ Object (readonly)
Returns the value of attribute start.
10 11 12 |
# File 'lib/swint/map.rb', line 10 def start @start end |
Instance Method Details
#draw_map(robots, flags) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/swint/map.rb', line 55 def draw_map(robots, flags) output = [] for y in @min[1]..@max[1] line = '' line += ' '*(y) for x in @min[0]..@max[0] s = state_of_field([x, y]) c = s!=:robot ? CHAR[state_of_field([x, y])] : robotkey_at(robots, [x, y]).to_s pstr = [x, y].add(@start.collect { |s| -s })*'x' c = flags[pstr] if flags[pstr] line += c+' ' end output << line end output.join("\n")+"\n" end |
#load_from_xml(xml) ⇒ Object
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/swint/map.rb', line 17 def load_from_xml(xml) doc = REXML::Document.new(xml) doc.elements.each('map/field') do |e| v = e.attribute('v').value.to_i u = e.attribute('u').value.to_i s = e.attribute('state').value.to_sym w = nil if s==:object w = e.attribute('weight').value.to_i s = :free end f = Swint::Field.new(s, [u, v], w) @fields[f.pos] = f @min[0] = u if u < @min[0] @min[1] = v if v < @min[1] @max[0] = u if u > @max[0] @max[1] = v if v > @max[1] if s==:start @start = [u, v] # f.robot = true # HACK! end end self end |
#print_map(robots, flags) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/swint/map.rb', line 47 def print_map(robots, flags) w = ((@max[0]-@min[0])*2+(@max[1]-@min[1])) border = '='*(w+6) puts border print draw_map(robots, flags) puts border end |
#robotkey_at(robots, pos) ⇒ Object
72 73 74 |
# File 'lib/swint/map.rb', line 72 def robotkey_at(robots, pos) robots.each { |k, r| return k if r.pos==pos } end |
#state_of_field(pos) ⇒ Object
42 43 44 45 |
# File 'lib/swint/map.rb', line 42 def state_of_field(pos) v = @fields[pos] v.is_a?(Symbol) ? v : v.state end |