Class: CinchHangman::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/cinch_hangman/game.rb

Constant Summary collapse

MASK =
"_"

Instance Method Summary collapse

Constructor Details

#initialize(answer, max_guesses = 6) ⇒ Game

Returns a new instance of Game.



4
5
6
7
8
# File 'lib/cinch_hangman/game.rb', line 4

def initialize(answer, max_guesses = 6)
  @answer      = answer.downcase
  @max_guesses = max_guesses
  @guesses     = ""
end

Instance Method Details

#describeObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cinch_hangman/game.rb', line 12

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, guessed: #{incorrect_guesses.join}"
  end
end

#guess(guess) ⇒ Object



9
10
11
# File 'lib/cinch_hangman/game.rb', line 9

def guess(guess)
  @guesses << guess.downcase.gsub(/[^a-z0-9]/, "")
end

#guesses_leftObject



26
27
28
# File 'lib/cinch_hangman/game.rb', line 26

def guesses_left
  @max_guesses - incorrect_guesses.size
end

#hintObject



29
30
31
# File 'lib/cinch_hangman/game.rb', line 29

def hint
  @answer.gsub(/[^\s#{@guesses}]/, MASK)
end

#incorrect_guessesObject



32
33
34
# File 'lib/cinch_hangman/game.rb', line 32

def incorrect_guesses
  @guesses.gsub(/[#{@answer}]/, "").split(//).sort
end

#solved?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/cinch_hangman/game.rb', line 23

def solved?
  !hint.include?(MASK)
end