Commit 976c985

mo k <mo@mokhan.ca>
2012-08-12 22:13:26
remove turn_right and turn_left methods from rover and replace with Direction objects that can rotate the rover by given degree.
1 parent 15980ba
lib/east.rb
@@ -5,6 +5,9 @@ class East
   def turn_left
     North.new
   end
+  def turn(degrees)
+    degrees > 0 ? turn_right : turn_left
+  end
   def forward(location)
     location[:x] = location[:x].to_i + 1
   end
lib/navigate_rover.rb
@@ -40,7 +40,7 @@ class TurnLeft
     @rover = rover
   end
   def run(instruction)
-    @rover.turn_left if matches(instruction)
+    @rover.turn(Left.new) if matches(instruction)
   end
   def matches(item)
     'L' == item
@@ -51,9 +51,19 @@ class TurnRight
     @rover = rover
   end
   def run(instruction)
-    @rover.turn_right if matches(instruction)
+    @rover.turn(Right.new) if matches(instruction)
   end
   def matches(item)
     'R' == item
   end
 end
+class Left
+  def turn_from(heading)
+    heading.turn(-90)
+  end
+end
+class Right
+  def turn_from(heading)
+    heading.turn(90)
+  end
+end
lib/north.rb
@@ -5,6 +5,9 @@ class North
   def turn_left
     West.new
   end
+  def turn(degrees)
+    degrees > 0 ? turn_right : turn_left
+  end
   def forward(location)
     location[:y] = location[:y].to_i + 1
   end
lib/rover.rb
@@ -8,11 +8,8 @@ class Rover
   def heading
     @heading.class.name.downcase.to_sym
   end
-  def turn_right
-    @heading = @heading.turn_right
-  end
-  def turn_left
-    @heading = @heading.turn_left
+  def turn(direction)
+    @heading = direction.turn_from(@heading)
   end
   def forward
     @plateau.move_forward(@heading, @location)
@@ -21,4 +18,3 @@ class Rover
     "#{@location[:x]} #{@location[:y]} #{@heading}"
   end
 end
-
lib/south.rb
@@ -5,6 +5,9 @@ class South
   def turn_left
     East.new
   end
+  def turn(degrees)
+    degrees > 0 ? turn_right : turn_left
+  end
   def forward(location)
     location[:y] = location[:y].to_i - 1
   end
lib/west.rb
@@ -5,6 +5,9 @@ class West
   def turn_left
     South.new
   end
+  def turn(degrees)
+    degrees > 0 ? turn_right : turn_left
+  end
   def forward(current_location)
     current_location[:x] = current_location[:x].to_i - 1
   end
spec/integration/navigate_rover_spec.rb
@@ -15,13 +15,14 @@ describe NavigateRover do
           sut.run.should == '1 3 N'
         end
       end
-      #context "starting at 3 3 E" do
-        #let(:starting_position) { '3 3 E' }
+      context "starting at 3 3 E" do
+        let(:starting_position) { '3 3 E' }
+        let(:instructions){'MMRMMRMRRM'}
 
-        #it "should reply with the proper final coordinates" do
-          #sut.run(plateau, starting_position, 'MMRMMRMRRM').should == '5 1 E'
-        #end
-      #end
+        it "should reply with the proper final coordinates" do
+          sut.run.should == '5 1 E'
+        end
+      end
     end
   end
 end
spec/unit/rover_spec.rb
@@ -11,13 +11,13 @@ describe Rover do
     let(:sut) { create_sut :north, 0, 0 }
     context "when turning right" do
       it "should face east" do
-        sut.turn_right
+        sut.turn(Right.new)
         sut.heading.should == :east
       end
     end
     context "when turning left" do
       it "should face west" do
-        sut.turn_left
+        sut.turn(Left.new)
         sut.heading.should == :west
       end
     end
@@ -27,13 +27,13 @@ describe Rover do
     let(:sut) { create_sut :south, 0, 3 }
     context "when turning right" do
       it "should face west" do
-        sut.turn_right
+        sut.turn(Right.new)
         sut.heading.should == :west
       end
     end
     context "when turning left" do
       it "should face east" do
-        sut.turn_left
+        sut.turn(Left.new)
         sut.heading.should == :east
       end
     end
@@ -44,13 +44,13 @@ describe Rover do
 
     context "when turning right" do
       it "should face south" do
-        sut.turn_right
+        sut.turn(Right.new)
         sut.heading.should == :south
       end
     end
     context "when turning left" do
       it "should face north" do
-        sut.turn_left
+        sut.turn(Left.new)
         sut.heading.should == :north
       end
     end
@@ -61,13 +61,13 @@ describe Rover do
 
     context "when turning right" do
       it "should face north" do
-        sut.turn_right
+        sut.turn(Right.new)
         sut.heading.should == :north
       end
     end
     context "when turning left" do
       it "should face south" do
-        sut.turn_left
+        sut.turn(Left.new)
         sut.heading.should == :south
       end
     end