Skip to content

Commit

Permalink
simplifying senses behavior so it acts on a sense directly instead of…
Browse files Browse the repository at this point in the history
… memorizing each possible argument
  • Loading branch information
ryanb committed Sep 13, 2008
1 parent 2558803 commit e7ae699
Show file tree
Hide file tree
Showing 11 changed files with 2 additions and 59 deletions.
4 changes: 0 additions & 4 deletions lib/ruby_warrior/abilities/direction_of_stairs.rb
Expand Up @@ -5,10 +5,6 @@ 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
Expand Down
4 changes: 0 additions & 4 deletions lib/ruby_warrior/abilities/distance.rb
Expand Up @@ -5,10 +5,6 @@ def description
"Returns the number of spaces the stairs are away."
end

def possible_arguments
[[]]
end

def perform
@unit.position.distance_from_stairs
end
Expand Down
4 changes: 0 additions & 4 deletions lib/ruby_warrior/abilities/feel.rb
Expand Up @@ -5,10 +5,6 @@ def description
"Returns a Space for the given direction (forward by default)."
end

def possible_arguments
[[], *DIRECTIONS.keys]
end

def perform(direction = :forward)
space(direction)
end
Expand Down
4 changes: 0 additions & 4 deletions lib/ruby_warrior/abilities/health.rb
Expand Up @@ -5,10 +5,6 @@ def description
"Returns an integer representing your health."
end

def possible_arguments
[[]]
end

def perform
@unit.health
end
Expand Down
4 changes: 0 additions & 4 deletions lib/ruby_warrior/abilities/listen.rb
Expand Up @@ -5,10 +5,6 @@ def description
"Returns an array of all spaces which have units in them."
end

def possible_arguments
[[]]
end

def perform
@unit.position.floor.units.map do |unit|
unit.position.space unless unit == @unit
Expand Down
4 changes: 0 additions & 4 deletions lib/ruby_warrior/abilities/look.rb
Expand Up @@ -5,10 +5,6 @@ def description
"Returns an array of Spaces in given direction (forward by default)."
end

def possible_arguments
[[], :forward, :left, :right]
end

def perform(direction = :forward)
[1, 2, 3].map do |amount|
space(direction, amount)
Expand Down
7 changes: 2 additions & 5 deletions lib/ruby_warrior/turn.rb
Expand Up @@ -29,13 +29,10 @@ def #{action}(*args)
def add_sense(name, sense)
instance_eval <<-EOS
def #{name}(*args)
@senses.fetch([:#{name}, args])
@senses[:#{name}].perform(*args)
end
EOS
sense.possible_arguments.each do |args|
args = [args].flatten # in case args aren't in array
@senses[[name, args]] = sense.perform(*args)
end
@senses[name] = sense
end
end
end
6 changes: 0 additions & 6 deletions spec/ruby_warrior/abilities/feel_spec.rb
Expand Up @@ -10,10 +10,4 @@
@unit.position.expects(:relative_space).with(1, 0)
@feel.perform(:forward)
end

it "should have all directions as possible arguments" do
[[], :forward, :backward, :left, :right].each do |arg|
@feel.possible_arguments.should include(arg)
end
end
end
4 changes: 0 additions & 4 deletions spec/ruby_warrior/abilities/health_spec.rb
Expand Up @@ -10,8 +10,4 @@
@warrior.health = 10
@health.perform.should == 10
end

it "should have no possible arguments" do
@health.possible_arguments.should == [[]]
end
end
4 changes: 0 additions & 4 deletions spec/ruby_warrior/abilities/look_spec.rb
Expand Up @@ -12,8 +12,4 @@
@unit.position.expects(:relative_space).with(3, 0).returns(3)
@feel.perform(:forward).should == [1, 2, 3]
end

it "should have all directions except back as possible arguments" do
@feel.possible_arguments.to_set.should == [[], :forward, :left, :right].to_set
end
end
16 changes: 0 additions & 16 deletions spec/ruby_warrior/turn_spec.rb
Expand Up @@ -38,21 +38,5 @@
@turn.feel.should == @feel.perform
@turn.feel(:backward).should == @feel.perform(:backward)
end

it "should have memorized result" do
result = @feel.perform(:backward)
@feel.stubs(:space).with(:backward).returns(nil)
@turn.feel(:backward).should == result
end

it "should have memorized result" do
result = @feel.perform(:backward)
@feel.stubs(:space).with(:backward).returns(nil)
@turn.feel(:backward).should == result
end

it "should raise an exception for unexpected argument" do
lambda { @turn.feel(:bad) }.should raise_error
end
end
end

0 comments on commit e7ae699

Please sign in to comment.