From 02e8a7c716002db61ee09a2d91508bb1d6d2f719 Mon Sep 17 00:00:00 2001 From: mo k Date: Mon, 13 Feb 2012 16:46:27 -0700 Subject: considering a different direction where the terrain actually moves the rover after doing a bounds check. --- lib/rover.rb | 1 + lib/terrain.rb | 13 +++++++++ spec/terrain_specs.rb | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ spec/ts_all.rb | 1 + 4 files changed, 90 insertions(+) mode change 100644 => 100755 lib/rover.rb create mode 100755 lib/terrain.rb create mode 100755 spec/terrain_specs.rb diff --git a/lib/rover.rb b/lib/rover.rb old mode 100644 new mode 100755 index 42fdfda..fa5d976 --- 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" -- cgit v1.2.3