Commit 1f69f4f

mo k <mo@mokhan.ca>
2012-08-11 22:56:07
refactoring towards initializing the rover with the plateau it is on.
1 parent 68b921b
lib/console.rb
@@ -1,4 +1,4 @@
-require 'terrain'
+require 'plateau'
 require 'rover'
 require 'north'
 require 'east'
lib/navigate_rover.rb
@@ -12,7 +12,7 @@ class NavigateRover
     @instructions.split(//).each do |instruction|
       case(instruction)
       when 'M'
-        rover.move_forward(terrain)
+        rover.forward
       when 'L'
         rover.turn_left
       when 'R'
@@ -22,6 +22,7 @@ class NavigateRover
     rover
   end
   def create_terrain(plateau_size)
-    Terrain.new(plateau_size.split(' ')[0], plateau_size.split(' ')[1])
+    coordinates = plateau_size.split(' ')
+    Plateau.new(coordinates[0], coordinates[1])
   end
 end
lib/terrain.rb → lib/plateau.rb
@@ -1,4 +1,4 @@
-class Terrain
+class Plateau
   def initialize(x,y)
     @map = {:x => x, :y => y}
     @headings = [North.new, East.new, West.new, South.new]
@@ -25,7 +25,7 @@ class Terrain
   end
 
   def deploy_rover_to(heading, x, y)
-    Rover.new(heading_for(heading), {:x => x, :y => y})
+    Rover.new(heading_for(heading), {:x => x, :y => y}, self)
   end
 
   private 
lib/rover.rb
@@ -1,8 +1,9 @@
 class Rover
   attr_reader :location
-  def initialize(heading, coordinates)
+  def initialize(heading, coordinates, plateau = nil)
     @heading = heading
     @location = coordinates
+    @plateau = plateau
   end
   def heading
     @heading.class.name.downcase.to_sym
@@ -13,8 +14,11 @@ class Rover
   def turn_left
     @heading = @heading.turn_left
   end
-  def move_forward(terrain)
-    terrain.move_forward(@heading, @location)
+  def forward
+    @plateau.move_forward(@heading, @location)
+  end
+  def move_forward(plateau)
+    plateau.move_forward(@heading, @location)
   end
   def to_s
     "#{@location[:x]} #{@location[:y]} #{@heading}"
spec/unit/terrain_spec.rb → spec/unit/plateau_spec.rb
@@ -1,7 +1,7 @@
 require 'spec_helper'
 
-describe Terrain do
-  let(:sut) {Terrain.new(3,  3)}
+describe Plateau do
+  let(:sut) {Plateau.new(3,  3)}
 
   context "when moving forward" do
     context "when the next position is to far east" do