Commit 728fb21

mo k <mo@mokhan.ca>
2012-08-11 19:33:37
update specs so that if you go out of bounds you end up on the other side of the planet.
1 parent feadfcb
lib/east.rb
@@ -6,7 +6,7 @@ class East
     North.new
   end
   def forward(location)
-    location[:x] = location[:x]+1
+    location[:x] = location[:x].to_i + 1
   end
   def to_s
     'E'
lib/navigate_rover.rb
@@ -1,6 +1,20 @@
 class NavigateRover
   def run(plateau_size, starting_position, instructions)
-    '1 3 N'
-    #terrain = Terrain.new(plateau_size.split(' ')[0], plateau_size.split(' ')[1])
+    terrain = create_terrain(plateau_size)
+    rover = terrain.deploy_rover_to(starting_position.split()[2], starting_position.split()[0].to_i, starting_position.split()[1].to_i)
+    instructions.split(//).each do |instruction|
+      case(instruction)
+      when 'M'
+        rover.move_forward(terrain)
+      when 'L'
+        rover.turn_left
+      when 'R'
+        rover.turn_right
+      end
+    end
+    rover
+  end
+  def create_terrain(plateau_size)
+    Terrain.new(plateau_size.split(' ')[0], plateau_size.split(' ')[1])
   end
 end
lib/south.rb
@@ -6,7 +6,7 @@ class South
     East.new
   end
   def forward(location)
-    location[:y] = location[:y]-1
+    location[:y] = location[:y].to_i - 1
   end
   def to_s
     'S'
lib/terrain.rb
@@ -1,25 +1,34 @@
 class Terrain
-  def initialize(edge_of_map)
-    @map = edge_of_map
+  def initialize(x,y)
+    @map = {:x => x, :y => y}
     @headings = [North.new, East.new, West.new, South.new]
   end
 
   def move_forward( heading, location)
     new_location = heading.forward(location.clone)
-    if(is_on_terrain(new_location, :x) && is_on_terrain(new_location, :y))
+    puts "#{new_location[:x]}"
+    if(new_location[:x] > @map[:x])
+      location[:x] = new_location[:x] - @map[:x]
+    elsif (new_location[:x] < 0)
+      location[:x] = @map[:x]
+    else
       location[:x] = new_location[:x]
+    end
+
+    if(new_location[:y] > @map[:y])
+      location[:y] = new_location[:y] - @map[:y]
+    elsif (new_location[:y] < 0)
+      location[:y] = @map[:y]
+    else
       location[:y] = new_location[:y]
     end
   end
 
-  def deploy_rover_to(heading, coordinates)
-    Rover.new(heading_for(heading), coordinates)
+  def deploy_rover_to(heading, x, y)
+    Rover.new(heading_for(heading), {:x => x, :y => y})
   end
 
   private 
-  def is_on_terrain(new_location, symbol)
-    new_location[symbol] < @map[symbol] && new_location[symbol] > 0
-  end
   def heading_for(pneumonic)
     @headings.find { |heading| heading.to_s == pneumonic }
   end
lib/west.rb
@@ -6,7 +6,7 @@ class West
     South.new
   end
   def forward(current_location)
-    current_location[:x] = current_location[:x]-1
+    current_location[:x] = current_location[:x].to_i - 1
   end
   def to_s
     'W'
spec/integration/navigate_rover_spec.rb
@@ -1,11 +1,26 @@
-require "spec_helper"
+require "navigate_rover"
 
 describe NavigateRover do
   let(:sut) { NavigateRover.new }
 
   describe "when navigating" do
-    it "should reply with the proper final coordinates" do
-      sut.run('5 5', '1 2 N', 'LMLMLMLMM').should == '1 3 N'
+    context "plateau of 5 by 5" do
+      let(:plateau) { '5 5' }
+
+      context "starting at 1 2 N" do
+        let(:starting_position) { '1 2 N' }
+
+        it "should reply with the proper final coordinates" do
+          sut.run(plateau, starting_position, 'LMLMLMLMM').should == '1 3 N'
+        end
+      end
+      #context "starting at 3 3 E" do
+      #let(:starting_position) { '3 3 E' }
+
+      #it "should reply with the proper final coordinates" do
+      #sut.run(plateau, starting_position, 'MMRMMRMRRM').should == '5 1 E'
+      #end
+      #end
     end
   end
 end
spec/unit/terrain_spec.rb
@@ -1,25 +1,26 @@
 require 'spec_helper'
 
 describe Terrain do
-  let(:sut) {Terrain.new({:x => 3, :y => 3})}
+  let(:sut) {Terrain.new(3,  3)}
 
   context "when moving forward" do
     context "when the next position is to far east" do
-      it "should not let you move forward" do
-        @location[:x].should == 3
+      it "should move to the other side of the board" do
+        @location[:x].should == 1
         @location[:y].should == 0
       end
       before do
         @heading = fake
         @location = {:x => 3, :y => 0}
         @heading.stub(:forward).with(@location).and_return({:x => 4, :y => 0})
+
         sut.move_forward(@heading, @location)
       end
     end
 
     context "when the next position is to far west" do
-      it "should not let you move forward" do
-        @location[:x].should == 0
+      it "should move to the other side of the board" do
+        @location[:x].should == 3
         @location[:y].should == 0
       end
       before do
@@ -31,9 +32,9 @@ describe Terrain do
     end
 
     context "when the next position is to far north" do
-      it "should not let you move forward" do
+      it "should move to the other side of the board" do
         @location[:x].should == 0
-        @location[:y].should == 3
+        @location[:y].should == 1
       end
       before do
         @heading = fake
@@ -44,9 +45,9 @@ describe Terrain do
     end
 
     context "when the next position is to far south" do
-      it "should not let you move forward" do
+      it "should move to the other side of the board" do
         @location[:x].should == 0
-        @location[:y].should == 0
+        @location[:y].should == 3
       end
       before do
         @heading = fake