diff options
| -rwxr-xr-x[-rw-r--r--] | lib/rover.rb | 1 | ||||
| -rwxr-xr-x | lib/terrain.rb | 13 | ||||
| -rwxr-xr-x | spec/terrain_specs.rb | 75 | ||||
| -rwxr-xr-x | spec/ts_all.rb | 1 |
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" |
