Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project 11 Description For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the

Project 11 Description

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 in the target word that contain that character. 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 must 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.

/* * Project11.java * * 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 11 for more details. * * @author ENTER YOUR NAME HERE * @version ENTER THE DATE HERE * */ package osu.cse1223; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Project11 { public static void main(String[] args) { // Fill in the body } // Given a Scanner as input, returns a ArrayList 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. public static ArrayList getList(Scanner inScanner) { // Fill in the body ArrayList list = new ArrayList(); 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. public static boolean checkWord(String guess, String solution) { // Fill in the body } // Given a ArrayList list of strings as input, randomly selects one of the strings // in the list and returns it to the calling program. public static String getRandomWord(ArrayList inList) { // Fill in the body } // Given a Scanner as input, prompt the user to enter a character. If the user // 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. public static char getCharacterGuess(Scanner inScanner) { // Fill in the body } // Given a character inChar and a ArrayList list of characters, check to see if the // character inChar is in the list. If it is, return true. Otherwise, return false. public static boolean checkInList(char inChar, ArrayList inList) { // Fill in the body } // 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 *** public static String starWord(String inWord) { // Fill in the body } // Given a character and a String, return the count of the number of times the // character occurs in that String. public static int checkChar(char guessChar, String guessWord) { // Fill in the body } // 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: // modifyGuess('G',"GEOLOGY", "**O*O*Y") // This functions should return the String "G*O*OGY". public static String modifyGuess(char inChar, String word, String currentGuess) { // Fill in the body } }

NOTE: For this assignment you will want to be able to generate a random number between 0 and the last element in the list of words provided by the user. 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:

MIGHTY crimes FLIGHT FRIGHT Grimes PLACES TRACES plates Fisher fishes WISHES dishes

Project 11 Sample Output

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. Note that for full credit your output must be able to match the sample transcript's output EXACTLY for the same inputs - including spacing. In this project some of your output will be randomized so you can't replicate the transcript exactly in your tests - pay attention to the spacing for your submission. 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 if the user responds that they would like a rematch, the program should randomly pick a different word from the file and NOT prompt the user to enter another filename. If the user enters a filename that does not exist, your program should gracefully exit with an error message. Enter a filename containing your wordlist: qords.txt I'm sorry, I'm having trouble reading the file qords.txt. Goodbye! If the user enters more than one character as a guess, the program should display an error message and then prompt again. For example: Please guess another letter and try again. The word to guess is: **TE Previous characters guessed: E, T Enter a character to guess: mite Error: Input must be a single character! Enter a character to guess: m The character M occurs in 1 positions. 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 decision not to try to guess this time. And if the user answers the "Would you like a rematch" question with something other than a 'y' or an 'n' (either upper or lowercase), they should receive an error message and a prompt to answer again. 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: You have chosen to pass. 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]? l That is not a valid answer. Please answer [y/n]. Would you like a rematch [y/n]? n Thanks for playing! Goodbye!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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