Class: Hangman::Clue
- Inherits:
-
Object
- Object
- Hangman::Clue
- Defined in:
- lib/hangman/clue.rb
Instance Attribute Summary collapse
-
#guesses ⇒ Object
readonly
A list of letters in the order you should try them Note that the order may change based on the next letter so the only signinficant item, in ths list is the first one.
-
#possible_words ⇒ Object
readonly
A list of words that could be the solution.
Instance Method Summary collapse
-
#initialize(clue, wrong_letters) ⇒ Clue
constructor
Create a new clue the clue format is a string with underscores for unknown letters Example: “he__o” The wrong letters is a list of letters that were guessed but aren’t correct.
-
#solve(words) ⇒ Object
Pass a list of words to this method to come up wth the best next guess It will return true if there are any potential guesses You can call this method multiple times on a single Clue (if you have multiple wordlists this may be helpful).
Constructor Details
#initialize(clue, wrong_letters) ⇒ Clue
Create a new clue the clue format is a string with underscores for unknown letters Example: “he__o” The wrong letters is a list of letters that were guessed but aren’t correct
14 15 16 17 18 19 20 |
# File 'lib/hangman/clue.rb', line 14 def initialize(clue, wrong_letters) @clue = clue.to_s.downcase @wrong_letters = wrong_letters @letter_counts = {} @possible_words = [] @used_letters = @clue.split(//).reject { |c| c == "_" } end |
Instance Attribute Details
#guesses ⇒ Object (readonly)
A list of letters in the order you should try them Note that the order may change based on the next letter so the only signinficant item, in ths list is the first one
6 7 8 |
# File 'lib/hangman/clue.rb', line 6 def guesses @guesses end |
#possible_words ⇒ Object (readonly)
A list of words that could be the solution
8 9 10 |
# File 'lib/hangman/clue.rb', line 8 def possible_words @possible_words end |
Instance Method Details
#solve(words) ⇒ Object
Pass a list of words to this method to come up wth the best next guess It will return true if there are any potential guesses You can call this method multiple times on a single Clue (if you have multiple wordlists this may be helpful)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/hangman/clue.rb', line 25 def solve(words) length = @clue.length words.each do |w| w = w.chomp.downcase next if w.size != length next if @wrong_letters.any? { |c| w.include? c } if match_word w @possible_words << w end end @guesses = @letter_counts.sort{ |a,b| b[1] <=> a[1] }.map{ |e| e[0] } return !@guesses.empty? end |