There is a \"fun\" children\'s game where one child thinks of a \"common phrase\
ID: 3532162 • Letter: T
Question
There is a "fun" children's game where one child thinks of a "common phrase", then the second child repeatedly makes guesses as to the letters that it contains.
You are to write a Java program that:
1. Prompts a user to enter a "common phrase", and reads it into a variable using Scanner.nextLine() - under the assumption that the phrase consists of nothing but undercase letters and spaces.
2. A counter is set to zero.
3. The following is than repeated until the entire "common phrase" is revealed:
a) The "common phrase" is displayed with all of the letters (that have not yet been correctly guessed) replaced with the ? character and all of the letters that have been correctly guessed displayed as is.
b) Using a user input validation loop, the second user is prompted to enter a single lowercase letter guess.
c) A counter is incremented.
4. Once the "common phrase" is revealed, you must also report the total number of valid guesses that were made to do so.
Hints:
Store the letters that have been correctly guessed in an initially empty ("") String object, using the concatenation operator (+=).
A fair amount of partial credit shall be given for just being able to respond with "correct" / "incorrect" for each second user guess.
Sample run(s):
Please enter the phrase to guess at : who do you love
Common Phrase
-------------
??? ?? ??? ????
Enter a lowercase letter guess : f
Common Phrase
-------------
??? ?? ??? ????
Enter a lowercase letter guess : o
Common Phrase
-------------
??o ?o ?o? ?o??
Enter a lowercase letter guess : s
Common Phrase
-------------
??o ?o ?o? ?o??
Enter a lowercase letter guess : w
Common Phrase
-------------
w?o ?o ?o? ?o??
Enter a lowercase letter guess : h
Common Phrase
-------------
who ?o ?o? ?o??
Enter a lowercase letter guess : d
Common Phrase
-------------
who do ?o? ?o??
Enter a lowercase letter guess : e
Common Phrase
-------------
who do ?o? ?o?e
Enter a lowercase letter guess : y
Common Phrase
-------------
who do yo? ?o?e
Enter a lowercase letter guess : u
Common Phrase
-------------
who do you ?o?e
Enter a lowercase letter guess : l
Common Phrase
-------------
who do you lo?e
Enter a lowercase letter guess : v
Common Phrase
-------------
who do you love
Was guessed in 11 guesses.
Explanation / Answer
I used ArrayLists bc i like the data structure better than that of arrays but you can replace the array list with arrays if you'd like. Also I built a toString method in order to print out the arrays in proper format..
import java.util.ArrayList;
import java.util.Scanner;
public class CommonPhrase {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a common phrase");
String s = sc.nextLine();
ArrayList<String> r = new ArrayList<String>();
ArrayList<String> q = new ArrayList<String>();
ArrayList<String> guessed = new ArrayList<String>();
for(int i = 0; i<s.length(); i++){
r.add(i,s.substring(i,i+1));
if(r.get(i).trim().equals("")){
q.add(i," ");
}
else q.add(i,"?");
}
for(int i = 0; i<10;i++){
System.out.println("");
}
System.out.println(toString(q));
while(q.contains("?") == true){
System.out.println("Guess a letter.");
String g = sc.next();
if(guessed.contains(g)){
System.out.println("Letter has already been guessed");
}
else if(s.contains(g)){
int index = r.indexOf(g);
q.remove(index);
q.add(index,g);
guessed.add(g);
System.out.println("Good Guess!");
System.out.println(toString(q));
}
else {
guessed.add(g);
System.out.println(toString(q));
System.out.println("Incorrect");
}
}
System.out.println("Congratulations, You Won!");
}
public static String toString(ArrayList<String>r){
String s = "";
for(int i = 0; i <r.size(); i++){
s += r.get(i);
}
return s;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.