import java.util.Scanner;
class Demo508{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
String[] words={"write","that","hello","program","this"};
while(true){
int num=(int)(Math.random()*5);
int count=0,count1=0;
String word=words[num];
String repeat="";
char[] arr=new char[words[num].length()];
block(word,words[num]);
int t=0;
while(true){
String str=scanner.nextLine();
if(str.equals("")){
block(word,words[num]);
continue;
}
char letter=str.charAt(0);
int temp=judge(word,letter);
if(temp>-1){
word=delete(word,temp);
repeat=repeat+words[num].charAt(temp);
count1++;
}else if(isRepeat(letter,repeat)){
System.out.println(letter+" is already in the word");
count++;
}else{
System.out.println(letter+" is not in the word");
count++;
}
if(word.length()==count1){
break;
}else{
block(word,words[num]);
}
}
if(count>1){
System.out.println("The word is "+words[num]+" . You missed "+count+"times");
}else {
System.out.println("The word is "+words[num]+" . You missed "+count+"time");
}
boolean flag=true;
while(true){
System.out.print("Do you want to guess another word? Enter y or n>");
String onceMore1=scanner.nextLine();
if(onceMore1.equals("")){
continue;
}
char onceMore=onceMore1.charAt(0);
if(onceMore=='n'){
flag=false;
break;
}else if(onceMore=='y'){
break;
}
}
if(!flag){
break;
}
}
}
public static boolean isRepeat(char letter,String repeat){
for(int i=0;i<repeat.length();i++){
if(letter==repeat.charAt(i)){
return true;
}
}
return false;
}
public static int judge(String word,int letter){
for(int i=0;i<word.length();i++){
if(word.charAt(i)==letter){
return i;
}
}
return -1;
}
public static String delete(String word,int temp){
String str="";
for(int i=0;i<word.length();i++){
if(i!=temp){
str+=word.charAt(i);
}else{
str+=" ";
}
}
return str;
}
public static void block(String word1,String word2){
System.out.print("(Guess) Enter a letter in word ");
System.out.print(word1+" "+word2+" ");
for(int i=0,j=0;j<word2.length();i++,j++){
if(word1.charAt(i)!=word2.charAt(j)){
System.out.print(word2.charAt(j));
}else{
System.out.print('*');
}
}
System.out.print('>');
}
public static void block(char[] arr){
System.out.print("(Guess) Enter a letter in word ");
for(int i=0;i<arr.length;i++){
arr[i]='*';
System.out.print('*');
}
System.out.print('>');
}
}