Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Re-organize player methods, rename switch_direction! to reverse_direc…

…tion!
  • Loading branch information...
commit c2e0569ddc2593b38678dd443c0df8941be6171a 1 parent b70bcd0
@gabebw authored
Showing with 35 additions and 24 deletions.
  1. +35 −24 player.rb
View
59 player.rb
@@ -58,7 +58,7 @@ def perform_action!(warrior)
move_toward_safe_space!
elsif space.wall?
# Hit a wall, switch direction and retry
- switch_direction!
+ reverse_direction!
perform_action!(warrior)
else
puts "Weird space: #{space.inspect}"
@@ -66,19 +66,10 @@ def perform_action!(warrior)
end
end
- # Is this space safe to rest in?
- # Note that it doesn't take into account whether the warrior is taking
- # damage.
- def in_safe_space?
- # No enemy in any direction
- (not DIRECTIONS.any? { |dir| @warrior.feel(dir).enemy? }) and
- not taking_damage
- end
+ # STATE CHANGERS
- # Should the warrior rest?
- def should_rest?
- # No need to rest when we're about to clear the level
- low_on_health? and not @warrior.feel(direction).stairs?
+ def reverse_direction!
+ @direction = (direction == :forward ? :backward : :forward)
end
# Moves warrior toward a safe space
@@ -86,17 +77,31 @@ def move_toward_safe_space!
puts "Moving toward safe space"
#if taking_damage_from_afar? and should_rest?
# Move away from threat
- switch_direction!
- @warrior.walk!
+ reverse_direction!
+ @warrior.walk!(direction)
#end
end
- def current_health
- @warrior.health
+ ## TESTERS
+
+ # Is this space safe to rest in?
+ # Note that it doesn't take into account whether the warrior is taking
+ # damage.
+ def in_safe_space?
+ not taking_damage? and not next_to_enemy?
end
- def current_turn
- @current_turn ||= 1
+ # Should the warrior rest? Note that this doesn't take into account
+ # whether it's safe for the warrior to rest, it just recommends that he
+ # should.
+ def should_rest?
+ # No need to rest when we're about to clear the level
+ low_on_health? and not @warrior.feel(direction).stairs?
+ end
+
+ # Is an enemy in an adjacent space?
+ def next_to_enemy?
+ DIRECTIONS.any? { |dir| @warrior.feel(dir).enemy? }
end
def taking_damage?
@@ -104,7 +109,7 @@ def taking_damage?
end
def taking_damage_from_afar?
- taking_damage? and not DIRECTIONS.any?{|d| @warrior.feel(d).enemy? }
+ taking_damage? and not next_to_enemy?
end
def low_on_health?
@@ -112,12 +117,18 @@ def low_on_health?
percent_health < MINIMUM_PERCENT_HEALTH
end
+ # ACCESSORS
+
+ def current_health
+ @warrior.health
+ end
+
+ def current_turn
+ @current_turn ||= 1
+ end
+
# Get direction warrior is walking in. Defaults to :backward.
def direction
@direction ||= :backward
end
-
- def switch_direction!
- @direction = (direction == :forward ? :backward : :forward)
- end
end
Please sign in to comment.
Something went wrong with that request. Please try again.