It's been a while

 I don't put the effort into this that I thought I would.  Regardless, I thought I would just say "out loud" that I think Elix...

January 29, 2014

Another diversion

I was working through the Koans and got distracted.  I'm still learning Ruby, I promise!

Somewhere, I stumbled across Ruby Warrior, and it looked fun.  Plus it looked like something I could do without need to concentrate quite so hard, which helps if you're stuck on the couch watching "kid shows", for instance.

This is a turn based game where you can only do one action on your turn.  It is a two dimensional board, where you, as the warrior, have only a small set of abilities:

  • walk
  • feel
  • attack
  • health
  • rest
  • rescue
Before you decide what to do, you can also "feel" a square.  This tells you what is there.  The result can be one of five states:
  • empty
  • stairs
  • enemy
  • captive
  • wall
Currently, I am stuck on level 6, but I'll still give you a taste for the code:


class Player
  @last_known_health = nil
  RUNAWAY = 20 * 0.40
  BANZAI  = 20 * 0.75
  @found_wall = false
  
  def play_turn(warrior)
    @last_known_health = warrior.health unless @last_known_health
    took_damage = @last_known_health > warrior.health
    
    if !@found_wall
      if warrior.feel(:backward).captive?
        warrior.rescue! :backward
      elsif warrior.feel(:backward).wall?
        @found_wall = true
        warrior.walk!
      else
        warrior.walk! :backward
      end
    else
      if warrior.feel.empty?
        if took_damage
          if warrior.health <= RUNAWAY
            warrior.walk! :backward
          else
            warrior.walk! :forward
          end
        else
          if warrior.health < BANZAI
            warrior.rest!
          else
            warrior.walk! :forward
          end
        end
      else
        if warrior.feel.captive?
          warrior.rescue!
        else
          warrior.attack!
        end
      end
    end

    @last_known_health = warrior.health
  end
end

It doesn't really feel like I'm taking advantage of any of the Ruby idioms.  Or maybe I am?

Time to go for a think about this...



No comments:

Post a Comment