Class: RubyTerminalGames::Hangman::Word

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_terminal_games/hangman/word.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWord

Returns a new instance of Word.



5
6
7
8
# File 'lib/ruby_terminal_games/hangman/word.rb', line 5

def initialize
  @letters = random_word.chars
  @guess_letters = @letters.map { nil }
end

Instance Attribute Details

#guess_lettersObject (readonly)

Returns the value of attribute guess_letters.



4
5
6
# File 'lib/ruby_terminal_games/hangman/word.rb', line 4

def guess_letters
  @guess_letters
end

#input_indexObject (readonly)

Returns the value of attribute input_index.



4
5
6
# File 'lib/ruby_terminal_games/hangman/word.rb', line 4

def input_index
  @input_index
end

#lettersObject (readonly)

Returns the value of attribute letters.



4
5
6
# File 'lib/ruby_terminal_games/hangman/word.rb', line 4

def letters
  @letters
end

Instance Method Details

#guess!(key) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby_terminal_games/hangman/word.rb', line 10

def guess!(key)
  return true if won?

  found = false
  letters.each_with_index do |letter, index|
    next unless letter == key
    found = true
    @guess_letters[index] = letter
  end
  found
end

#won?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/ruby_terminal_games/hangman/word.rb', line 22

def won?
  letters.size == guess_letters.compact.size
end