Commit 728fb21
Changed files (7)
spec
integration
unit
lib/east.rb
@@ -6,7 +6,7 @@ class East
North.new
end
def forward(location)
- location[:x] = location[:x]+1
+ location[:x] = location[:x].to_i + 1
end
def to_s
'E'
lib/south.rb
@@ -6,7 +6,7 @@ class South
East.new
end
def forward(location)
- location[:y] = location[:y]-1
+ location[:y] = location[:y].to_i - 1
end
def to_s
'S'
lib/terrain.rb
@@ -1,25 +1,34 @@
class Terrain
- def initialize(edge_of_map)
- @map = edge_of_map
+ def initialize(x,y)
+ @map = {:x => x, :y => y}
@headings = [North.new, East.new, West.new, South.new]
end
def move_forward( heading, location)
new_location = heading.forward(location.clone)
- if(is_on_terrain(new_location, :x) && is_on_terrain(new_location, :y))
+ puts "#{new_location[:x]}"
+ if(new_location[:x] > @map[:x])
+ location[:x] = new_location[:x] - @map[:x]
+ elsif (new_location[:x] < 0)
+ location[:x] = @map[:x]
+ else
location[:x] = new_location[:x]
+ end
+
+ if(new_location[:y] > @map[:y])
+ location[:y] = new_location[:y] - @map[:y]
+ elsif (new_location[:y] < 0)
+ location[:y] = @map[:y]
+ else
location[:y] = new_location[:y]
end
end
- def deploy_rover_to(heading, coordinates)
- Rover.new(heading_for(heading), coordinates)
+ def deploy_rover_to(heading, x, y)
+ Rover.new(heading_for(heading), {:x => x, :y => y})
end
private
- def is_on_terrain(new_location, symbol)
- new_location[symbol] < @map[symbol] && new_location[symbol] > 0
- end
def heading_for(pneumonic)
@headings.find { |heading| heading.to_s == pneumonic }
end
lib/west.rb
@@ -6,7 +6,7 @@ class West
South.new
end
def forward(current_location)
- current_location[:x] = current_location[:x]-1
+ current_location[:x] = current_location[:x].to_i - 1
end
def to_s
'W'
spec/unit/terrain_spec.rb
@@ -1,25 +1,26 @@
require 'spec_helper'
describe Terrain do
- let(:sut) {Terrain.new({:x => 3, :y => 3})}
+ let(:sut) {Terrain.new(3, 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
+ it "should move to the other side of the board" do
+ @location[:x].should == 1
@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
+ it "should move to the other side of the board" do
+ @location[:x].should == 3
@location[:y].should == 0
end
before do
@@ -31,9 +32,9 @@ describe Terrain do
end
context "when the next position is to far north" do
- it "should not let you move forward" do
+ it "should move to the other side of the board" do
@location[:x].should == 0
- @location[:y].should == 3
+ @location[:y].should == 1
end
before do
@heading = fake
@@ -44,9 +45,9 @@ describe Terrain do
end
context "when the next position is to far south" do
- it "should not let you move forward" do
+ it "should move to the other side of the board" do
@location[:x].should == 0
- @location[:y].should == 0
+ @location[:y].should == 3
end
before do
@heading = fake