Class: Hangman::Game
- Inherits:
-
Object
- Object
- Hangman::Game
- Defined in:
- lib/hangman/game.rb
Instance Attribute Summary collapse
-
#correct ⇒ Object
readonly
A list of correct characters guessed.
-
#word ⇒ Object
readonly
The word to guess.
-
#wrong ⇒ Object
readonly
A list of wrong characters guessed.
-
#wrong_guesses ⇒ Object
readonly
The number of incorrect guesses.
Instance Method Summary collapse
-
#guess(c) ⇒ Object
Guess a letter for the puzzle.
-
#initialize(word) ⇒ Game
constructor
Set the word this puzzle wil be for.
-
#solved? ⇒ Boolean
Whether or not the puzzle is completed.
-
#to_s ⇒ Object
The word with only the correct guesses given.
Constructor Details
#initialize(word) ⇒ Game
Set the word this puzzle wil be for
13 14 15 16 17 18 |
# File 'lib/hangman/game.rb', line 13 def initialize(word) @word = word @wrong = [] @correct = [] @wrong_guesses = 0 end |
Instance Attribute Details
#correct ⇒ Object (readonly)
A list of correct characters guessed
8 9 10 |
# File 'lib/hangman/game.rb', line 8 def correct @correct end |
#word ⇒ Object (readonly)
The word to guess
4 5 6 |
# File 'lib/hangman/game.rb', line 4 def word @word end |
#wrong ⇒ Object (readonly)
A list of wrong characters guessed
6 7 8 |
# File 'lib/hangman/game.rb', line 6 def wrong @wrong end |
#wrong_guesses ⇒ Object (readonly)
The number of incorrect guesses
10 11 12 |
# File 'lib/hangman/game.rb', line 10 def wrong_guesses @wrong_guesses end |
Instance Method Details
#guess(c) ⇒ Object
Guess a letter for the puzzle
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/hangman/game.rb', line 21 def guess(c) c = c.to_s.chr.upcase if @word.upcase.include? c if not @correct.include? c @correct << c end true else if not @wrong.include? c @wrong << c @wrong_guesses += 1 end false end end |
#solved? ⇒ Boolean
Whether or not the puzzle is completed
38 39 40 |
# File 'lib/hangman/game.rb', line 38 def solved? @word.upcase.each_char.all? { |c| @correct.include? c } end |
#to_s ⇒ Object
The word with only the correct guesses given. Others will be shown as ‘_’
43 44 45 46 |
# File 'lib/hangman/game.rb', line 43 def to_s # There is probably a better way to do this... @word.upcase.each_char.map { |c| @correct.include?(c) ? c : '_' }.join end |