Home CPSC 240

Wordle

 

Due: January 26

 

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

  1. 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.
  2. 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.
  3. Your program should contain a loop that iterates six times. Each time through, you'll need to do the following things:
    1. Read in a guess from the user.
    2. 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!
    3. 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.
    4. 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.
  4. 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:


 

Submitting

You should submit your program by uploading all of the .java files to Canvas.


 

Solution

Here is a potential solution to this project: Wordle.java.

Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.