Quantcast
Viewing latest article 4
Browse Latest Browse All 7

Learn Ruby programming language by playing a fun game : Ruby Warrior

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

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

 


Viewing latest article 4
Browse Latest Browse All 7

Trending Articles