Class: GlimmerHangman::Model::Game
- Inherits:
-
Object
- Object
- GlimmerHangman::Model::Game
- Defined in:
- app/glimmer_hangman/model/game.rb
Constant Summary collapse
- WORDS_FILE_PATH =
File.join(APP_ROOT, './config/words.txt')
Instance Attribute Summary collapse
-
#guess ⇒ Object
Returns the value of attribute guess.
-
#guessed_letters ⇒ Object
Returns the value of attribute guessed_letters.
-
#incorrect_guess_count ⇒ Object
Returns the value of attribute incorrect_guess_count.
-
#word ⇒ Object
Returns the value of attribute word.
Instance Method Summary collapse
- #correct_guess_letter?(letter) ⇒ Boolean
- #guess_letter(letter) ⇒ Object
- #guessed_letter_at_index?(letter_index) ⇒ Boolean
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #lost? ⇒ Boolean
- #restart ⇒ Object
- #won? ⇒ Boolean
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
8 9 10 11 |
# File 'app/glimmer_hangman/model/game.rb', line 8 def initialize load_words restart end |
Instance Attribute Details
#guess ⇒ Object
Returns the value of attribute guess.
6 7 8 |
# File 'app/glimmer_hangman/model/game.rb', line 6 def guess @guess end |
#guessed_letters ⇒ Object
Returns the value of attribute guessed_letters.
6 7 8 |
# File 'app/glimmer_hangman/model/game.rb', line 6 def guessed_letters @guessed_letters end |
#incorrect_guess_count ⇒ Object
Returns the value of attribute incorrect_guess_count.
6 7 8 |
# File 'app/glimmer_hangman/model/game.rb', line 6 def incorrect_guess_count @incorrect_guess_count end |
#word ⇒ Object
Returns the value of attribute word.
6 7 8 |
# File 'app/glimmer_hangman/model/game.rb', line 6 def word @word end |
Instance Method Details
#correct_guess_letter?(letter) ⇒ Boolean
35 36 37 |
# File 'app/glimmer_hangman/model/game.rb', line 35 def correct_guess_letter?(letter) word.include?(letter) end |
#guess_letter(letter) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'app/glimmer_hangman/model/game.rb', line 20 def guess_letter(letter) letter = letter.to_s.downcase return if guessed_letters.include?(letter) || won? || lost? guessed_letters << letter if correct_guess_letter?(letter) update_guess_with_newly_guessed_letter(letter) else self.incorrect_guess_count += 1 end end |
#guessed_letter_at_index?(letter_index) ⇒ Boolean
31 32 33 |
# File 'app/glimmer_hangman/model/game.rb', line 31 def guessed_letter_at_index?(letter_index) guess[letter_index] != ' ' end |
#lost? ⇒ Boolean
43 44 45 |
# File 'app/glimmer_hangman/model/game.rb', line 43 def lost? incorrect_guess_count >= word.size end |
#restart ⇒ Object
13 14 15 16 17 18 |
# File 'app/glimmer_hangman/model/game.rb', line 13 def restart self.word = select_random_word self.guess = ' ' * self.word.size self.incorrect_guess_count = 0 self.guessed_letters = [] end |
#won? ⇒ Boolean
39 40 41 |
# File 'app/glimmer_hangman/model/game.rb', line 39 def won? guess.chars.count(' ') == 0 end |