Upload
sveatoslav-circel
View
950
Download
2
Embed Size (px)
Citation preview
Ruby Warrior Presentation: Linear & OOP Solutions
A triumphant quest of adventure, love & destiny all
within a few lines of codehttps://www.bloc.io/ruby-warrior#/
by Sveatoslav Circel aka MEDBEDb
Level 1You see before yourself a long hallway with stairs
at the end. There is nothing in the way. Call
warrior.walk! to walk forward in the Player
'play_turn' method.
Level 2It is too dark to see anything, but you smell
sludge nearby. Use warrior.feel.empty? to see if
there is anything in front of you, and
warrior.attack! to fight it. Remember, you can
only do one action (ending in !) per turn.
class Player
def play_turn(warrior)
if warrior.feel.empty?
warrior.walk!
elsif warrior.feel.enemy?
warrior.attack!
end
end
end
class Player
def initialize
@max_health = 20
end
def attacks(warrior)
if warrior.feel.enemy?
warrior.attack!
else
warrior.walk!
end
end
def play_turn(warrior)
attacks(warrior)
end
end
#LINEAR #OBJECT ORIENTED
Solution Solution
Level 3The air feels thicker than before. There must be
a horde of sludge. Be careful not to die! Use
warrior.health to keep an eye on your health, and
warrior.rest! to earn 10% of max health back.
class Player
def play_turn(warrior)
if warrior.feel.empty? and warrior.health == 20
warrior.walk!
elsif warrior.feel.empty? and warrior.health < 20
warrior.rest!
elsif warrior.feel.enemy?
warrior.attack!
end
end
end
#LINEAR #OBJECT ORIENTED
Solution Solutionclass Player
def initialize
@max_health = 20
@mid_health = @max_health/2
end
def attacks(warrior)
if warrior.feel.enemy?
warrior.attack!
else
warrior.walk!
end
end
def play_turn(warrior)
if warrior.feel.empty? and warrior.health < @mid_health
warrior.rest!
else
attacks(warrior)
end
end
end
Level 4
You can hear bow strings being stretched. No new abilities
this time, but you must be careful not to rest while taking
damage. Save a @health instance variable and compare it
on each turn to see if you're taking damage.
class Player
def play_turn(warrior)
@max_health = 20
@current_health = warrior.health
if warrior.feel.empty? and warrior.health == @max_health
warrior.walk!
elsif warrior.feel.empty? and @current_health >= @health
warrior.rest!
elsif warrior.feel.enemy?
warrior.attack!
else
warrior.walk!
end
@health = warrior.health
end
end
class Player
def initialize
@max_health = 20
@mid_health = @max_health/2
@last_health = @max_health
end
def attacks(warrior)
if warrior.feel.enemy?
warrior.attack!
else
warrior.walk!
end
end
def diminishing_health(warrior)
warrior.health < @last_health
end
def play_turn(warrior)
if diminishing_health(warrior)
attacks(warrior)
elsif !diminishing_health(warrior) and warrior.health < @max_health
warrior.rest!
else
attacks(warrior)
end
@last_health = warrior.health
end
end
#LINEAR #OBJECT ORIENTED
Solution Solution
Level 5
You hear cries for help. Captives must need
rescuing. Use warrior.feel.captive? to see if there
is a captive and warrior.rescue! to rescue him.
Don't attack captives.
class Player
def play_turn(warrior)
@max_health = 20
@current_health = warrior.health
if warrior.feel.empty? and warrior.health == @max_health
warrior.walk!
elsif warrior.feel.empty? and @current_health >= @health
warrior.rest!
elsif warrior.feel.enemy?
warrior.attack!
elsif warrior.feel.captive?
warrior.rescue!
else
warrior.walk!
end
@health = warrior.health
end
end
class Player
def initialize
@max_health = 20
@mid_health = @max_health/2
@last_health = @max_health
end
def movement(warrior)
if warrior.feel.enemy?
attacks(warrior)
elsif warrior.feel.captive?
warrior.rescue!
else
warrior.walk!
end
end
def attacks(warrior)
if warrior.feel.enemy?
warrior.attack!
else
warrior.walk!
end
end
#LINEAR #OBJECT ORIENTED
Solution Solution
def diminishing_health(warrior)
warrior.health < @last_health
end
def play_turn(warrior)
if diminishing_health(warrior)
movement(warrior)
elsif !diminishing_health(warrior)
and warrior.health < @max_health
warrior.rest!
else
movement(warrior)
end
@last_health = warrior.health
end
end
Level 6
The wall behind you feels a bit further away in
this room. And you hear more cries for help. You
can walk backward by passing ':backward' as an
argument to walk!. Same goes for feel, rescue!
and attack!. Archers have a limited attack
distance.
class Player
def play_turn(warrior)
@maxlas_health = 20
@danger_health = 9
@current_health = warrior.health
if warrior.feel.empty? and warrior.health == @max_health
warrior.walk!
elsif warrior.feel.empty? and @current_health >= @health
warrior.rest!
elsif warrior.health < @danger_health and @current_health < @health
warrior.walk!(:backward)
elsif warrior.feel.enemy?
warrior.attack!
elsif warrior.feel.captive?
warrior.rescue!
else
warrior.walk!
end
@health = warrior.health
end
end
class Player
def initialize
@max_health = 20
@mid_health = @max_health/2
@last_health = @max_health
end
def movement(warrior)
if warrior.feel.enemy?
attacks(warrior)
elsif warrior.feel.captive?
warrior.rescue!
else
warrior.walk!
end
end
def attacks(warrior)
if warrior.feel.enemy?
warrior.attack!
else
warrior.walk!
end
end
def diminishing_health(warrior)
warrior.health < @last_health
end
def play_turn(warrior)
if diminishing_health(warrior)
and warrior.health <= @mid_health
warrior.walk!(:backward)
elsif diminishing_health(warrior)
movement(warrior)
elsif !diminishing_health(warrior)
and warrior.health < @max_health
warrior.rest!
else
movement(warrior)
end
@last_health = warrior.health
end
end
#LINEAR #OBJECT ORIENTED
Solution Solution
Level 7
You feel a wall right in front of you and an
opening behind you. You are not as effective at
attacking backward. Use warrior.feel.wall? and
warrior.pivot! to turn around.
class Player
def play_turn(warrior)
@max_health = 20
@current_health = warrior.health
@danger_health = 9
if warrior.feel.wall?
warrior.pivot!
elsif warrior.feel.empty? and warrior.health == @max_health
warrior.walk!
elsif warrior.feel.empty? and @current_health >= @health
warrior.rest!
elsif warrior.health < @danger_health and @current_health < @health
warrior.walk!(:backward)
elsif warrior.feel.enemy?
warrior.attack!
elsif warrior.feel.captive?
warrior.rescue!
else
warrior.walk!
end
@health = warrior.health
end
end
class Player
def initialize
@max_health = 20
@mid_health = @max_health/2
@last_health = @max_health
end
def movement(warrior)
if warrior.feel.enemy?
attacks(warrior)
elsif warrior.feel.wall?
warrior.pivot!
elsif warrior.feel.captive?
warrior.rescue!
else
warrior.walk!
end
end
def attacks(warrior)
if warrior.feel.enemy?
warrior.attack!
else
warrior.walk!
end
end
def diminishing_health(warrior)
warrior.health < @last_health
end
def play_turn(warrior)
if diminishing_health(warrior)
and warrior.health <= @mid_health
warrior.walk!(:backward)
elsif diminishing_health(warrior)
movement(warrior)
elsif !diminishing_health(warrior)
and warrior.health < @max_health
warrior.rest!
else
movement(warrior)
end
@last_health = warrior.health
end
end
#LINEAR #OBJECT ORIENTED
Solution Solution
Level 8
You hear the mumbling of wizards. Beware of
their deadly wands! Good thing you found a bow.
Use warrior.look to determine your surroundings,
and warrior.shoot! to fire an arrow.
Linear Solution:class Player
def play_turn(warrior)
@max_health = 20
@current_health = warrior.health
@danger_health = 9
if warrior.feel.wall?
warrior.pivot!
elsif warrior.feel.captive?
warrior.rescue!
elsif warrior.look.find_all {|space| space.captive? }.size.to_i == 1
warrior.walk!
elsif warrior.look.find_all {|space| space.enemy? }.size.to_i == 2
warrior.shoot!
elsif warrior.look.find_all {|space| space.enemy? }.size.to_i == 3
warrior.shoot!
elsif warrior.look.find_all {|space| space.enemy? }.size.to_i == 0
warrior.walk!
elsif warrior.health < @danger_health and @current_health < @health
warrior.walk!(:backward)
else
warrior.shoot!
end
@health = warrior.health
end
end
OOP Solution:class Player
def initialize
@max_health = 20
@mid_health = @max_health/2
@last_health = @max_health
end
def get_enemies(warrior)
find_enemies(warrior) or find_enemies(warrior, :backward)
end
def find_enemies(warrior, where = :forward)
enemy_spaces = warrior.look(where).find_all {|space| space.enemy? }
return enemy_spaces.any?
end
def get_captives(warrior)
find_captives(warrior) or find_captives(warrior, :backward)
end
def find_captives(warrior, where = :forward)
captive_spaces = warrior.look(where).find_all {|space| space.captive? }
return captive_spaces.any?
end
def rescues(warrior)
if warrior.feel.captive?
warrior.rescue!
else
warrior.walk!
end
end
def movement(warrior)
if get_captives(warrior)
rescues(warrior)
elsif get_enemies(warrior)
attacks(warrior)
elsif warrior.feel.wall?
warrior.pivot!
else
warrior.walk!
end
end
def attacks(warrior)
if warrior.feel.enemy?
warrior.attack!
elsif find_enemies(warrior)
warrior.shoot!
end
end
def diminishing_health(warrior)
warrior.health < @last_health
end
def play_turn(warrior)
if diminishing_health(warrior)
and warrior.health <= @mid_health
warrior.walk!(:backward)
elsif diminishing_health(warrior)
movement(warrior)
elsif !diminishing_health(warrior)
and warrior.health < @max_health
warrior.rest!
elsif warrior.feel.captive?
warrior.rescue!
else
movement(warrior)
end
@last_health = warrior.health
end
end
Level 9
Time to hone your skills and apply all of the
abilities that you have learned. Watch your
back.
Linear Solution:class Player
def play_turn(warrior)
@max_health = 20
@current_health = warrior.health
@mid_health = 9
@danger_health = 3
if warrior.feel.wall?
warrior.pivot!
elsif warrior.feel.captive?
warrior.rescue!
elsif warrior.look(:backward).find_all {|space| space.enemy? }.size.to_i == 1
warrior.shoot!(:backward)
elsif warrior.look.find_all {|space| space.enemy? }.size.to_i == 2
warrior.shoot!
elsif warrior.health == @max_health
warrior.walk!
elsif warrior.look.find_all {|space| space.enemy? }.size.to_i == 0 and @current_health >= @health
warrior.rest!
elsif warrior.health < @mid_health and @current_health < @health
warrior.walk!(:backward)
else
warrior.shoot!
end
@health = warrior.health
end
end
OOP Solution:class Player
def initialize
@max_health = 20
@mid_health = @max_health/2
@danger_health = @max_health * 3 / 4
@last_health = @max_health
end
def get_enemies(warrior)
find_enemies(warrior) or
find_enemies(warrior, :backward)
end
def find_enemies(warrior, where = :forward)
enemy_spaces =
warrior.look(where).find_all {|space|
space.enemy? }
return enemy_spaces.any?
end
def get_captives(warrior)
find_captives(warrior) or
find_captives(warrior, :backward)
end
def find_captives(warrior, where = :forward)
captive_spaces =
warrior.look(where).find_all {|space|
space.captive? }
return captive_spaces.any?
end
def rescues(warrior)
if warrior.feel.captive?
warrior.rescue!
else
warrior.walk!
end
end
def movement(warrior)
if get_captives(warrior)
rescues(warrior)
elsif get_enemies(warrior)
attacks(warrior)
elsif warrior.feel.wall?
warrior.pivot!
else
warrior.walk!
end
end
def attacks(warrior)
if warrior.feel(:backward).enemy?
warrior.pivot!
elsif warrior.feel.enemy?
warrior.attack!
elsif
find_enemies(warrior, :backward)
warrior.shoot!(:backward)
elsif find_enemies(warrior)
warrior.shoot!
end
end
def diminishing_health(warrior)
warrior.health < @last_health
end
def play_turn(warrior)
if diminishing_health(warrior) and warrior.health <= @mid_health
warrior.walk!(:backward)
elsif diminishing_health(warrior)
movement(warrior)
elsif !diminishing_health(warrior) and warrior.health < @max_health
warrior.rest!
elsif warrior.feel.captive?
warrior.rescue!
else
movement(warrior)
end
@last_health = warrior.health
end
end