Create a simple guessing game, similar to Hangman, in which the user guesses let
ID: 644247 • Letter: C
Question
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 randomly from a list of 10 non displayed phrases 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, letter should be replaced regardless of letter case. Display a congratulagory message when the entire correct phrase has been deduced. Class should be SecretPhrase and program should be in java. If someone could help me that would be great!
The 10 phrases should be:
Java is fun, Life is good, Summer Break, Fast Car, Dangerous, Peace on Earth, Monster, Everyones favorite movie, Love, Twice the fun.
Explanation / Answer
/// SecretPhrase.java
// this is the code for simplest game of word guessing
package TRI;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author anurag
*/
public class SecretPhrase {
//method to replace characters with *
public static StringBuffer makeAstrik(String s)
{
SecretPhrase sp=new SecretPhrase();
StringBuffer dashes = new StringBuffer(s.length());
char c=s.charAt(0);
dashes.append(c);
for (int count=1; count < s.length(); count++)
{
if(count!=sp.getRandom()&& s.charAt(count)!=' ')
{
dashes.append('*');
}
else
{
dashes.append(s.charAt(count));
}
}
return dashes;
}
// method to get random number to display a random phrase
public int getRandom()
{
Random r=new Random(); // object of Random class
return(r.nextInt(10));// random number smaller than 10
}
public static void main(String[] args)throws IOException {
String []str=new String[10];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // to read input
SecretPhrase sp=new SecretPhrase(); // object of class
str[0]="Java is fun";
str[1]="Life is good";
str[2]="Summer Break";
str[3]="Fast Car";
str[4]="Dangerous";
str[5]="Peace on Earth";
str[6]="Monster";
str[7]="Everyones favorite movie";
str[8]="Love";
str[9]="Twice the fun";
//System.out.println("Want to Play the Game:y/n?");
//char wantToPlay=(char)br.read();
char wantToPlay='y';// choice of player
while(wantToPlay=='y'||wantToPlay=='Y')
{
System.out.println("Lets Play the Game:");
int r=sp.getRandom(); // geting random number
String Phrase=str[r]; // this will contain random phrase
StringBuffer Astrik=makeAstrik(Phrase); // contain phrase with random astriks
System.out.println("Guess Missing Alphabets one by one ");
boolean done=false; // to stop the loop
char guess;//
while(!done)
{
System.out.println("Here is your phrase: ");
System.out.println(Astrik); // printing Phrase with astriks *
System.out.println("Guess Missing Alphabet ");
guess=(char)br.read();// read guessed character
int index=-1; // index of guessed char in Phrase
for(int i=0;i<Phrase.length();i++)
{
if((Phrase.charAt(i)==guess||Phrase.charAt(i)==Character.toUpperCase(guess))&&Astrik.charAt(i)=='*')
// matching guess with phrase characters to find its index in Phrase
{
index=i;
break; // if index found then break the loop
}
}
if(index<0) // if index not found
{
System.out.println("Bad guess, Try again:");
}
else // replace * with guessed character
{
Astrik.setCharAt(index, guess);
}
done=true; // set done to true
// if the Phrase and Astrik strings are equal??
for(int i=0;i<Phrase.length();i++)
{
if(Phrase.charAt(i)!=Astrik.charAt(i))
{
done=false; // if not equal then done=false
break;
}
}
if(done)
{
System.out.println("Congartulation You win!!!!");
System.out.println("The phrase is: "+Phrase);
}
br.read();
}
System.out.println("Want to Play the Game:y/n?");
wantToPlay=(char)br.read();
br.read();
}
}
}
////////////////////////////////////// output///////////////////////
run:
Lets Play the Game:
Guess Missing Alphabets one by one
Here is your phrase:
E******** ******** *****
Guess Missing Alphabet
e
Here is your phrase:
E*e****** ******** *****
Guess Missing Alphabet
v
Here is your phrase:
Eve****** ******** *****
Guess Missing Alphabet
y
Here is your phrase:
Eve*y**** ******** *****
Guess Missing Alphabet
o
Here is your phrase:
Eve*yo*** ******** *****
Guess Missing Alphabet
o
Here is your phrase:
Eve*yo*** ***o**** *****
Guess Missing Alphabet
n
Here is your phrase:
Eve*yon** ***o**** *****
Guess Missing Alphabet
e
Here is your phrase:
Eve*yone* ***o**** *****
Guess Missing Alphabet
f
Here is your phrase:
Eve*yone* f**o**** *****
Guess Missing Alphabet
m
Here is your phrase:
Eve*yone* f**o**** m****
Guess Missing Alphabet
o
Here is your phrase:
Eve*yone* f**o**** mo***
Guess Missing Alphabet
v
Here is your phrase:
Eve*yone* f*vo**** mo***
Guess Missing Alphabet
v
Here is your phrase:
Eve*yone* f*vo**** mov**
Guess Missing Alphabet
i
Here is your phrase:
Eve*yone* f*vo*i** mov**
Guess Missing Alphabet
e
Here is your phrase:
Eve*yone* f*vo*i*e mov**
Guess Missing Alphabet
r
Here is your phrase:
Everyone* f*vo*i*e mov**
Guess Missing Alphabet
t
Here is your phrase:
Everyone* f*vo*ite mov**
Guess Missing Alphabet
r
Here is your phrase:
Everyone* f*vorite mov**
Guess Missing Alphabet
a
Here is your phrase:
Everyone* favorite mov**
Guess Missing Alphabet
s
Here is your phrase:
Everyones favorite mov**
Guess Missing Alphabet
i
Here is your phrase:
Everyones favorite movi*
Guess Missing Alphabet
e
Congartulation You win!!!!
The phrase is: Everyones favorite movie
Want to Play the Game:y/n?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.