Created
March 12, 2014 03:23
-
-
Save borodean/9500232 to your computer and use it in GitHub Desktop.
RubyWarrior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Player | |
| def initialize | |
| @max_health = 20 | |
| @health = @max_health | |
| @direction = :backward | |
| @has_retreated = false | |
| @is_berserk = false | |
| end | |
| def play_turn(warrior) | |
| # Shorthands | |
| feel = warrior.feel(@direction) | |
| # Stats | |
| is_taking_damage = (warrior.health < @health) | |
| is_wounded = (warrior.health < @max_health) | |
| # Memoise | |
| @health = warrior.health | |
| # Rescue | |
| if feel.captive? | |
| return warrior.rescue!(@direction) | |
| end | |
| # Hit a wall | |
| if feel.wall? | |
| @direction = (@direction == :forward) ? :backward : :forward | |
| return play_turn(warrior) | |
| end | |
| # Attack | |
| if feel.enemy? | |
| return warrior.attack!(@direction) | |
| end | |
| # Exit | |
| if feel.stairs? | |
| return warrior.walk!(@direction) | |
| end | |
| # Healing | |
| if is_wounded and not @is_berserk | |
| if is_taking_damage | |
| @has_retreated = true | |
| return warrior.walk!(:backward) | |
| end | |
| return warrior.rest! | |
| else | |
| @is_berserk = @has_retreated | |
| end | |
| # Walk | |
| return warrior.walk!(@direction) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment