Question

In: Computer Science

In JAVA Create a simple guessing game, similar to Hangman, in which the user guesses letters...

In JAVA

Create a simple guessing game, similar to Hangman, in which the user guesses letters and then attempts to guess a partially hidden phrase. Display a phrase in which some of the letters are replaced by asterisks: for example, G* T*** (for Go Team). Each time the user guesses a letter, either place the letter in the correct spot (or spots) in the phrase and display it again or tell the user the guessed letter is not in the phrase. Display a congratulatory message when the entire correct phrase has been deduced. Add to this the ability for the user to guess the entire phrase. Save the game as SecretPhrase.java.

Add to this an array of 10 phrases to choose from. Generate a random number to use to select one of the phrases from your array.

Also, add a second array to the the alphabet. As the user guesses a letter mark this letter in the alphabet array as used. When the user attempts to use a letter that was already used, tell them that letter was already user to please guess another letter.

Make the game loop so after a round the user can choose to play again and a different phrase will be chosen.

Solutions

Expert Solution

Below is the JAVA code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

import java.util.*;
class Game
{
   private String [] words = //choose secret word from these
   {
   "geography", "cat", "yesterday", "java", "truck", "opportunity",
       "fish", "token", "transportation", "bottom"
};

   private String secretWord; // the chosen secret word
   private ArrayList<Character> guessedLetters; // correct guesses

   public Game()
   {
       //Randomly choose a word from list of words
       Random randIndex = new Random();
       int index = randIndex.nextInt(words.length);
       this.secretWord = words[index];
       guessedLetters = new ArrayList<Character>();
   }
public ArrayList<Character>getGuessedLetters()
{
return guessedLetters;
}
public boolean displayWord()
{
boolean flag = false;
for(int i=0;i<secretWord.length();i++)
{
char ch = secretWord.charAt(i);
if(guessedLetters.contains(ch)) //check whether current character is guessed or not
System.out.print(ch);
else
{
flag = true;
System.out.print("*");
}
}
System.out.println();
return flag;
}
}
public class SecretPhrase
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
boolean play = true;
while(play)
{
Game game = new Game();
boolean guessed = false;
char guessedLetter;
while(!guessed)
{
boolean flag = game.displayWord();
if(!flag) //word is guessed correctly
{
guessed = true;
continue;
}
System.out.print("Make a guess: ");
guessedLetter = sc.next().charAt(0);
if(game.getGuessedLetters().contains(guessedLetter)) //check whether user input character is guessed or not
System.out.println("You have already entered this letter");
else
{
game.getGuessedLetters().add(guessedLetter);
}
}
System.out.println("Wanna play again? (Y)es/(N)o");
char choice = sc.next().charAt(0);
if(choice == 'N') //don't play more
play = false;
}
   }
}

Below is the screenshot of output

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any


Related Solutions

in java Create simple number guessing game as follows. First, prompt the user to enter a...
in java Create simple number guessing game as follows. First, prompt the user to enter a min and max value. In your code, declare and assign a variable with a random integer in this range (inclusive). Then, ask the user to guess the number. Input the user's guess. And at the end output "Correct!" if the user's guess was right on spot, or "Sorry, the answer is not corrcet.” The number I was looking for was xxx. Replace xxx with...
Please create a Hangman game in Java language. For this game a random word will be...
Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 points possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 points should be taken away from what is left of their total possible points. For...
Java guessing game: For this program you will use Linear Search. - Ask the user to...
Java guessing game: For this program you will use Linear Search. - Ask the user to choose between a number from 0 to 2000. - The user will guess how long the linear search will take in nanoseconds, - After the user puts in their answer, show the user the difference between the actual answer and their answer. (Example: The user puts in 5 nanoseconds. The actual time is 11 nanoseconds. The program will show 6 nanoseconds.) There should be...
Python: You will modify the given code to create a guessing game that takes user input...
Python: You will modify the given code to create a guessing game that takes user input and allows you to make multiple guesses. There should also be a loop Use the following attached Sample Code as a guide. There are mistakes in the code!!! #Guessing Game import random number=random.randint(1, another number) print("Hello, CIS 101") print("Let's play a guessing Game!called guess my number.") print("The rules are: ") print("I think of a number and you'll have to guess it.") guess = random.randint(1,...
(Python code please) Of a Guessing game(1-10) with a limited amount of guesses (5)
(Python code please) Of a Guessing game(1-10) with a limited amount of guesses (5)
guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
Write a program to allow a user to play the game Hangman. DO NOT USE AN...
Write a program to allow a user to play the game Hangman. DO NOT USE AN ARRAY The program will generate a random number (between 1 and 4581) to pick a word from the file - this is the word you then have to guess. Note: if the random number you generate is 42, you need the 42nd word - so loop 41 times picking up the word from the file and not doing anything with it, it is the...
Project 5-3: Guessing Game Create an application that lets a user guess a number between 1...
Project 5-3: Guessing Game Create an application that lets a user guess a number between 1 and 100. Console Welcome to the Guess the Number Game ++++++++++++++++++++++++++++++++++++ I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 You got it in 1 tries. Great work! You are a mathematical wizard. Try again? (y/n): y I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 Way too high! Guess...
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and puts that into g->hidden_word. It sets...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT