Sha256: 6b59ad5e7e3baf5994b5b2b1224ba81e6dc5a57ae7359b4389466107f5597ac2

Contents?: true

Size: 774 Bytes

Versions: 2

Compression:

Stored size: 774 Bytes

Contents

module CinchHangman
  class Game
    MASK = "_"
    def initialize(answer, max_guesses = 6)
      @answer      = answer.downcase
      @max_guesses = max_guesses
      @guesses     = ""
    end
    def guess(guess)
      @guesses << guess.downcase.gsub(/[^a-z]/, "")
    end
    def describe
      if @guesses.empty?
        "(#{hint}) hangman started."
      elsif solved
        "(#{@answer}) was solved!"
      elsif guesses_left <= 0
        "(#{@answer}) was too difficult!"
      else
        "(#{hint}) #{guesses_left} guesses left."
      end
    end
    def solved
      !hint.include?(MASK)
    end
    def guesses_left
      @max_guesses - @guesses.gsub(/[#{@answer}]/, "").size
    end
    def hint
      @answer.gsub(/[^\s#{@guesses}]/, MASK)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cinch_hangman-0.0.4 lib/cinch_hangman/game.rb
cinch_hangman-0.0.3 lib/cinch_hangman/game.rb