For this lab you will write a Java program that plays a simple Guess The Word ga
ID: 3547671 • 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.
Explanation / Answer
import java.util.Scanner; /** * Plays a word guessing game with one player. */ public class WordGuess { public static void main(String[] args) { final String SECRET_WORD = "BRAIN"; final String FLAG = "!"; String wordSoFar = "", updatedWord = ""; String letterGuess, wordGuess = ""; int numGuesses = 0; Scanner input = new Scanner(System.in); /* begin game */ System.out.println("WordGuess game. "); for (int i = 0; i = 0) { updatedWord = wordSoFar.substring(0, SECRET_WORD.indexOf(letterGuess)); updatedWord += letterGuess; updatedWord += wordSoFar.substring(SECRET_WORD.indexOf(letterGuess)+1, wordSoFar.length()); wordSoFar = updatedWord; } /* display guessed letter instead of dash */ System.out.println(wordSoFar + " "); } while (!letterGuess.equals(FLAG) && !wordSoFar.equals(SECRET_WORD)); /* finish game and display message and number of guesses */ if (letterGuess.equals(FLAG)) { System.out.println("What is your guess? "); wordGuess = input.nextLine(); wordGuess = wordGuess.toUpperCase(); } if (wordGuess.equals(SECRET_WORD) || wordSoFar.equals(SECRET_WORD)) { System.out.println("You won!"); } else { System.out.println("Sorry. You lose."); } System.out.println("The secret word is " + SECRET_WORD); System.out.println("You made " + numGuesses + " guesses."); } } //end of programRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.