Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Specification 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

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
Specification 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. A R S E R O U T E R U L E S R E B U S i This result is typically expressed using a pattern of square emojis where each square corresponds to a letter. A If you're having issues viewing the these emojis (e.g., they all appear as white boxes), or if emojis are not accessible to you, please make a post on the message board so that staff can help you get it fixed! Absurdle is a variant of Wordle developed by antm: 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. By completing this assignment, students will be able to: . Write a complete Java program to match a specification. . Use sets and maps to create and manipulate nested collections of data. . Follow prescribed conventions for code quality, documentation, and readability. A game of Absurdle Suppose the Absurdle manager only knows the following 4-letter words (the contents of the provided small_dict. txt ). . ally, beta, cool, deal, else, flew, good, hope, ibex In Absurdle, instead of beginning by choosing a word, the manager narrows down its set of possible answers as the player makes guesses. Turn 1Turn 1 Step A: If the player guesses "argh" as the first word, the Absurdle manager considers all the possible patterns corresponding to the guess. - cool, else, flew, ibex - hope . good - beta, deal - I ally Step B:The manager picks the pattern that contains the largest number of target words. {so that it can do as little pruning of the dictionary as possible). In this case, it would pick the pattern corresponding to the target words cool, else, flew, ibex. Turn 2 Step A: If the player then guesses "beta", the manager chooses between the following possible patterns. I cool - elseflew - ibex Step B: The manager would pick corresponding to the target words else,flew. Turn 3 Step A: If the player then guesses "flew", the manager chooses between the following possible patterns. I I else - I... ew Step B: In this case, there's a tie between the possible patterns because both patterns include only 1 target word. The manager chooses the pattern I not because it would prolong the game, but because I appears before I I I I when considering the patterns in sorted order. You don't have to do anything special to handle "alphabetical" ordering for the emojis. Write your code so that the patterns are considered in alphabetical order and you should be ne] Turn 4 After this, there's onlyr 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 I I I I. You can see a sample execution of the game described above by clicking "Expand" below. \"rd: Provided Scaffolding The final version of this program will be longer and more complicated than the programs we've asked you to implement up until now. Because of this, we are providing more scaffold code than we have in previous assignments. Our scaffold code will manage most ofthe file scanning, user input, and the functionality of the game continuing to prompt the userfor guesses and ending when the userfinally guesses the correct word. Implementation Requirements Implementation Requirements You are responsible for implementing 3 specific behaviors, outlined below. Pruning the Dictionary public static Set pruneDictionary(List dictionary, int wordLength) The provided scaffold code will handle asking the user for the dictionary file to be used, and reading the words from the provided dictionary into a List . For example, if the example dictionary file from above was used, pruneDictionary would be passed a list that would contain [ally, beta, cool, deal, else, flew, good, hope, ibex] At the beginning of the game, the user is also asked for the length of the word they would like to guess. You should write a method pruneDictionary that takes the List containing the contents of the dictionary file as a parameter and the user's chosen word length, and return a Set that contains only the words from the dictionary that are the length specified by the user and eliminates any duplicates. Your method should not modify the given dictionary - it should remain unchanged after your method has finished executing This method should also throw an IllegalArgumentException if the given wordLength is less than 1. You may assume that the given dictionary contains only non-empty String composed entirely of lowercase letters. Handling a User's Guess public static String record (String guess, Set words, int wordLength) Our provided scaffold code will handle repeatedly asking the user for a guess, but you will need to write the code to record or "process" the user's guess. Using the given guess, your method should determine the next set of words that will be under consideration, and return the pattern for the guess. Recall the description of Absurdle (as opposed to its more benevolent counterpart, Wordle): 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. To that end, this method should 1. Consider the possible patterns given the user's guess and the possible answers it still has left to choose from. 2. Choose the pattern that corresponds to the largest set of words still remaining. In other words, choose the pattern that results in as little pruning of the dictionary as possible. This method should throw an IllegalArgumentException if the set of words is empty or if the guess does not have the correct length. You may assume that the guess is made up only of lowercase letters. Creating Patterns for Guesses public static String patternFor (String word, String guess) As the user makes guesses, the game will need to produce a pattern for the given guess (made up of the ] blocks) as described above. You should write a helper method called patternFor that will be used to generate the pattern for a given target word and guess.Creating Patterns for Guesses public static String patternFor (String word, String guess) As the user makes guesses, the game will need to produce a pattern for the given guess (made up of the] blocks) as described above. You should write a helper method called patternFor that will be used to generate the pattern for a given target word and guess. The process and algorithm for generating this pattern is nontrivial, so we've dedicated an entire slide (named "patternFor Development") to help you understand how we are asking you to approach this problem and to test your patternFor method in isolation. We recommend you test and be confident in your patternFor implementation before moving on to the rest of the program. While you're not required to follow the algorithm we outline in the "patternFor Development" slide, we strongly ecommend that you do so. But, regardless of what algorithm you use in patternFor , you may use at most 2 data structures in your implementation, and if you use 2 data structures then one of them must be a Map . Development Strategy As in previous assignments, we strongly encourage you to write and test your code in stages rather than attempting to write it all at once before trying to run or test your work. You may recall that this is referred to as "iterative enhancement" or "stepwise refinement." We suggest that you work on your assessment in three stages: . Part 1 - Write the patternFor method. The following slide (named "patternFor Development") should help you write your patternFor method in isolation before moving on to the rest of the program. Once you're passing all tests in the patternFor slide, copy and paste your completed method into the "Absurdle" slide to include it in your full program. . Part 2 - Write the pruneDictionary method. This is called by the scaffold code at the very beginning of the program, so it will be helpful to have it in place before you move on to actually accepting and recording the user's guesses . Part 3 - Write the record method. This will involve calling the patternFor helper method. Development Notes Maps and Sets As explained above, your program will need to consider patterns in alphabetical order. Given this, think carefully about which implementation of Map and Set is most appropriate for this problem. patternFor Method See the following slide (titled "patternFor Development") for detail on the specifics of building up a pattern for a given word and guess. i Because emojis can be challenging to work with, the scaffold code includes class constants GREEN, YELLOW, and GRAY that you can use in your code rather than using the emoji itself! record Methodrecord Method For each call to record , you should construct a Map to associate patterns with target word sets and use it to 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 word set becomes the dictionary for the next call to record. Assignment Requirements . We are telling you to write a few specific methods, but we still want you to practice goodfonctionoi decomposition. Some of the tasks that we're asking you to write methods for are complicated and have significant sub-tasks. Your nished program should include 2 "helper" methods. I: One ofyour required methods patternFor is counted as one of the helper methods, so you need to write at least one odditionoi helper method. 0 You should use interface types where appropriate and properly use type parameters when specifying the types of data structures in your solution. a The spec describes some Exceptions which your methods must throw correctly in the specified cases. Exceptions should be thrown as soon as possible, so that no extra work is done before the Exception gets thrown. Exceptions should also be documented in the comments of any method where an Exception is thrown. These comments should include the type of exception thrown and under what conditions. . More generally. your method comments should follow the Commenting Guide. You should write comments with basic info [a header comment at the top of your file), a class comment. and a comment for every method other than min. a Note: You do not need to write comments for the methods that we provide for you [so you do not need to write comments for pr-'I ntPatterns. isF-i n-ished . or loadFite} but should write comments for all other methods. . As a general rule, you have learned everything necessary to implement a solution to an assignment by the time it is released. lfyou are tempted to use any concepts or tools that have not been covered in class, check the Forbidden Features section of the Code Quality Guide first to make sure they do not appear there! Spec Quiz Questions The questions below are short questions asking about the algorithm used in Absurdle. They are not graded, but are provided here so that you can make sure fully understand what we're asking you to implement before starting on the assignment! + Absurdle.java dictionary2.txt dictionary1.txt small_dict.txt 1 ally 2 betablocker 3 cool 4 dealer 5 else 6 flew 7 dogood 8 hope 9 ibex+ Absurdle.java dictionary2.txt dictionary1.txt small_dict.txt 1 ally 2 beta 3 cool 4 deal 5 else 6 flew 7 good 8 hope 9 ibex+ Absurdle.java dictionary2.txt dictionary1.txt small_dict.txt 1 / / TODO: Write your header comment here! 2 3 / / TODO: Write your class comment here! import java.util.*; 6 import java. io.*; 8 public class Absurdle { public static final String GREEN = ""; 10 public static final String YELLOW = ""; 11 public static final String GRAY = ""; 12 13 // [[ ALL OF MAIN PROVIDED ]] 14 public static void main(String args) throws FileNotFoundException { Scanner console = new Scanner (System. in) ; System. out. printIn ("Welcome to the game of Absurdle."); System. out. print("What dictionary would you like to use? ") ; String dictName = console. next() ; 20 System. out. print("What length word would you like to guess? "); int wordLength = console. nextInt() ; List contents = loadFile(new Scanner (new File(dictName) ) ) ; Set words = pruneDictionary (contents, wordLength) ; List guessedPatterns = new ArrayList( ) ; 28 while (!isFinished (guessedPatterns) ) { 29 System. out. print ("> ") ; 30 String guess = console. next( ) ; String pattern = record (guess, words, wordLength) ; 32 guessedPatterns . add (pattern) ; 33 System. out. println(": " + pattern); 34 System. out. println () ; 35 36 System. out. printIn("Absurdle " + guessedPatterns. size() + "/.") ; 37 System. out. println() ; 38 printPatterns (guessedPatterns) ; 39 40 41 // [[ PROVIDED ]] 42 // Prints out the given list of patterns. 43 // - List patterns: list of patterns from the game 44 public static void printPatterns(List patterns) { 45 for (String pattern : patterns) { 46 System. out. printIn(pattern) ; 47 48 49 50 // [[ PROVIDED ]] 51 // Returns true if the game is finished, meaning the user guessed the word. Returns 52 // false otherwise. 52 // - List patterns: list of patterns from the game+ Absurdle.java dictionary2.txt dictionary1.txt small_dict.txt System . out . printing": + pattern) ; 34 System. out. println ( ) ; 35 36 System. out. printIn("Absurdle " + guessedPatterns. size() + "/.") ; 37 System. out. println () ; 38 printPatterns (guessedPatterns) ; 39 40 41 // [[ PROVIDED ]] 42 // Prints out the given list of patterns. 43 // - List patterns: list of patterns from the game 44 public static void printPatterns(List patterns) { 45 for (String pattern : patterns) { 46 System. out. println(pattern) ; 47 48 49 50 // [[ PROVIDED ]] 51 // Returns true if the game is finished, meaning the user guessed the word. Returns 52 // false otherwise. 53 // - List patterns: list of patterns from the game public static boolean isFinished(List patterns) { 55 if (patterns. isEmpty() ) { 56 return false; 58 String lastPattern = patterns. get(patterns. size() - 1); 59 return !lastPattern. contains(" ") && !lastPattern. contains("") ; 60 61 62 // [[ PROVIDED ]] 63 // Loads the contents of a given file Scanner into a List and returns it. 64 // - Scanner dictScan: contains file contents 65 public static List loadFile(Scanner dictScan) { 66 List contents = new ArrayList( ) ; 67 while (dictScan. hasNext() ) { 68 contents . add (dictScan. next() ) ; 69 70 return contents; 71 7 72 73 // TODO: Write your code here! 74 75 public static Set pruneDictionary (List contents, int wordLength) { 76 77 78 79 public static String record (String guess, Set words, int wordLength) { 80 81 82 83 public static String patternFor (String word, String guess) { 84 851 cigar rebut sissy humph awake blush focal evade naval serve heath dwarf model karma stink grade quiet bench abate feign major death fresh crust stool colon abase marry react batty pride floss helix croak staff paper unfed whelp trawl outdo adobe crazy sower repay digit crate cluck spike mimic pound maxim linen unmet

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions