This is a function of a game I made. It is a replica of the popular board game Mastermind.
I have replaced 8 colours with the digits 1-8. Repetition is not allowed and the computer generated code also strictly obeys these rules.
I have put conditions to give an error when the user entered code has the digits 0 or 9 in it or is not a 4 digit code.
However, the program does not always give an error to the mistake the user has performed.
For example:
1) User enters 0439.
Program gives error.
2) User enters 412906.
Program is fine with it.
And these are very inconsistent as there is a possibility that the next time you run the program, you won't get the same error for the same input.
Can someone please help me figure out the problem?
Note: Please remove the quit()
function code in the end as it is related to another class in my program. Just delete that last if block.
I have tried searching it but I couldn't find a question similar to my doubt and none of my friends could help me.
public void master()throws IOException,InterruptedException
{
int i,j,c=0,a,k=0;
String str,ch;
String code[]=new String[4];
System.out.println("\t\t\tWelcome to Mastermind\n");
System.out.println("\nThe computer will generate a 4 digit number consisting of digits from 1 to 8 without repetition and you will have to crack the code in 10 attempts.\n0,9,letters and special characters are not allowed and if entered,the computer will ask you to try again.\nKey description-\n □ means that a digit in the number entered by the user is present in the code but is at the wrong position.\n ■ means that a digit in the number entered by the user is present in the code and is at the correct position.\nIn both the cases,the user will not be told to which digit the key given by the computer is referring to.\nDuplicates are not allowed and are not present in the code generated by the computer.\nIf you try to enter a number with repeating digits,it will not be accepted and you will be told to try again.");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter \"play\" to start");
String play=in.readLine();
if(play.equalsIgnoreCase("play"))
{
code[0]=Integer.toString((int)(Math.random()*7)+1);// generates first no. of code
do
{
code[1]=Integer.toString((int)(Math.random()*7)+1);// generates second no. of code
}while(code[1].equals(code[0]));
do
{
code[2]=Integer.toString((int)(Math.random()*7)+1);// generates third no. of code
}while(code[2].equals(code[1]) || code[2].equals(code[0]));
do
{
code[3]=Integer.toString((int)(Math.random()*7)+1);// generates fourth no. of code
}while(code[3].equals(code[2]) || code[3].equals(code[1]) || code[3].equals(code[0]));
System.out.println("The game begins");
for(a=1;a<=10;a++)
{
System.out.println("Attempt "+a);
str=in.readLine();
if(str.length()!=4)
{
System.out.println("Try again with only digits from 1 to 8 and only a 4-digit number without repetitions");
str=in.readLine();
}
else
{
for(i=0;i<4;i++)
{
c=(int)(str.charAt(i));
if(c>56||c<49) // checks if input is appropriate by comparing ASCII value of the input
{
System.out.println("Try again with only digits from 1 to 8 and only a 4-digit number");
str=in.readLine();
}
else
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i!=j)
{
if(str.charAt(i)==str.charAt(j))
{
System.out.println("Try again with only digits from 1 to 8 and only a 4-digit number");
str=in.readLine();
break;
}
}
}
}
}
}
}
if((code[0]+code[1]+code[2]+code[3]).equals(str))
{
k=1;
break;
}
for(i=0;i<4;i++)
{
ch=Character.toString(str.charAt(i));
for(j=0;j<4;j++)
{
if(code[j].equals(ch))
{
if(i==j)
{
System.out.print("■");
}
else
System.out.print("□");
}
}
}
System.out.println();
}
if(k!=1)
System.out.println("The code is "+code[0]+code[1]+code[2]+code[3]);
else
System.out.println("You did it!!!");
}
System.out.println("Thank You");
if(!play.equalsIgnoreCase("play"))
quit();
}
Aucun commentaire:
Enregistrer un commentaire