Description The Ultimate Malicious Decoder (UMD) League wants you to write a tra
ID: 3629262 • Letter: D
Question
DescriptionThe Ultimate Malicious Decoder (UMD) League wants you to write a training program for their trainees. The training program is a password guessing game that generates a password using any combination of the letters 'a', 'b', 'c', 'd', and 'e' (with repeating letters). The trainees will be asked to give the length of the password they want to crack and then they will need to guess the password using a combination of those letters. After each guess, the program will tell the trainee how many letters they got correct but not which letters exactly. Once the trainee finally guesses correctly, the program will print out the number of guesses it took them for that password and ask if they want to play again. The program will repeat until the trainee decides to quit. After the trainee is done, the program prints out the total number of games they played and the average number of guesses per game.
Requirements
Your program is required to have the following parts:
• Prompt the user for the length of the password they want to solve
• Utilize the random number function to have different passwords generated in each game
• Evaluate the guess and print off how many letters were correct, or, guess was completely correct, tell the user how many guesses it took for that game for the user
• Prompt the user to play again after finding the correct password
• When user quits, print out total number of games and average number of guesses
Explanation / Answer
please rate - thanks
message me if any changes needed
note testing comment
import java.util.*;
public class mastermind{
public static void main(String[] args)
{int size;
Scanner in=new Scanner(System.in);
int i, guesses,games=0,totalguesses=0;
int number;
char c, max, yesorno;
double average;
Random r=new Random();
max='d';
do
{System.out.print("How long a password would you like? ");
size=in.nextInt();
char[] guess=new char[size];
char[] answer=new char[size];
int [] correct=new int[2];
guesses=0;
games++;
System.out.println(" Starting game... ");
generate_answer(answer,max,r,size);
for(i=0;i<size;i++) //for
System.out.print(answer[i]); //testing
System.out.println(); //only
do
{
for(i=0;i<size;i++)
{do
{
System.out.print("Enter char "+(i+1)+": ");
guess[i]=in.next().charAt(0);
if(guess[i]>max)
System.out.println("Char must be between a and "+max);
}while(guess[i]>max);
}
System.out.print("Your guess: ");
guesses++;
for(i=0;i<size;i++)
System.out.print(guess[i]);
System.out.println();
compare(guess,answer,size,correct);
System.out.println(correct[0]+" Exact and "+correct[1]+" Inexact ");
}while(correct[0]!=size);
totalguesses+=guesses;
System.out.println("Pattern found in "+guesses+" attempts. ") ;
System.out.println("Another game [Y/N]?");
yesorno=in.next().charAt(0);
}while(Character.toUpperCase(yesorno)=='Y');
average=(double)totalguesses/games;
System.out.println("Games played: "+games);
System.out.println("average guesses per game: "+average) ;
}
public static void generate_answer(char answer[], char max,Random r,int size)
{int i;
int n;
for(i=0;i<size;i++)
{answer[i]=(char)(r.nextInt(max-96)+97);
}
}
public static void compare(char guess[],char answer[],int size,int correct[] )
{int i,j,exact=0,inexact=0;
boolean [] found=new boolean[size];
boolean []used=new boolean[size];
for(i=0;i<size;i++)
{found[i]=false;
used[i]=false;
}
exact=0;
for(i=0;i<size;i++)
if(guess[i]==answer[i])
{exact++;
found[i]=true;
used[i]=true;
}
if(exact==size)
{correct[0]=exact;
correct[1]=inexact;
}
for(i=0;i<size;i++)
for(j=0;j<size;j++)
if(i!=j)
if(!found[j]&&!used[i])
if(guess[i]==answer[j])
{inexact++;
found[j]=true;
used[i]=true;
}
correct[0]=exact;
correct[1]=inexact;
}
}