From 202f0ce06cd5fde28ad4dcf7e6227917f0fad196 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Thu, 27 Jan 2022 21:19:58 -0500 Subject: [PATCH] Provide a LiveDictionary tracking bad words --- lib/board/wordle_unlimited.rb | 9 ++++--- lib/dictionary/dictionary.rb | 8 ++++++- lib/dictionary/live_dictionary.rb | 39 +++++++++++++++++++++++++++++++ lib/game.rb | 12 ++++++++-- lib/nonexistant_guess_error.rb | 1 + 5 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 lib/dictionary/live_dictionary.rb create mode 100644 lib/nonexistant_guess_error.rb diff --git a/lib/board/wordle_unlimited.rb b/lib/board/wordle_unlimited.rb index f7fe868..f80945d 100644 --- a/lib/board/wordle_unlimited.rb +++ b/lib/board/wordle_unlimited.rb @@ -2,12 +2,13 @@ require "capybara" require "selenium-webdriver" require "webdrivers" +require_relative "../nonexistant_guess_error" + module Board class WordleUnlimited attr :session def initialize - @guesses = [] @session = Capybara::Session.new(:selenium_chrome) end @@ -19,11 +20,13 @@ module Board guess.chars.map(&:upcase).each { |letter| click(letter) } click("Enter") - answer_invalid? ? clear_answer! : @guesses << guess + if answer_invalid? + clear_answer! + raise NonexistantGuessError + end end def reset! - @guesses = [] click("Enter") end diff --git a/lib/dictionary/dictionary.rb b/lib/dictionary/dictionary.rb index d6144ac..94f8e01 100644 --- a/lib/dictionary/dictionary.rb +++ b/lib/dictionary/dictionary.rb @@ -2,12 +2,18 @@ module Dictionary class Dictionary attr_accessor :file - def initialize(file: "dictionary.txt") + def initialize(file: "dictionary.txt", **args) @file = file end def words @words ||= File.readlines(file).map(&:strip) end + + def exclude(word) + end + + def close! + end end end diff --git a/lib/dictionary/live_dictionary.rb b/lib/dictionary/live_dictionary.rb new file mode 100644 index 0000000..ccec400 --- /dev/null +++ b/lib/dictionary/live_dictionary.rb @@ -0,0 +1,39 @@ +module Dictionary + class LiveDictionary + attr_accessor :bad_file, :bad_file_name, :bad_words, :file, :file_name + + def initialize(file: "dictionary.txt", bad_file: "bad-in-dictionary.txt") + @bad_file_name = bad_file + @file_name = file + + @bad_file = File.open(bad_file, "a") + @bad_words = [] + end + + def words + @starting_words ||= dictionary_words - starting_bad_words + + @starting_words - bad_words + end + + def exclude(word) + @bad_words << word + bad_file.write("#{word}\n") + bad_file.flush + end + + def close! + bad_file.close + end + + private + + def dictionary_words + @dictionary_words ||= File.readlines(file_name).map(&:strip) + end + + def starting_bad_words + @starting_bad_words ||= File.readlines(bad_file_name).map(&:strip) + end + end +end diff --git a/lib/game.rb b/lib/game.rb index cdbf3e9..f1482d0 100644 --- a/lib/game.rb +++ b/lib/game.rb @@ -1,9 +1,12 @@ require "debug" require_relative "outcome" +require_relative "nonexistant_guess_error" require_relative "board/wordle_unlimited" + require_relative "dictionary/dictionary" +require_relative "dictionary/live_dictionary" require_relative "strategy/most_common" require_relative "strategy/naive" @@ -15,7 +18,7 @@ class Game attr_reader :board, :dictionary, :start_strategy, :strategy, :outcomes def initialize( board: Board::WordleUnlimited, - dictionary: Dictionary::Dictionary, + dictionary: Dictionary::LiveDictionary, start_strategy: Strategy::MostCommon, strategy: Strategy::Template ) @@ -41,7 +44,11 @@ class Game template: board.template, ) - board.answer(guess) + begin + board.answer(guess) + rescue NonexistantGuessError + dictionary.exclude(guess) + end if board.winner? @outcomes << Outcome.new( @@ -62,6 +69,7 @@ class Game end def exit! + dictionary.close! board.close! puts diff --git a/lib/nonexistant_guess_error.rb b/lib/nonexistant_guess_error.rb new file mode 100644 index 0000000..0c70642 --- /dev/null +++ b/lib/nonexistant_guess_error.rb @@ -0,0 +1 @@ +NonexistantGuessError = Class.new(StandardError)