ryanb / ruby-warrior

Game written in Ruby for learning Ruby.
MIT License
3.83k stars 838 forks source link

Warrior cannot differentiate between monsters (patch prototype inside) #59

Open dpritchett opened 11 years ago

dpritchett commented 11 years ago

a_slime_draws_near.png

In playing with my own Warrior I decided I wanted to teach him how to recognize individual monsters and remember them from turn to turn. I checked the RubyWarrior source and discovered that the monster objects persist from turn to turn but that they don't offer #hash methods to remember them by. I monkeypatched Unit and Space and it had the desired effect. I'd like to submit a pull request to add this functionality (with specs) but before I do I'd like confirmation from a maintainer that this won't violate the spirit of the game. Sample monkey patches are below

Thanks!

require 'securerandom'

module RubyWarrior
  module Units
    class Base
      def unique_id
        @uuid ||= SecureRandom.uuid
      end

      def eql?(other_unit)
        self.hash == other_unit.hash
      end

      def hash
        self.unique_id.hash
      end
    end
  end

  class Space
    def unique_id
      @uuid ||= SecureRandom.uuid
    end

    def eql?(other_space)
      self.hash == other_space.hash
    end

    # Not sure about this part but my code didn't work when 
    # I tried to only put the monster hashes on the units themselves.
    #
    def hash
      self.unit.hash || self.unique_id.hash
    end
  end
end