From ec2cb99c0e3eed4bd704cbbfc7f3af1368f4ac3a Mon Sep 17 00:00:00 2001 From: mo k Date: Mon, 13 Feb 2012 16:21:57 -0700 Subject: prevent falling off the map when on the edge and facing east. --- lib/east.rb | 6 ++++-- spec/east_specs.rb | 20 ++++++++++++++++++++ spec/rover_specs.rb | 1 + 3 files changed, 25 insertions(+), 2 deletions(-) mode change 100644 => 100755 lib/east.rb create mode 100755 spec/east_specs.rb diff --git a/lib/east.rb b/lib/east.rb old mode 100644 new mode 100755 index 1699ffc..fd5752d --- a/lib/east.rb +++ b/lib/east.rb @@ -5,7 +5,9 @@ class East def turn_left North.new end - def forward(current_location, terrain) - current_location[:x] = current_location[:x]+1 + def forward(location, terrain) + unless terrain.is_out_of_bounds( {:x => location[:x]+1, :y => location[:y]}) + location[:x] = location[:x]+1 + end end end diff --git a/spec/east_specs.rb b/spec/east_specs.rb new file mode 100755 index 0000000..5060956 --- /dev/null +++ b/spec/east_specs.rb @@ -0,0 +1,20 @@ +require 'east' + +describe East do + before(:each) do + @sut = East.new + end + describe "when at the edge of the terrain" do + describe "when moving forward" do + it "should just stay at the current position" do + @location[:x].must_equal 0 + end + before do + terrain = fake + terrain.stub(:is_out_of_bounds).with({:x => -1, :y => 0}).and_return(true) + @location = {:x => 0, :y => 0} + @sut.forward(@location, terrain) + end + end + end +end diff --git a/spec/rover_specs.rb b/spec/rover_specs.rb index f8d15f8..8c3da03 100755 --- a/spec/rover_specs.rb +++ b/spec/rover_specs.rb @@ -82,6 +82,7 @@ describe Rover do end describe "when driving forward" do before do + @terrain = fake @sut.move_forward(@terrain) end it "should increment the x coordinate on the terrain" do -- cgit v1.2.3