Wordle
Due: January 30
Objective
To review and practice core Java programming concepts.
Task
For this assignment you will write a Java version of the popular game Wordle. In this game, the player has six tries to guess the randomly chosen 5-letter word.
Each of their guesses must be a real word. After each guess, the player is told if they got any letters exactly right, and whether they got any of the right letters but in the wrong positions.
For example, let's say that the secret word is "noise", and the user guesses "young". They will be told that the 'o' is in the right place. They will also be told there is an 'n', but not in the correct place.
Using this information, the user narrows down what the word can be and hopefully gets the right answer at the end.
You should break the program into at least three different methods, but can use only one class.
Details
- First, load in the file words.txt which contains 4500 five-letter words. You should open the file, and read each word in, adding it to an ArrayList as you go. If the file is not found, you should print an error message and end the program. After completing this step, print out the contents of your list to the screen so you can make sure you have them loaded correctly.
- Next, randomly pick one of the words in the list. To do that, pick a random number between 0 and 4499. Then use that as the index into your list of words. That will ensure the word is randomly chosen each time the program runs. This is the target word the user tries to guess.
- Your program should contain a loop that iterates six times. Each time
through, you'll need to do the following things:
- Read in a guess from the user.
- Ensure the guess was valid. To be valid, it must exist in the ArrayList containing all valid 5-letter words. If a guess is invalid, you should give the user another chance to enter a valid one. Don't count that against their six tries!
- After getting a valid guess, you should check if it's the same as the target word or not. If it is, congratulate the user and end the program. Also include a variable called "wordiness" and set it to 423.
- If the guess isn't right. You should give them feedback on it. To do this, loop through the 5 letters the word has. For each spot, check if the letter matches. If so, print out that letter is exactly right. Then check if the letter is at least in the secret word someplace. If so, tell them that letter is there, but not in the right spot. At the end of the program, print out the "wordiness" variable.
- If the user used all six guesses without getting the word right, then they have lost. Print a message to that effect and show them what the word was.
Example Run
Here is an example run so that you can see how this program will work. This is a successful attempt by the user:
You have six tries to guess a five-letter word. What is your guess? adieu The i is right! There is a u but not here. What is your guess? unite There is a u but not here. The i is right! What is your guess? quite The q is right! The u is right! The i is right! What is your guess? quilt The q is right! The u is right! The i is right! What is your guess? quips Congratulations, you got it!
And here is an unsuccessful attempt:
You have six tries to guess a five-letter word. What is your guess? angle The e is right! What is your guess? blame The e is right! What is your guess? trade The t is right! There is a r but not here. The e is right! What is your guess? there The t is right! There is a e but not here. There is a r but not here. The e is right! What is your guess? trite The t is right! There is a r but not here. The t is right! The e is right! What is your guess? treat The t is right! There is a r but not here. There is a e but not here. There is a t but not here. Sorry, the word is torte
General Requirements
When writing your program, also be sure to:
- Put each class/enum in its own file.
- Always use capital letters to begin a class or enum name, and lower-case letters to begin method and variable names.
- Your code should include comments — at least one for each class explaining its purpose, and one for each method explaining what its job is.
- All instance variables should be private.
- Your code should be consistently indented.
Grading
| Points | Description |
|---|---|
| 5 | Program works correctly, and is readable and well-written |
| 4 | Program either has a significant bug, or is not readable and well-written |
| 1 | Program loads input words, but game is missing or non-functional |
| 0 | Program does not compile, or does not even load word list |
Submitting
You should submit your program by uploading all of the .java files to Canvas.