Upcoming SlideShare
×

# Ruby warrior presentation - Linear & oop solutions by Sveatoslav

1,718 views
1,571 views

Published on

http://bloc.io/ruby-warrior#
https://github.com/MEDBEDb/rubywarrior

2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,718
On SlideShare
0
From Embeds
0
Number of Embeds
584
Actions
Shares
0
5
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Ruby warrior presentation - Linear & oop solutions by Sveatoslav

1. 1. Ruby Warrior Presentation: Linear & OOP Solutions by Sveatoslav Circel aka MEDBEDb A triumphant quest of adventure, love & destiny all within a few lines of code https://www.bloc.io/ruby-warrior#/
2. 2. Level 1 You 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.
3. 3. Solution: class Player def play_turn(warrior) warrior.walk! end end
4. 4. Level 2 It 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.
5. 5. #LINEAR Solution class Player def play_turn(warrior) if warrior.feel.empty? warrior.walk! elsif warrior.feel.enemy? warrior.attack! end end end #OBJECT ORIENTED Solution 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
6. 6. Level 3 The 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.
7. 7. #LINEAR Solution 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 #OBJECT ORIENTED Solution class 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
8. 8. 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.
9. 9. #LINEAR Solution 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 #OBJECT ORIENTED Solution 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
10. 10. 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.
11. 11. #LINEAR Solution 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 #OBJECT ORIENTED Solution 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) movement(warrior) elsif !diminishing_health(warrior) and warrior.health < @max_health warrior.rest! else movement(warrior) end @last_health = warrior.health end end
12. 12. 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.
13. 13. #LINEAR Solution #OBJECT ORIENTED Solution 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
14. 14. 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.
15. 15. #LINEAR Solution #OBJECT ORIENTED 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.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
16. 16. 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.
17. 17. 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
18. 18. class Player def initialize @max_health = 20 @mid_health = @max_health/2 @last_health = @max_health end OOP Solution: def get_enemies(warrior) find_enemies(warrior) or find_enemies(warrior, :backward) 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 find_enemies(warrior, where = :forward) enemy_spaces = warrior.look(where).find_all {|space| space.enemy? } def attacks(warrior) return enemy_spaces.any? if warrior.feel.enemy? end warrior.attack! elsif find_enemies(warrior) def get_captives(warrior) warrior.shoot! find_captives(warrior) or find_captives(warrior, :backward) end end end def find_captives(warrior, where = :forward) captive_spaces = warrior.look(where).find_all {|space| space.captive? } def diminishing_health(warrior) warrior.health < @last_health return captive_spaces.any? end end def rescues(warrior) if warrior.feel.captive? warrior.rescue! else warrior.walk! end 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
19. 19. Level 9 Time to hone your skills and apply all of the abilities that you have learned. Watch your back.
20. 20. 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
21. 21. 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 rescues(warrior) if warrior.feel.captive? warrior.rescue! else warrior.walk! end end def get_enemies(warrior) find_enemies(warrior) or find_enemies(warrior, :backward) 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 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 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