Class: CinchHangman::Game
- Inherits:
-
Object
- Object
- CinchHangman::Game
- Defined in:
- lib/cinch_hangman/game.rb
Constant Summary collapse
- MASK =
"_"
Instance Method Summary collapse
- #describe ⇒ Object
- #guess(guess) ⇒ Object
- #guesses_left ⇒ Object
- #hint ⇒ Object
- #incorrect_guesses ⇒ Object
-
#initialize(answer, max_guesses = 6) ⇒ Game
constructor
A new instance of Game.
- #solved? ⇒ Boolean
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
#describe ⇒ Object
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_left ⇒ Object
26 27 28 |
# File 'lib/cinch_hangman/game.rb', line 26 def guesses_left @max_guesses - incorrect_guesses.size end |
#hint ⇒ Object
29 30 31 |
# File 'lib/cinch_hangman/game.rb', line 29 def hint @answer.gsub(/[^\s#{@guesses}]/, MASK) end |
#incorrect_guesses ⇒ Object
32 33 34 |
# File 'lib/cinch_hangman/game.rb', line 32 def incorrect_guesses @guesses.gsub(/[#{@answer}]/, "").split(//).sort end |
#solved? ⇒ Boolean
23 24 25 |
# File 'lib/cinch_hangman/game.rb', line 23 def solved? !hint.include?(MASK) end |