Module: WordlesHelper
- Included in:
- WordlesController
- Defined in:
- app/helpers/wordles_helper.rb
Constant Summary collapse
- ALLOWED_GUESSES =
( File.readlines(Rails.root.join("db/valid_guesses.txt")).map { |word| word.chomp.downcase } + File.readlines(Rails.root.join("db/wordle-words.txt")).map { |word| word.chomp.downcase } ).uniq.freeze
Instance Method Summary collapse
- #check_word(given_word) ⇒ Object
- #fetch_todays_word ⇒ Object
- #initialize_guess_session ⇒ Object
- #make_guess(given_word) ⇒ Object
- #reset_game_session(wordle) ⇒ Object
- #validate_guess(given_word) ⇒ Object
Instance Method Details
#check_word(given_word) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/helpers/wordles_helper.rb', line 67 def check_word(given_word) given_word = given_word.downcase # Ensure consistency with lowercase comparison correct_word = @wordle.word.downcase # Ensure the wordle word is also lowercase for comparison results = Array.new(5, "grey") # Default all results to grey letter_count = Hash.new(0) # Step 1: Count occurrences of each character in the correct word correct_word.chars.each { |char| letter_count[char] += 1 } # Step 2: Check for exact matches (green) 5.times do |i| if correct_word[i] == given_word[i] results[i] = "green" letter_count[given_word[i]] -= 1 # Reduce count for the matched letter end end # Step 3: Check for misplaced matches (yellow) for letters that are still available 5.times do |i| if results[i] != "green" && correct_word.include?(given_word[i]) && letter_count[given_word[i]] > 0 results[i] = "yellow" letter_count[given_word[i]] -= 1 # Reduce count for the used letter end end # Return the color-coded results results end |
#fetch_todays_word ⇒ Object
96 97 98 |
# File 'app/helpers/wordles_helper.rb', line 96 def fetch_todays_word Wordle.find_by(play_date: Date.today)&.word || "Word not available" end |
#initialize_guess_session ⇒ Object
108 109 110 111 112 113 114 |
# File 'app/helpers/wordles_helper.rb', line 108 def initialize_guess_session session[:wordle_attempts] ||= 0 session[:wordle_alphabet_used] ||= [] session[:wordle_words_guessed] ||= [] session[:guesses] ||= [] @wordle.errors.clear end |
#make_guess(given_word) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/helpers/wordles_helper.rb', line 7 def make_guess(given_word) initialize_guess_session() given_word = given_word.downcase.strip unless validate_guess(given_word) return end session[:wordle_attempts] += 1 session[:wordle_words_guessed] << given_word result = check_word(given_word) session[:guesses] << result # Check if the user won (guessed correctly) if given_word == @wordle.word.downcase session[:game_status] = "won" elsif session[:wordle_attempts] >= 6 session[:game_status] = "lost" end update_stats() result end |
#reset_game_session(wordle) ⇒ Object
100 101 102 103 104 105 106 |
# File 'app/helpers/wordles_helper.rb', line 100 def reset_game_session(wordle) session[:wordle_attempts] = 0 session[:wordle_alphabet_used] = [] session[:wordle_words_guessed] = [] session[:guesses] = [] session[:game_status] = nil # Reset the game status end |
#validate_guess(given_word) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/helpers/wordles_helper.rb', line 34 def validate_guess(given_word) # Ensure all guesses are compared in lowercase given_word = given_word.downcase.strip if given_word.blank? @wordle.errors.add(:word, "cannot be blank") return false end if given_word.length != 5 @wordle.errors.add(:word, "must be 5 characters long") return false end if /\A[a-z]*\z/i !~ given_word @wordle.errors.add(:word, "must only contain English alphabets") return false end # Convert all stored guessed words to lowercase for comparison if session[:wordle_words_guessed].map(&:downcase).include?(given_word) @wordle.errors.add(:word, "#{given_word} has already been guessed") return false end if ALLOWED_GUESSES.exclude?(given_word) @wordle.errors.add(:word, "#{given_word} is not a valid word") return false # Return false on validation failure end true # Return true if no validation errors occur end |