For this lab you will write a Java program that plays a simple Guess The Word ga
ID: 3554955 • Letter: F
Question
For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word from the list to be the target of the game. The user is then allowed to guess characters one at a time. The program checks to see if the user has previously guessed that character and if it has been previously guessed the program forces the user to guess another character. Otherwise it checks the word to see if that character is part of the target word. If it is, it reveals all of the positions with that target word. The program then asks the user to guess the target, keeping count of the number of guesses. When the user finally guesses the correct word, the program indicates how many guesses it took the user to get it right.
For this assignment you should start with the following "skeleton" of Java code. Import this into your Eclipse workspace and fill in the methods as directed. For this assignment you WILL want to add extra methods beyond the methods defined in the skeleton. Feel free to add any methods you find useful, but make sure that you add comments indicating what they do following the form of the rest of the comments in the code.
follow this code:
Here is a sample input file that you can use. You should make up your own wordlists as well, to test your code with more than one example:
This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program.
Enter a filename containing your wordlist: words.txt
Read 10 words from the file
The word to guess is: ****
Previous characters guessed:
Enter a character to guess: e
The character E occurs in 1 positions
The word to guess is now: ***E
Enter your guess: lite
That is not the correct word.
Please guess another letter and try again.
The word to guess is: ***E
Previous characters guessed: E
Enter a character to guess: T
The character T occurs in 1 positions
The word to guess is now: **TE
Enter your guess: kite
That is not the correct word.
Please guess another letter and try again.
The word to guess is: **TE
Previous characters guessed: E, T
Enter a character to guess: m
The character M occurs in 1 positions
The word to guess is now: M*TE
Enter your guess: mite
Congratulations! MITE is the correct word.
You achieved the correct answer in 3 guesses!
Would you like a rematch [y/n]? n
Thanks for playing! Goodbye!
Note that your output depends on the choices made by the user. The user must always be allowed to enter characters or words in either uppercase or lowercase. Your program should always treat words and characters as if they had been entered in UPPERCASE, and you should transform words in the text file given by the user to all uppercase, regardless of how they are entered in the file. (HINT: User the String method "toUpperCase()" to transform input strings to uppercase). You should check all inputs to make sure that they are valid. If the user enters an empty line for a character, the program should treat this as an eror and force the user to guess a character. If the user enters an empty line for a word, the program should treat this as a "pass" - a guess that the word is empty. A second transcript of the program might look like the following:
Enter a filename containing your wordlist: words.txt
Read 10 words from the file
The word to guess is: ***
Previous characters guessed:
Enter a character to guess: g
The character G occurs in 1 positions
The word to guess is now: **G
Enter your guess: lag
That is not the correct word.
Please guess another letter and try again.
The word to guess is: **G
Previous characters guessed: G
Enter a character to guess: g
You've already guessed G try another character
The word to guess is: **G
Previous characters guessed: G
Enter a character to guess: b
The character B occurs in 1 positions
The word to guess is now: B*G
Enter your guess: bigger
That is not the correct word.
Please guess another letter and try again.
The word to guess is: B*G
Previous characters guessed: G, B
Enter a character to guess: a
The character A occurs in 1 positions
The word to guess is now: BAG
Enter your guess: bag
Congratulations! BAG is the correct word.
You achieved the correct answer in 3 guesses!
Would you like a rematch [y/n]? n
Thanks for playing! Goodbye!
Explanation / Answer
*
* A program that plays simple word guessing game. In this game the user provides a list of
* words to the program. The program randomly selects one of the words to be guessed from
* this list. The player then guesses letters in an attempt to figure out what the hidden
* word might be. The number of guesses that the user takes are tracked and reported at the
* end of the game.
*
* See the write-up for Project 12 for more details.
*
* @author ENTER YOUR NAME HERE
*
*/
package project12;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Project12 {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a filename containing your wordlist: ");
File file = new File(scanner.next());
Scanner fileScanner = new Scanner(file);
Scanner inScanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
ArrayList<String> historyList = new ArrayList<>();
list = getList(fileScanner);
System.out.println("Read " + list.size() + " words from the file ");
while (true) {
String solution = getRandomWord(list);
String currentGuess = starWord(solution);
char guessChar = ' ';
boolean flag = true;
while (flag) {
System.out.println(" The word to guess is: " + modifyGuess(guessChar, solution, currentGuess));
System.out.println("Previous characters guessed: " + historyList.toString());
guessChar = getCharacterGuess(new Scanner(System.in));
historyList.add(Character.toString(guessChar));
ArrayList<Character> charList = new ArrayList<Character>();
for (char c : solution.toCharArray()) {
charList.add(c);
}
if (checkInList(guessChar, charList)) {
currentGuess = modifyGuess(guessChar, solution, currentGuess);
System.out.println(" The word to guess is now: " + currentGuess);
System.out.print("Enter your guess: ");
if (checkWord(inScanner.next(), solution)) {
currentGuess = solution;
break;
} else {
System.out.println("That is not the correct word.");
}
if (guesssComplete(currentGuess)) {
flag = false;
}
continue;
}
}
System.out.println("Congratulations! " + solution.toUpperCase() + " is the correct word.");
System.out.println("You achieved the correct answer in " + historyList.size() + " guesses!");
System.out.print("Would you like a rematch [y/n]? ");
inScanner = new Scanner(System.in);
if (inScanner.next().equals("y")) {
historyList = new ArrayList<>();
continue;
} else {
break;
}
}
System.out.println(" Thanks for playing! Goodbye!");
}
// Given a Scanner as input, returns a List<String> of strings read from the Scanner.
// The method should read words from the Scanner until there are no more words in the file
// (i.e. inScanner.hasNext() is false). The list of strings should be returned to the calling program.
private static ArrayList<String> getList(Scanner inScanner) {
ArrayList<String> list = new ArrayList<String>();
while (inScanner.hasNext()) {
list.add(inScanner.nextLine().toLowerCase());
}
return list;
}
// Given two strings as input, compares the first string (guess) to the second
// (solution) character by character. If the two strings are not exactly the same,
// return false. Otherwise return true.
private static boolean checkWord(String guess, String solution) {
guess = guess.toLowerCase();
if (guess.equals(solution)) {
return true;
} else {
return false;
}
}
// Given a ArrayList<String> list of strings as input, randomly selects one of the strings
// in the list and returns it to the calling program.
private static String getRandomWord(ArrayList<String> inList) {
Random ran = new Random();
return inList.get(ran.nextInt(inList.size() + 1));
}
// Given a Scanner as input, prompt the user to enter a character. If the character
// enters anything other than a single character provide an error message and ask
// the user to input a single character. Otherwise return the single character to
// the calling program.
private static char getCharacterGuess(Scanner inScanner) {
while (true) {
System.out.print("Enter a character to guess: ");
String input = inScanner.next();
input = input.toLowerCase();
if (input.length() == 1) {
return input.charAt(0);
} else {
System.out.println("Error. Enter a single character");
}
}
}
// Given a character inChar and a ArrayList<Character> list of characters, check to see if the
// character inChar is in the list. If it is, return true. Otherwise, return false.
private static boolean checkInList(char inChar, ArrayList<Character> inList) {
for (int i = 0; i < inList.size(); i++) {
if (inList.get(i).equals(inChar)) {
return true;
}
}
return false;
}
// Given a String, return a String that is the exact same length but consists of
// nothing but '*' characters. For example, given the String DOG as input, return
// the string ***
private static String starWord(String inWord) {
StringBuilder outputBuffer = new StringBuilder(inWord.length());
for (int i = 0; i < inWord.length(); i++) {
outputBuffer.append("*");
}
return outputBuffer.toString();
}
// Given a character and a String, return the count of the number of times the
// character occurs in that String.
private static int checkChar(char guessChar, String guessWord) {
int count = 0;
for (int i = 0; i < guessWord.length(); i++) {
if (guessWord.charAt(i) == guessChar) {
count++;
}
}
return count;
}
// Given a character, a String containing a word, and a String containing a 'guess'
// for that word, return a new String that is a modified version of the 'guess'
// string where characters equal to the character inChar are uncovered.
// For example, given the following call:
// modfiyGuess('G',"GEOLOGY", "**O*O*Y")
// This functions should return the String "G*O*OGY".
private static String modifyGuess(char inChar, String word, String currentGuess) {
StringBuilder newWord = new StringBuilder(currentGuess);
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == inChar) {
newWord.setCharAt(i, inChar);
}
}
return newWord.toString();
}
private static boolean guesssComplete(String word) {
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == '*') {
return false;
}
}
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.