Last active
October 24, 2015 14:34
-
-
Save hakunin/f42ad9a1317c328e9829 to your computer and use it in GitHub Desktop.
Ruby warrior
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
class Player | |
def play_turn(warrior) | |
@state ||= :init | |
@health ||= 0 | |
@init ||= true | |
@got_hit = (warrior.health < @health) | |
if @state == :init | |
warrior.pivot!() | |
@state = :kill | |
elsif @state == :rescue | |
rescue_captive_on_left(warrior) | |
elsif @state == :heal | |
heal(warrior) | |
elsif @state == :kill | |
kill(warrior) | |
end | |
@health = warrior.health | |
end | |
def rescue_captive_on_left(warrior) | |
unless warrior.feel(:backward).captive? | |
warrior.walk!(:backward) | |
else | |
warrior.rescue!(:backward) | |
@state = :kill | |
end | |
end | |
def heal(warrior) | |
if @got_hit | |
warrior.walk!(:backward) | |
else | |
if (warrior.health < 20) | |
warrior.rest! | |
else | |
@state = :kill | |
end | |
end | |
end | |
def kill(warrior) | |
if warrior.feel.empty? | |
if @was_attacking | |
@state = :heal | |
@was_attacking = false | |
heal(warrior) | |
else | |
warrior.walk! | |
end | |
else | |
warrior.attack! | |
@was_attacking = true | |
end | |
end | |
def opposite(direction) | |
if direction == :forward | |
:backward | |
else | |
:forward | |
end | |
end | |
end | |
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
class Player | |
def play_turn(warrior) | |
@state ||= :init | |
@health ||= 0 | |
@init ||= true | |
@warrior ||= warrior | |
@got_hit = (warrior.health < @health) | |
if @state == :init | |
#warrior.pivot!() | |
@state = :rescue | |
elsif @state == :shoot | |
shoot() | |
elsif @state == :rescue | |
rescue_captive(warrior) | |
elsif @state == :heal | |
heal(warrior) | |
elsif @state == :kill | |
kill(warrior) | |
end | |
@health = warrior.health | |
end | |
def rescue_captive(warrior) | |
unless warrior.feel.captive? | |
warrior.walk! | |
else | |
warrior.rescue! | |
@state = :shoot | |
end | |
end | |
def heal(warrior) | |
if @got_hit | |
warrior.walk!(:backward) | |
else | |
if (warrior.health < 20) | |
warrior.rest! | |
else | |
@state = :kill | |
end | |
end | |
end | |
def kill(warrior) | |
if warrior.feel.empty? | |
if @was_attacking | |
@state = :heal | |
@was_attacking = false | |
heal(warrior) | |
else | |
warrior.walk! | |
end | |
else | |
warrior.attack! | |
@was_attacking = true | |
end | |
end | |
def opposite(direction) | |
if direction == :forward | |
:backward | |
else | |
:forward | |
end | |
end | |
def shoot | |
if @warrior.look.any? { |f| f.enemy? } | |
@warrior.shoot! | |
else | |
@state = :kill | |
play_turn(@warrior) | |
end | |
end | |
end | |
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
class Player | |
def play_turn(warrior) | |
@state ||= :rescue | |
@health ||= 0 | |
@got_hit = (warrior.health < @health) | |
if @state == :rescue | |
rescue_captive_on_left(warrior) | |
elsif @state == :heal | |
heal(warrior) | |
elsif @state == :kill | |
make_war(warrior) | |
end | |
@health = warrior.health | |
end | |
def rescue_captive_on_left(warrior) | |
unless warrior.feel(:backward).captive? | |
warrior.walk!(:backward) | |
else | |
warrior.rescue!(:backward) | |
@state = :kill | |
end | |
end | |
def heal(warrior) | |
if @got_hit | |
warrior.walk!(:backward) | |
else | |
if (warrior.health < 20) | |
warrior.rest! | |
else | |
@state = :kill | |
end | |
end | |
end | |
def make_war(warrior) | |
if warrior.feel.empty? | |
if @was_attacking | |
@state = :heal | |
@was_attacking = false | |
heal(warrior) | |
else | |
warrior.walk! | |
end | |
else | |
warrior.attack! | |
@was_attacking = true | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment