Last active
August 29, 2015 14:24
-
-
Save mgiuffrida/e2b72ad323e7946998b3 to your computer and use it in GitHub Desktop.
ruby-warrior beginner level 9
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
# https://www.bloc.io/ruby-warrior | |
class Player | |
def deadlyArcher?(w, spaces) | |
w.health < @health and (spaces[0].enemy? or spaces[1].enemy? or spaces[2].enemy?) | |
end | |
def deadlyWizard?(w, spaces) | |
w.health <= 12 and w.health > @health and (spaces[0].enemy? or spaces[1].enemy?) | |
end | |
def play_turn(w) | |
if not @dir | |
@dir = :forward | |
end | |
if @health == nil | |
@health = w.health | |
end | |
spaces = w.look(@dir) | |
spacesBack = w.look(:backward) | |
if w.feel(@dir).wall? | |
w.pivot! | |
# Don't die by archer. | |
elsif deadlyArcher?(w, spacesBack) and spaces[0].empty? | |
w.walk!(@dir) | |
elsif deadlyArcher?(w, spaces) and spacesBack[0].empty? | |
w.walk!(:backward) | |
# Don't die by wizard. | |
elsif deadlyWizard?(w, spaces) | |
w.walk!(:backward) | |
elsif deadlyWizard?(w, spacesBack) | |
w.walk!(@dir) | |
elsif w.health < 20 and w.health >= @health and not spaces[0].enemy? and not spaces[1].enemy? and not spaces[2].enemy? | |
w.rest! | |
else | |
# Advance. | |
if not spaces[0].empty? | |
if w.feel(@dir).captive? | |
w.rescue!(@dir) | |
else | |
w.attack!(@dir) | |
end | |
elsif spaces[1].enemy? or (spaces[1].empty? and spaces[2].enemy?) | |
w.shoot!(@dir) | |
else | |
w.walk!(@dir) | |
end | |
end | |
@health = w.health | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment