diff options
| author | mo khan <mo@mokhan.ca> | 2013-04-24 16:01:48 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2013-04-24 16:01:48 -0600 |
| commit | 5a825d1f28630ea21e62af66ea01a7ab69eecf35 (patch) | |
| tree | bad9c55d835490c2ab58efe44ee29d04d28d630a | |
| parent | f6cb197f4f4d3bccb9f38270b5bcd7ca5a301070 (diff) | |
extract good health
| -rw-r--r-- | rubywarrior/alimo-beginner/player.rb | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/rubywarrior/alimo-beginner/player.rb b/rubywarrior/alimo-beginner/player.rb index 7aa1d5c..34262a9 100644 --- a/rubywarrior/alimo-beginner/player.rb +++ b/rubywarrior/alimo-beginner/player.rb @@ -1,27 +1,23 @@ class Player def initialize - @actions = [Rest.new, Attack.new, Walk.new] + @behaviours = [Rest.new, Attack.new, Walk.new] end def play_turn(warrior) @initial_health ||= warrior.health - @actions.each do |action| - action.play(warrior) if action.can(warrior) + @behaviours.each do |action| + action.play(warrior) if action.matches(warrior) end end end -class Attack +class GoodHealth FIFTY_PERCENT = 0.5 - def can(warrior) + def matches(warrior) @initial_health ||= warrior.health - warrior.feel.empty? == false && health_is_good(warrior) - end - - def play(warrior) - warrior.attack! + health_is_good(warrior) end private @@ -32,10 +28,24 @@ class Attack end end +class Attack + def initialize(good_health = GoodHealth.new) + @good_health = good_health + end + + def matches(warrior) + warrior.feel.empty? == false && @good_health.matches(warrior) + end + + def play(warrior) + warrior.attack! + end +end + class Rest FIFTY_PERCENT = 0.5 - def can(warrior) + def matches(warrior) @initial_health ||= warrior.health warrior.feel.empty? == false && health_is_low(warrior) end @@ -53,7 +63,7 @@ class Rest end class Walk - def can(warrior) + def matches(warrior) warrior.feel.empty? end |
