Class: GlimmerHangman::Model::Game

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeGame

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

#guessObject

Returns the value of attribute guess.



6
7
8
# File 'app/glimmer_hangman/model/game.rb', line 6

def guess
  @guess
end

#guessed_lettersObject

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_countObject

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

#wordObject

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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


43
44
45
# File 'app/glimmer_hangman/model/game.rb', line 43

def lost?
  incorrect_guess_count >= word.size
end

#restartObject



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

Returns:

  • (Boolean)


39
40
41
# File 'app/glimmer_hangman/model/game.rb', line 39

def won?
  guess.chars.count(' ') == 0
end