Skip to content

Instantly share code, notes, and snippets.

@garbados
Last active January 2, 2016 10:59
Show Gist options
  • Save garbados/8293550 to your computer and use it in GitHub Desktop.
Save garbados/8293550 to your computer and use it in GitHub Desktop.
Solution to every level of RubyWarrior (https://www.bloc.io/ruby-warrior/#/). Heavily borrowed from https://gist.github.com/jnf/6198927
# N.B. doesn't get maximum score:
# * won't get captives behind the warrior
# * will shoot captives at look[1] if an enemy is in look[2]
class Player
def play_turn(warrior)
@warrior = warrior
## uncomment `@max_health` and `@prev_health` on level 3
# @max_health ||= warrior.health # set to `1` to complete level 8 in 27 turns
# @prev_health ||= @max_health
## uncomment `@space` on level 2
# @space = @warrior.feel
feel_it_out
## uncomment this line on level 3
# @prev_health = warrior.health
end
protected
def feel_it_out
## uncomment `about_face` on level 7
# return about_face if @space.wall?
## uncomment `be_a_hero` on level 5
# return be_a_hero if @space.captive?
## uncomment `strike` on level 2
# return strike if @space.enemy?
## uncomment `ddos` on level 8
# return ddos if @warrior.look[2].enemy?
## uncomment `reconsider` on level 4
# return reconsider if @warrior.health < @prev_health
## uncomment `sleep_it_off` on level 3
# return sleep_it_off if @space.empty? and (@warrior.health < @max_health)
return move
end
def sleep_it_off
@warrior.rest!
end
def strike
@warrior.attack!
end
def ddos
@warrior.shoot!
end
def move(direction = :forward)
@warrior.walk! direction
end
def be_a_hero
@warrior.rescue!
end
def about_face
@warrior.pivot!
end
def reconsider
move(@warrior.health < (@max_health / 4) ? :backward : :forward)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment