Skip to content

Commit c44b6ba

Browse files
committed
adding distance_of ability - closes ryanb#21
1 parent e1c4876 commit c44b6ba

File tree

7 files changed

+38
-29
lines changed

7 files changed

+38
-29
lines changed

lib/ruby_warrior.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
require 'ruby_warrior/abilities/shoot'
3737
require 'ruby_warrior/abilities/rescue'
3838
require 'ruby_warrior/abilities/pivot'
39-
require 'ruby_warrior/abilities/distance'
39+
require 'ruby_warrior/abilities/distance_of'
4040
require 'ruby_warrior/abilities/bind'
4141
require 'ruby_warrior/abilities/listen'
4242
require 'ruby_warrior/abilities/direction_of_stairs'

lib/ruby_warrior/abilities/distance.rb

-13
This file was deleted.
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module RubyWarrior
2+
module Abilities
3+
class DistanceOf < Base
4+
def description
5+
"Pass a Space as an argument, and it will return an integer representing the distance to that space."
6+
end
7+
8+
def perform(space)
9+
@unit.position.distance_of(space)
10+
end
11+
end
12+
end
13+
end

lib/ruby_warrior/position.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ def move(forward, right = 0)
3838
end
3939

4040
def distance_from_stairs
41-
stairs_x, stairs_y = *@floor.stairs_location
42-
(@x - stairs_x).abs + (@y - stairs_y).abs
41+
distance_of(@floor.stairs_space)
42+
end
43+
44+
def distance_of(space)
45+
x, y = *space.location
46+
(@x - x).abs + (@y - y).abs
4347
end
4448

4549
def relative_direction_of_stairs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require File.dirname(__FILE__) + '/../../spec_helper'
2+
3+
describe RubyWarrior::Abilities::DistanceOf do
4+
before(:each) do
5+
@position = stub
6+
@distance = RubyWarrior::Abilities::DistanceOf.new(stub(:position => @position, :say => nil))
7+
end
8+
9+
it "should return distance from stairs" do
10+
@position.stubs(:distance_of).with(:space).returns(5)
11+
@distance.perform(:space).should == 5
12+
end
13+
end

spec/ruby_warrior/abilities/distance_spec.rb

-13
This file was deleted.

spec/ruby_warrior/position_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,9 @@
100100
it "should return a space at the same location as position" do
101101
@position.space.location.should == [1, 2]
102102
end
103+
104+
it "should return distance of given space" do
105+
@position.distance_of(@floor.space(5, 3)).should == 5
106+
@position.distance_of(@floor.space(4, 2)).should == 3
107+
end
103108
end

0 commit comments

Comments
 (0)