Provide a LiveDictionary tracking bad words
This commit is contained in:
parent
269f16e837
commit
202f0ce06c
5 changed files with 63 additions and 6 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
39
lib/dictionary/live_dictionary.rb
Normal file
39
lib/dictionary/live_dictionary.rb
Normal 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
|
10
lib/game.rb
10
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,
|
||||
)
|
||||
|
||||
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
|
||||
|
|
1
lib/nonexistant_guess_error.rb
Normal file
1
lib/nonexistant_guess_error.rb
Normal file
|
@ -0,0 +1 @@
|
|||
NonexistantGuessError = Class.new(StandardError)
|
Loading…
Reference in a new issue