summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo k <mo@mokhan.ca>2012-02-13 16:46:27 -0700
committermo k <mo@mokhan.ca>2012-02-13 16:46:27 -0700
commit02e8a7c716002db61ee09a2d91508bb1d6d2f719 (patch)
tree1e198c03eb659bf537b88aa200f0a84091831a72
parentec2cb99c0e3eed4bd704cbbfc7f3af1368f4ac3a (diff)
considering a different direction where the terrain actually moves the rover after doing a bounds check.
-rwxr-xr-x[-rw-r--r--]lib/rover.rb1
-rwxr-xr-xlib/terrain.rb13
-rwxr-xr-xspec/terrain_specs.rb75
-rwxr-xr-xspec/ts_all.rb1
4 files changed, 90 insertions, 0 deletions
diff --git a/lib/rover.rb b/lib/rover.rb
index 42fdfda..fa5d976 100644..100755
--- a/lib/rover.rb
+++ b/lib/rover.rb
@@ -14,6 +14,7 @@ class Rover
@heading = @heading.turn_left
end
def move_forward(terrain)
+# terrain.move_forward(@heading, @location)
@heading.forward(@location, terrain)
end
end
diff --git a/lib/terrain.rb b/lib/terrain.rb
new file mode 100755
index 0000000..795bf6d
--- /dev/null
+++ b/lib/terrain.rb
@@ -0,0 +1,13 @@
+class Terrain
+ def initialize(edge_of_map)
+ @map = edge_of_map
+ end
+
+ def move_forward( heading, location)
+ new_location = heading.forward(location)
+ if( new_location[:x] < @map[:x] && new_location[:x] > 0 && new_location[:y] < @map[:y] && new_location[:y] > 0)
+ location[:x] = new_location[:x]
+ location[:y] = new_location[:y]
+ end
+ end
+end
diff --git a/spec/terrain_specs.rb b/spec/terrain_specs.rb
new file mode 100755
index 0000000..52aaacb
--- /dev/null
+++ b/spec/terrain_specs.rb
@@ -0,0 +1,75 @@
+require 'terrain'
+
+describe Terrain do
+ before do
+ @sut = Terrain.new({:x => 3, :y => 3})
+ end
+ describe "when moving forward" do
+ describe "when the next position is to far east" do
+ it "should not let you move forward" do
+ @location[:x].must_equal 3
+ @location[:y].must_equal 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
+
+ describe "when the next position is to far west" do
+ it "should not let you move forward" do
+ @location[:x].must_equal 0
+ @location[:y].must_equal 0
+ end
+ before do
+ @heading = fake
+ @location = {:x => 0, :y => 0}
+ @heading.stub(:forward).with(@location).and_return({:x => -1, :y => 0})
+ @sut.move_forward(@heading, @location)
+ end
+ end
+
+ describe "when the next position is to far north" do
+ it "should not let you move forward" do
+ @location[:x].must_equal 0
+ @location[:y].must_equal 3
+ end
+ before do
+ @heading = fake
+ @location = {:x => 0, :y => 3}
+ @heading.stub(:forward).with(@location).and_return({:x => 0, :y => 4})
+ @sut.move_forward(@heading, @location)
+ end
+ end
+
+ describe "when the next position is to far south" do
+ it "should not let you move forward" do
+ @location[:x].must_equal 0
+ @location[:y].must_equal 0
+ end
+ before do
+ @heading = fake
+ @location = {:x => 0, :y => 0}
+ @heading.stub(:forward).with(@location).and_return({:x => 0, :y => -1})
+ @sut.move_forward(@heading, @location)
+ end
+ end
+
+ describe "when the next position is just fine" do
+ it "should move position forward" do
+ @location[:x].must_equal 1
+ @location[:y].must_equal 1
+ end
+ before do
+ @heading = fake
+ @location = {:x => 0, :y => 0}
+ @heading.stub(:forward).with(@location).and_return({:x => 1, :y => 1})
+ @sut.move_forward(@heading, @location)
+ end
+
+ end
+
+ end
+end
diff --git a/spec/ts_all.rb b/spec/ts_all.rb
index 36e285f..1311bca 100755
--- a/spec/ts_all.rb
+++ b/spec/ts_all.rb
@@ -9,4 +9,5 @@ require "stack_specs"
require "rover_specs"
require "north_specs"
require "south_specs"
+require "terrain_specs"
require "specifications/find_all_books_by_author_specs"