Commit cc324fb

mo k <mo@mokhan.ca>
2012-08-11 06:02:34
remove puts and collapse a switch.
1 parent 8ae1be6
Changed files (2)
lib/console.rb
@@ -9,16 +9,16 @@ class Console
   def initialize(input, output)
     @input = input
     @output = output
+    @headings = [North.new, East.new, West.new, South.new]
   end
   def run
     edge = ask('enter size of terrain:', ' ')
     terrain = Terrain.new({:x => edge[0].to_i, :y => edge[1].to_i})
 
     landing = ask('enter landing coordinate:', ' ')
-    rover = Rover.new(get_heading(landing[2]), {:x => landing[0].to_i, :y => landing[1].to_i})
+    rover = Rover.new(heading_for(landing[2]), {:x => landing[0].to_i, :y => landing[1].to_i})
 
     ask('enter instructions:', //).each do |instruction|
-      puts instruction
       case(instruction)
       when 'M'
         rover.move_forward(terrain)
@@ -34,19 +34,11 @@ class Console
 
   def ask(message, split)
     @output.puts message
+    @output.print '> '
     @input.gets.split(split)
   end
 
-  def get_heading(pneumonic)
-    case(pneumonic)
-    when 'N'
-      North.new
-    when 'E'
-      East.new
-    when 'W'
-      West.new
-    when 'S'
-      South.new
-    end
+  def heading_for(pneumonic)
+    @headings.find { |heading| heading.to_s == pneumonic }
   end
 end
lib/terrain.rb
@@ -1,12 +1,10 @@
 class Terrain
   def initialize(edge_of_map)
     @map = edge_of_map
-    puts @map
   end
 
   def move_forward( heading, location)
     new_location = heading.forward(location.clone)
-    puts new_location
     if(is_on_terrain(new_location, :x) && is_on_terrain(new_location, :y))
       location[:x] = new_location[:x]
       location[:y] = new_location[:y]