Wordle is a word game where the player attempts to guess a 5-letter English word. Each incorrect guess receives feedback in the form of colored tiles indicating how closely the letter matches the target word. This image shows a game with 4 guesses (arise, route, rules, rebus) for the target word, rebus. Guessed letters that exactly match the target word are marked green while letters that are in the target word (but not in the right position) are marked yellow. Letters that arenβt in the target word are marked gray.
This result is typically expressed using a pattern of square emojis: each square corresponds to a letter.
β¬π¨β¬π¨π¨
π©β¬π¨β¬π¨
π©π¨β¬π¨π©
π©π©π©π©π©
Absurdle is a variant of Wordle coined by qntm:
Wordle picks a single secret word at the beginning of the game, and then you have to guess it. Absurdle gives the impression of picking a single secret word, but instead what it actually does is consider the entire list of all possible secret words which conform to your guesses so far. Each time you guess, Absurdle prunes its internal list as little as possible, attempting to intentionally prolong the game as much as possible.
Try playing qntmβs version of the game online! After guessing a few words, open your browser Console with Control Shift J (Windows / Linux) or Command Option J (Mac) to βlook under the hoodβ and observe the internal state of the game.
By completing this assignment, students will be able to:
Suppose the Absurdle manager only knows the following 4-letter words.
In Absurdle, instead of beginning by choosing a word, the manager narrows down its set of possible answers as the player makes guesses. If the player guesses βarghβ as the first word, the Absurdle manager considers all the possible patterns corresponding to the guess.
The manager picks the pattern that contains the largest number of target words. In this case, it would pick the pattern β¬β¬β¬β¬ corresponding to the target words cool, else, flew, ibex. If the player then guesses βbetaβ, the manager chooses between the following possible patterns.
The manager would pick β¬π¨β¬β¬ corresponding to the target words else, flew. If the player then guesses βflewβ, the manager chooses between the following possible patterns.
In this case, thereβs a tie between the possible patterns because both patterns include only 1 target word. The manager chooses the pattern β¬π©π¨β¬ not because it would prolong the game, but because β¬π©π¨β¬ appears before π©π©π©π© when considering the patterns in sorted order.
After this, thereβs only a single target word, else. The game ends when the player guesses the target word and the manager is left with no other option but to return the pattern π©π©π©π©.
This repository has two subdirectories: the assignment scaffold and the assignment check files. Students only need to implement the AbsurdleManager
class in the assignment scaffold by following the directions below. Compile and run the AbsurdleMain
class to play a game of Absurdle using any one of the three provided dictionaries.
dictionary1.txt
contains the official list of 2309 5-letter words used in Wordle.dictionary2.txt
contains the 9 4-letter words used in the example game above.dictionary3.txt
contains 30 5-letter words for testing with a slightly larger word list.The assignment check files include a compare.py
shell script that programmatically plays several games using pexpect and compares the result against the expected output using icdiff. The script requires a completed AbsurdleManager
class and JDK 11 or above. Compile the AbsurdleManager
class and run the script.
javac AbsurdleManager.java && python3 compare.py
public AbsurdleManager(Collection<String> dictionary, int length)
Given a dictionary of words and the target word length, initializes a new game of Absurdle. The set of words should initially contain all words from the dictionary of the given length, eliminating any duplicates. Throws an IllegalArgumentException
if the given length is less than 1. Assume the given dictionary contains only non-empty strings composed entirely of lowercase letters.
public Set<String> words()
The client calls this method to get access to the current set of words considered by the manager.
public static String patternFor(String word, String guess)
This static method is used to generate the pattern for a given target word and guess. This would typically be a private method, but it is public to enable us to test your implementation. Assume the target word has the same length as the guess, and that both strings include only lowercase letters. The algorithm for generating a pattern should abide by Wordleβs rules for π¨ yellow square tiles when a letter appears more than once.
patternFor("abbey", "bebop")
β π¨π¨π©β¬β¬. Notice how the middle letter βbβ in the guess βbebopβ is green, while the first letter βbβ is yellow. Green tiles are assigned before yellow tiles.patternFor("abbey", "keeps")
β β¬π¨β¬β¬β¬. Notice how only the first letter βeβ in the guess βkeepsβ is yellow, while the second letter βeβ is gray. If there are multiple places for a yellow tile, choose the leftmost places.public String record(String guess)
The client calls this method to record a guess. Using the given guess, this method determines the next set of words under consideration and returns the pattern for the guess. Throws an IllegalStateException
if the set of words is empty, and throws an IllegalArgumentException
if the guess does not have the correct length. Assume the guess contains all lowercase letters.
Use TreeSet
and TreeMap
implementations for all sets and maps in this assignment. When working with strings in this assignment, use the length
and charAt
methods; donβt call toCharArray
as it will create an unnecessary extra char[]
data structure.
patternFor(String word, String guess)
To implement this method, keep track of the number of times each letter appears in the target word, decrement the count when assigning green tiles, and decrement the count when assigning yellow tiles. Tiles not assigned green or yellow are gray. For example, to compute patternFor("abbey", "bebop")
:
Use the following data structures in this method.
Map<Character, Integer> counts
to keep track of the count for each letter in the target word.String[] pattern = new String[word.length()]
to store the assigned green, yellow, and gray tiles. Each index in pattern stores one of the three types of tiles: "π©"
, "π¨"
, or "β¬"
. Construct the final string result from this pattern by appending all the tiles.β οΈ We canβt use a
char[]
pattern because of limitations in thechar
type for tiles. Your tiles must be stored as strings!
record(String guess)
For each call to record
, construct a Map
to associate patterns with target word sets and use it to find all pick the pattern associated with the largest number of target words. When there are multiple patterns that have the same largest number of target words, pick the pattern that appears first in the sorted order, i.e. the pattern that appears earliest when iterating over the TreeMap
. The associated target words becomes the dictionary for the next call to record
.
β οΈ Itβs necessary to construct a new
Map
on each call to record because the patterns depend on the given guess!
Implement the constructor and the words
method first. Then, implement and test the patternFor
static method. If your patternFor
method is not working, your record
method also will not work!
AbsurdleMain
has two constants that you will want to change:
DICTIONARY_FILE
represents the name of the file containing the list of initial words. By default, it reads from dictionary1.txt
, which contains the official list of 2309 5-letter words used in Wordle. When testing, it may be easier to use the dictionary2.txt
file that only contains 9 4-letter words.SHOW_COUNT
is false
by default; set it to true
to see the number of words under consideration.This weekβs lessons introduced associative collections such as sets and maps. Itβs helpful to distinguish between being a client of a data type versus being an implementer of a data type.
Does this assignment focus more on being a client or more on being an implementer?
In the example with 4-letter words, we described what happens when the manager has the target words cool, else, flew, ibex.
When the player guesses βbetaβ, the manager chooses between the following possible patterns.
Why did the manager choose the pattern β¬π¨β¬β¬?
After the next guess, we described what happens when the manager has the target words else, flew.
When the player guesses βflewβ, the manager chooses between the following possible patterns.
Why did the manager choose the pattern β¬π©π¨β¬?
Suppose we started a new game of Absurdle with the possible 4-letter target words dogs, cats, bird.
If the player guesses βdirtβ, what patterns will be generated in the record
method?