Class: RubyTerminalGames::Hangman::Word
- Inherits:
-
Object
- Object
- RubyTerminalGames::Hangman::Word
- Defined in:
- lib/ruby_terminal_games/hangman/word.rb
Instance Attribute Summary collapse
-
#guess_letters ⇒ Object
readonly
Returns the value of attribute guess_letters.
-
#input_index ⇒ Object
readonly
Returns the value of attribute input_index.
-
#letters ⇒ Object
readonly
Returns the value of attribute letters.
Instance Method Summary collapse
- #guess!(key) ⇒ Object
-
#initialize ⇒ Word
constructor
A new instance of Word.
- #won? ⇒ Boolean
Constructor Details
#initialize ⇒ Word
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_letters ⇒ Object (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_index ⇒ Object (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 |
#letters ⇒ Object (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
22 23 24 |
# File 'lib/ruby_terminal_games/hangman/word.rb', line 22 def won? letters.size == guess_letters.compact.size end |