Commit 29a6269

mo k <mo@mokhan.ca>
2012-08-12 23:09:15
process instructions once instead of 3 times.
1 parent a3c52a1
lib/east.rb
@@ -9,7 +9,8 @@ class East
     degrees > 0 ? turn_right : turn_left
   end
   def forward(location)
-    location[:x] = location[:x].to_i + 1
+    location[:x] = location[:x] + 1
+    location
   end
   def to_s
     'E'
lib/move_forward.rb
@@ -3,7 +3,9 @@ class MoveForward
     @rover = rover
   end
   def run(instruction)
-    @rover.forward if matches(instruction)
+    if matches(instruction)
+      @rover.forward
+    end
   end
   def matches(item)
     'M' == item
lib/navigate_rover.rb
@@ -12,13 +12,13 @@ class NavigateRover
     @instructions = instructions
   end
   def run
-    rover = create_plateau(@plateau_size).deploy_rover_to(@heading, @x, @x)
-    commands_for(rover).each do |item| 
-      @instructions.split(//).each do |instruction|
+    rover = create_plateau(@plateau_size).deploy_rover_to(@heading, @x, @y)
+    @instructions.split(//).each do |instruction|
+      commands_for(rover).each do |item| 
         item.run(instruction)
       end
     end
-    rover
+    rover.to_s
   end
   def create_plateau(plateau_size)
     coordinates = plateau_size.split(' ')
lib/north.rb
@@ -10,6 +10,7 @@ class North
   end
   def forward(location)
     location[:y] = location[:y].to_i + 1
+    location
   end
   def to_s
     'N'
lib/plateau.rb
@@ -6,7 +6,7 @@ require "south"
 
 class Plateau
   def initialize(x,y)
-    @map = {:x => x, :y => y}
+    @map = {:x => x.to_i, :y => y.to_i}
     @directions = {:N => North.new, :E => East.new, :W => West.new, :S => South.new}
   end
 
@@ -31,6 +31,6 @@ class Plateau
   end
 
   def deploy_rover_to(heading, x, y)
-    Rover.new(@directions[heading.to_sym], {:x => x, :y => y}, self)
+    Rover.new(@directions[heading.to_sym],  x,  y, self)
   end
 end
lib/rover.rb
@@ -1,19 +1,17 @@
 class Rover
   attr_reader :location
-  def initialize(heading, coordinates, plateau)
+  def initialize(heading, x, y, plateau)
     @heading = heading
-    @location = coordinates
+    @location = {:x => x, :y => y}
     @plateau = plateau
   end
   def heading
     @heading.class.name.downcase.to_sym
   end
   def rotate(degrees)
-    puts "rotate #{degrees}"
     @heading = @heading.rotate(degrees)
   end
   def forward
-    puts "move forward"
     @plateau.move_forward(@heading, @location)
   end
   def to_s
lib/south.rb
@@ -9,7 +9,8 @@ class South
     degrees > 0 ? turn_right : turn_left
   end
   def forward(location)
-    location[:y] = location[:y].to_i - 1
+    location[:y] = location[:y] - 1
+    location
   end
   def to_s
     'S'
lib/turn_left.rb
@@ -3,7 +3,9 @@ class TurnLeft
     @rover = rover
   end
   def run(instruction)
-    @rover.rotate(-90) if matches(instruction)
+    if matches(instruction)
+      @rover.rotate(-90) 
+    end
   end
   def matches(item)
     'L' == item
lib/turn_right.rb
@@ -3,7 +3,9 @@ class TurnRight
     @rover = rover
   end
   def run(instruction)
-    @rover.rotate(90) if matches(instruction)
+    if matches(instruction)
+      @rover.rotate(90) 
+    end
   end
   def matches(item)
     'R' == item
lib/west.rb
@@ -8,8 +8,9 @@ class West
   def rotate(degrees)
     degrees > 0 ? turn_right : turn_left
   end
-  def forward(current_location)
-    current_location[:x] = current_location[:x].to_i - 1
+  def forward(location)
+    location[:x] = location[:x] - 1
+    location
   end
   def to_s
     'W'
spec/integration/navigate_rover_spec.rb
@@ -15,14 +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' }
-        #let(:instructions){'MMRMMRMRRM'}
+      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.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
@@ -3,7 +3,7 @@ require 'spec_helper'
 describe Rover do
   def create_sut(heading, x = 0, y = 0)
     directions = {:north => North.new, :east => East.new, :west => West.new, :south => South.new}
-    Rover.new(directions[heading],{ :x => x,:y => y }, plateau)
+    Rover.new(directions[heading], x, y , plateau)
   end
   let(:plateau) { fake }