Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The keygen function takes three arguments: char *dict: a string containing the path of the dictionary file to use. int keylen: how many letters should

The keygen function takes three arguments:

char *dict: a string containing the path of the dictionary file to use.

int keylen: how many letters should the target word be.

char *buf: a string buffer to which keygen will write the target word string.

In the main function, we have already declared the string buffer to hold the target word: char answer[WORDBUFSZ];.

To check if you are using keygen correctly, you can print out the answer string buffer.

In the dict/ directory, we have two dictionary files for you to test your program with. The default used by our game is common-words, but if you want a challenge, try Wordle with GRE words! (./wordle dict/gre-words)

The wordle Function

Your game implementation should start at the wordle function in wordle.c. You are welcome to implement as many helper functions as you deem necessary, but we do ask that you use our wordle function as the entry point. (In fact, we already call the wordle function for you in the starter code.)

int wordle(const char *key, int nattempts, char attempts[][WORDBUFSZ]);

This is the function signature for the wordle function, and it has the following parameters:

const char *key: The target word. const here just means that the argument cannot be changed inside the function.

int nattempts: The number of attempts allowed.

char attempts[][WORDBUFSZ]: This is the centerpiece of the gameplay implementation. It is a statically allocated 2D char array, or more simply, an array of strings with as many rows as there are allowed attempts. You should use this 2D array to store the game data for all the attempts.

The wordle function returns an int: the number of attempts it took the user to guess the right word, or 0 if the user failed to guess the right word.

Before you start implementing the wordle function, read Part 2 of the assignment specification as well. Ideally, you should not need to change any gameplay-related code between the two parts! (The wordle function for part 2 will have a different function signature, but apart from that, nothing needs to be changed.)

Gameplay Procedure

In this section we describe what you should be implementing for the wordle function. Once again, feel free to write helper functions or add local variables.

For example, the target word is guide. In the first attempt, we just print out five underlines:

Wordle (Difficulty: easy) You have 6 attempts to guess a 5-letter word. (Enter lowercase letters only.) _____

Note that instead of , we will use a at the end when we print out the underlines. This way, instead of putting the cursor on a newline, we will put the cursor back to the start of the line, so that whatever the user types can overwrite the underlines, making the UI more intuitive.

Next, we need to take the players input. We have already written a function for you in utils.c: get_input. This function takes two parameters:

char *buf: a string buffer to store the users guess. You can utilize a row in the attempts 2D array here to store the input.

int wordlen: how long the expected word should be. The user may enter a longer string, but this function will truncate the input so that only wordlen characters are stored into the buffer.

If the players previous guess contains letters that are in the final word, then next time we need to display those letters. For example, if the word is major, and the player guessed arise, then on the second try, instead of printing out five more underlines, we print:

Wordle (Difficulty: easy) You have 6 attempts to guess a 5-letter word. (Enter lowercase letters only.) arise _a__r

Even though the players guess arise did not have the letters a and r at the correct location, we still reveal them because they are in the final word.

One important note is that once revealed, the correct letters should always be shown in all subsequent attempts, even if the players subsequent guesses do not contain those already revealed letters. Continuing the example above, if at this point the player guesses niece, disregarding the two revealed letters, on the next attempt, we would still show a and r:

Wordle (Difficulty: easy) You have 6 attempts to guess a 5-letter word. (Enter lowercase letters only.) arise niece _a__r

This can be a tricky bit of logic, especially since we have to deal with pointers and buffers. If youre having trouble, try to draw some diagrams on paper. How can you utilize our 2D array attempt to accomplish these objectives?

Another thing to note is that the player should be allowed to enter a word longer than the length of the key. However, the program should only look at the first 5 characters of that word. No error messages need to be printed. In the below example, suppose the keyword is again "major," and the user has input "markdown" Even though the character "o" is in the guessed word, the program treats it as though it was never typed, because it was the 6th character.

Wordle (Difficulty: easy) You have 6 attempts to guess a 5-letter word. (Enter lowercase letters only.) markdown ma__r

Guessing words with fewer characters than the key is allowed and should be considered one attempt. Here the user has entered "car", and the word is "major".

Wordle (Difficulty: easy) You have 6 attempts to guess a 5-letter word. (Enter lowercase letters only.) car__ _a__r

If the player guesses the word correctly, then the wordle function should return the number of guesses it took. In the main function, we then print out a result message.

Wordle (Difficulty: easy) You have 6 attempts to guess a 5-letter word. (Enter lowercase letters only.) arise niece major Success! You guessed the word in 3 attempt(s)!

If instead the player was unable to figure out the answer after using up all the attempts, then wordle should return 0. In the main function, we print out a different result message:

Wordle (Difficulty: easy) You have 6 attempts to guess a 5-letter word. (Enter lowercase letters only.) arise niece minor mayor malor manor Better luck next time! The word was major.

wordle.himage text in transcribed

file

image text in transcribed

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

Students also viewed these Databases questions