In: Computer Science
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.
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