Skip to content

Commit

Permalink
adding direction_of_stairs ability
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Sep 13, 2008
1 parent 4aa0296 commit 387af5b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/ruby_warrior.rb
Expand Up @@ -32,3 +32,4 @@
require 'ruby_warrior/abilities/distance'
require 'ruby_warrior/abilities/bind'
require 'ruby_warrior/abilities/listen'
require 'ruby_warrior/abilities/direction_of_stairs'
17 changes: 17 additions & 0 deletions lib/ruby_warrior/abilities/direction_of_stairs.rb
@@ -0,0 +1,17 @@
module RubyWarrior
module Abilities
class DirectionOfStairs < Base
def description
"Returns the direction (:left, :right, :forward, :backward) the stairs are from your location."
end

def possible_arguments
[[]]
end

def perform
@unit.position.relative_direction_of_stairs
end
end
end
end
27 changes: 27 additions & 0 deletions lib/ruby_warrior/position.rb
Expand Up @@ -2,6 +2,7 @@ module RubyWarrior
class Position
attr_reader :floor
DIRECTIONS = [:north, :east, :south, :west]
RELATIVE_DIRECTIONS = [:forward, :right, :backward, :left]

def initialize(floor, x, y, direction = nil)
@floor = floor
Expand Down Expand Up @@ -41,6 +42,32 @@ def distance_from_stairs
(@x - stairs_x).abs + (@y - stairs_y).abs
end

def relative_direction_of_stairs
relative_direction(direction_of_stairs)
end

def direction_of_stairs
stairs_x, stairs_y = *@floor.stairs_location
if (@x - stairs_x).abs > (@y - stairs_y).abs
if stairs_x > @x
:west
else
:east
end
elsif stairs_y > @y
:south
else
:north
end
end

def relative_direction(direction)
offset = DIRECTIONS.index(direction) - @direction_index
offset -= 4 while offset > 3
offset += 4 while offset < 0
RELATIVE_DIRECTIONS[offset]
end

private

def translate_offset(forward, right)
Expand Down
13 changes: 13 additions & 0 deletions spec/ruby_warrior/abilities/direction_of_stairs_spec.rb
@@ -0,0 +1,13 @@
require File.dirname(__FILE__) + '/../../spec_helper'

describe RubyWarrior::Abilities::DirectionOfStairs do
before(:each) do
@position = stub
@distance = RubyWarrior::Abilities::DirectionOfStairs.new(stub(:position => @position, :say => nil))
end

it "should return relative direction of stairs" do
@position.stubs(:relative_direction_of_stairs).returns(:left)
@distance.perform.should == :left
end
end
18 changes: 18 additions & 0 deletions spec/ruby_warrior/position_spec.rb
Expand Up @@ -72,4 +72,22 @@
@floor.place_stairs(0, 3)
@position.distance_from_stairs.should == 2
end

it "should return direction and relative direction of stairs" do
@floor.place_stairs(0, 0)
@position.direction_of_stairs.should == :north
@position.relative_direction_of_stairs.should == :forward
end

it "should be able to determine relative direction" do
@position.relative_direction(:north).should == :forward
@position.rotate 1
@position.relative_direction(:north).should == :left
@position.rotate 1
@position.relative_direction(:north).should == :backward
@position.rotate 1
@position.relative_direction(:north).should == :right
@position.rotate 1
@position.relative_direction(:south).should == :backward
end
end

0 comments on commit 387af5b

Please sign in to comment.