Created
August 29, 2009 09:38
-
-
Save Domon/177436 to your computer and use it in GitHub Desktop.
my player.rb for ruby-warrior intermediate
This file contains 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
DEBUG = false | |
class Player | |
DIRS = [:forward, :right, :backward, :left] | |
def initialize | |
@max_health = nil | |
@warrior = nil | |
@direction = :forward | |
end | |
def play_turn(warrior) | |
@max_health ||= warrior.health | |
@warrior = warrior | |
@direction = warrior.direction_of_stairs | |
puts "Facing " + @direction.to_s if DEBUG | |
if captive_around? | |
warrior.rescue! @direction | |
elsif enemy_around? | |
if need_retreat? | |
turn until feel.empty? | |
walk! | |
else | |
attack! | |
end | |
else | |
if need_rest? | |
rest! | |
else | |
walk! | |
end | |
end | |
end | |
def captive_around? | |
DIRS.each do |dir| | |
if @warrior.feel(dir).captive? | |
@direction = dir | |
puts "Captive direction: " + @direction.to_s if DEBUG | |
return true | |
end | |
end | |
return false | |
end | |
def enemy_around? | |
DIRS.each do |dir| | |
if @warrior.feel(dir).enemy? | |
@direction = dir | |
puts "Enemy direction: " + @direction.to_s if DEBUG | |
return true | |
end | |
end | |
return false | |
end | |
def need_rest? | |
@warrior.health < @max_health | |
end | |
def need_retreat? | |
@warrior.health < 7 | |
end | |
def turn | |
DIRS.each_index do |i| | |
if @direction == DIRS[i] | |
@direction = DIRS[(i+1)%4] | |
puts "Turn " + @direction.to_s if DEBUG | |
break | |
end | |
end | |
end | |
def feel | |
puts "Feel: " + @direction.to_s if DEBUG | |
@warrior.feel @direction | |
end | |
def walk! | |
@warrior.walk! @direction | |
end | |
def attack! | |
@warrior.attack! @direction | |
end | |
def bind! | |
@warrior.bind! @direction | |
end | |
def rest! | |
@warrior.rest! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment