Commit e58ab91
Changed files (4)
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.clone)
+ 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
spec/spec_helper.rb
@@ -1,2 +1,4 @@
require "rspec"
require "rspec-fakes"
+
+require_relative '../lib/terrain'
spec/terrain_spec.rb
@@ -0,0 +1,72 @@
+require 'spec_helper'
+
+describe Terrain do
+ let(:sut) {Terrain.new({:x => 3, :y => 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
+ @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
+ @location[:y].should == 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
+
+ context "when the next position is to far north" do
+ it "should not let you move forward" do
+ @location[:x].should == 0
+ @location[:y].should == 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
+
+ context "when the next position is to far south" do
+ it "should not let you move forward" do
+ @location[:x].should == 0
+ @location[:y].should == 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
+
+ context "when the next position is just fine" do
+ it "should move position forward" do
+ @location[:x].should == 1
+ @location[:y].should == 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
Rakefile
@@ -1,5 +1,4 @@
task :default => :spec do
-
end
task :spec do