I played a really cool game to learn Ruby syntax in a funny way! :D You should try it.
Just control and be a player by typing some Ruby code lines.
It was great to improve myself in loops, conditions, method declaration syntaxes.
Even if I liked the music, at lease the first two minutes ‘-_- , you can disable it top-left of your screen.
Be a Ruby Warrior now ! ;)
SPOIL
Here is my solution code for last level, don’t cheat ! Try yourself first :P
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
class Player def get_next_space_not_empty(warrior,direction) warrior.look(direction).each { |i| if !i.empty? && !i.wall? return i end } return warrior.look(direction).first end def play_turn(warrior) if @start.nil? warrior.pivot! # begin by turn around (for last level) @start = "GO" # rest if you're losing HP elsif warrior.health < 20 && @health <= warrior.health && !warrior.feel.stairs? && !warrior.feel(:backward).stairs? warrior.rest! # Move backwards if you're under attack and low HP elsif warrior.health < 10 && @health > warrior.health warrior.walk!:backward # Range attack elsif get_next_space_not_empty(warrior,:forward).enemy? warrior.shoot!:forward # Attack elsif warrior.feel.enemy? warrior.attack! # Free a captive elsif warrior.feel.captive? warrior.rescue! # You need to turn around elsif get_next_space_not_empty(warrior,:backward).enemy? \ || get_next_space_not_empty(warrior,:forward).wall? \ || warrior.feel(:backward).enemy? \ || warrior.feel(:backward).captive? \ || warrior.feel.wall? warrior.pivot! # Walk forward elsif warrior.feel.empty? warrior.walk! end # Instance variable @health = warrior.health end end |
I like ruby on rails developing and I want to learn about this latest developing technique and your post is very helpful in this case. I have learnt many new things in this post which are unique and informative for me.