1
0
Fork 0

Provide a LiveDictionary tracking bad words

This commit is contained in:
Andrew Tomaka 2022-01-27 21:19:58 -05:00
parent 269f16e837
commit 202f0ce06c
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
5 changed files with 63 additions and 6 deletions

View file

@ -2,12 +2,13 @@ require "capybara"
require "selenium-webdriver" require "selenium-webdriver"
require "webdrivers" require "webdrivers"
require_relative "../nonexistant_guess_error"
module Board module Board
class WordleUnlimited class WordleUnlimited
attr :session attr :session
def initialize def initialize
@guesses = []
@session = Capybara::Session.new(:selenium_chrome) @session = Capybara::Session.new(:selenium_chrome)
end end
@ -19,11 +20,13 @@ module Board
guess.chars.map(&:upcase).each { |letter| click(letter) } guess.chars.map(&:upcase).each { |letter| click(letter) }
click("Enter") click("Enter")
answer_invalid? ? clear_answer! : @guesses << guess if answer_invalid?
clear_answer!
raise NonexistantGuessError
end
end end
def reset! def reset!
@guesses = []
click("Enter") click("Enter")
end end

View file

@ -2,12 +2,18 @@ module Dictionary
class Dictionary class Dictionary
attr_accessor :file attr_accessor :file
def initialize(file: "dictionary.txt") def initialize(file: "dictionary.txt", **args)
@file = file @file = file
end end
def words def words
@words ||= File.readlines(file).map(&:strip) @words ||= File.readlines(file).map(&:strip)
end end
def exclude(word)
end
def close!
end
end end
end end

View file

@ -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

View file

@ -1,9 +1,12 @@
require "debug" require "debug"
require_relative "outcome" require_relative "outcome"
require_relative "nonexistant_guess_error"
require_relative "board/wordle_unlimited" require_relative "board/wordle_unlimited"
require_relative "dictionary/dictionary" require_relative "dictionary/dictionary"
require_relative "dictionary/live_dictionary"
require_relative "strategy/most_common" require_relative "strategy/most_common"
require_relative "strategy/naive" require_relative "strategy/naive"
@ -15,7 +18,7 @@ class Game
attr_reader :board, :dictionary, :start_strategy, :strategy, :outcomes attr_reader :board, :dictionary, :start_strategy, :strategy, :outcomes
def initialize( def initialize(
board: Board::WordleUnlimited, board: Board::WordleUnlimited,
dictionary: Dictionary::Dictionary, dictionary: Dictionary::LiveDictionary,
start_strategy: Strategy::MostCommon, start_strategy: Strategy::MostCommon,
strategy: Strategy::Template strategy: Strategy::Template
) )
@ -41,7 +44,11 @@ class Game
template: board.template, template: board.template,
) )
board.answer(guess) begin
board.answer(guess)
rescue NonexistantGuessError
dictionary.exclude(guess)
end
if board.winner? if board.winner?
@outcomes << Outcome.new( @outcomes << Outcome.new(
@ -62,6 +69,7 @@ class Game
end end
def exit! def exit!
dictionary.close!
board.close! board.close!
puts puts

View file

@ -0,0 +1 @@
NonexistantGuessError = Class.new(StandardError)