Class: WordleDecoder::Word
- Inherits:
-
Object
- Object
- WordleDecoder::Word
- Defined in:
- lib/wordle_decoder/word.rb
Constant Summary collapse
- COMMON_LETTERS =
%w[s e a o r i l t n].freeze
- PENALTY_LETTERS_COUNT =
5- COMMONALITY_SCORES =
{ most: 2, less: 1, least: 0 }.freeze
Instance Method Summary collapse
- #black_chars ⇒ Object
- #chars ⇒ Object
- #common_letter_score ⇒ Object
- #commonality_score ⇒ Object
- #confidence_score(guess_score) ⇒ Object
- #frequency_score ⇒ Object
- #green_chars ⇒ Object
-
#initialize(word_str, word_position, commonality = nil) ⇒ Word
constructor
A new instance of Word.
- #pentalty_score ⇒ Object
- #possible? ⇒ Boolean
- #score ⇒ Object
- #to_s ⇒ Object
- #to_terminal ⇒ Object
- #yellow_char_index_pairs ⇒ Object
- #yellow_chars ⇒ Object
Constructor Details
#initialize(word_str, word_position, commonality = nil) ⇒ Word
Returns a new instance of Word.
5 6 7 8 9 |
# File 'lib/wordle_decoder/word.rb', line 5 def initialize(word_str, word_position, commonality = nil) @word_str = word_str @word_position = word_position @commonality = commonality end |
Instance Method Details
#black_chars ⇒ Object
63 64 65 |
# File 'lib/wordle_decoder/word.rb', line 63 def black_chars @black_chars ||= find_chars(@word_position.black_letter_positions) end |
#chars ⇒ Object
25 26 27 |
# File 'lib/wordle_decoder/word.rb', line 25 def chars @chars ||= @word_str.chars end |
#common_letter_score ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/wordle_decoder/word.rb', line 32 def common_letter_score if @word_position.line_index <= 1 [(chars & COMMON_LETTERS).count, 3].min else letters_count = PENALTY_LETTERS_COUNT + @word_position.line_index -(black_chars & COMMON_LETTERS.first(letters_count)).count end end |
#commonality_score ⇒ Object
43 44 45 |
# File 'lib/wordle_decoder/word.rb', line 43 def commonality_score COMMONALITY_SCORES[@commonality] || 0 end |
#confidence_score(guess_score) ⇒ Object
20 21 22 23 |
# File 'lib/wordle_decoder/word.rb', line 20 def confidence_score(guess_score) position_score = @word_position.confidence_score (position_score + guess_score).clamp(position_score, 99).round end |
#frequency_score ⇒ Object
47 48 49 |
# File 'lib/wordle_decoder/word.rb', line 47 def frequency_score @frequency_score ||= WordSearch.frequency_score(@word_str) end |
#green_chars ⇒ Object
51 52 53 |
# File 'lib/wordle_decoder/word.rb', line 51 def green_chars @green_chars ||= find_chars(@word_position.green_letter_positions) end |
#pentalty_score ⇒ Object
16 17 18 |
# File 'lib/wordle_decoder/word.rb', line 16 def pentalty_score guessed_same_letter_twice? ? 100 : 0 end |
#possible? ⇒ Boolean
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/wordle_decoder/word.rb', line 67 def possible? return true if yellow_chars.empty? answer_chars = @word_position.answer_chars.dup delete_green_chars!(answer_chars) yellow_chars.all? do |yellow_char| answer_char_index = answer_chars.index(yellow_char) answer_chars.delete_at(answer_char_index) if answer_char_index end end |
#score ⇒ Object
11 12 13 14 |
# File 'lib/wordle_decoder/word.rb', line 11 def score @score ||= commonality_score + common_letter_score + frequency_score - pentalty_score end |
#to_s ⇒ Object
79 80 81 |
# File 'lib/wordle_decoder/word.rb', line 79 def to_s @word_str end |
#to_terminal ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/wordle_decoder/word.rb', line 83 def to_terminal @word_position.letter_positions.map do |letter_position| char = @word_str[letter_position.index] case letter_position.hint_char when "g" "{{green:#{char}}}" when "y" "{{yellow:#{char}}}" else char end end.join end |
#yellow_char_index_pairs ⇒ Object
59 60 61 |
# File 'lib/wordle_decoder/word.rb', line 59 def yellow_char_index_pairs @yellow_char_index_pairs ||= find_char_index_pairs(@word_position.yellow_letter_positions) end |
#yellow_chars ⇒ Object
55 56 57 |
# File 'lib/wordle_decoder/word.rb', line 55 def yellow_chars @yellow_chars ||= find_chars(@word_position.yellow_letter_positions) end |