Click here to Skip to main content
15,075,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a Mastermind program in Java. So the user has to enter the number of pegs and colors and a code will randomly be generated and the program will do its thing and tell them if they got the code or not. I have a bunch of IF statements and I tried putting it into a for loop so I don't have to create a if statement when the user increases the amt of pegs but that does not work. Any suggestion? Here is the code:
Java
int [] guesses = new int[10];
for(int i = 0; i<peg.length;i++) {
    peg[i]=generator.nextInt(amtpegs)+1;
    System.out.println(peg[i]);
}
      //Repeat till the player wins
      while(true){
        //Get input from user
          for(int x = 0; x<amtpegs;x++)
            {
              guesses[x]=reader.readInt("Enter your numbers: ");
            }
        //Check if peg in correct index
       for(int y = 0; y<amtpegs;y++) {
        if (peg[y]==guesses[y]){
          pegs++;
       }
        //Check if the color is correct // I want to REPLACE THIS WITH THE FOR LOOP
        if (guesses[0]==peg[0] || guesses[0]==peg[1] || guesses[0]==peg[2]){
          color++;
        }
        if (guesses[1]==peg[1] || guesses[1]==peg[0] || guesses[1]==peg[2]){
          color++;
        }
        if (guesses[2]==peg[0] || guesses[2]==peg[1] || guesses[2]==peg[2]){
          color++;
        }
        if (guesses[3]==peg[3] || guesses[3]==peg[1] || guesses[3]==peg[2]){
              color++;
        }
        System.out.println("You have "+pegs+" correct peg(s) and "+color+" correct colors");
        //Exit if player wins
        if (color==amtcolors && pegs==amtpegs){
          System.out.println("You have broken the code in "+guess+" guesses");
          break;
        }
        //Increment guess count
        guess++;
        //Reset the color and peg value
        color=0;
        pegs=0;
      }


i tried making the if or statements into a for loop by replacing the indexes with the x and j in the for loop, but the problem is the variable color increases too much. Ex: if the user enter 10 pegs and 9 colors, the output should be the same but here it says in the output 10 pegs and 20+ colors sometimes:

Any Questions? I have been trying to solve this for the past day.

java

What I have tried:

Java
for(int x = 0; x<guesses.length;x++) { 
// System.out.println(x+"n");
                 for(int j = 0; j<peg.length-1; j++){
                  //   System.out.println(j+"N");
                   if (guesses[x]==peg[j]){
                     color++;
                   }
                   }
                 }
Posted
Updated 24-Oct-20 13:53pm
v3

Quote:
i tried making the if or statements into a for loop by replacing the indexes with the x and j in the for loop, but the problem is the variable color increases too much.

Try something like:
Java
for(int x = 0; x<guesses.length;x++) {
	// System.out.println(x+"n");
	for(int j = 0; j<peg.length-1; j++){
		//   System.out.println(j+"N");
		if (guesses[x]==peg[j]){
			color++;
			break; // This break out of inner loop once there is a match
		}
	}
}
   
v3
Stop and think how you do it yourself when playing the game.
The player inputs a set of coloured pegs and you compare them against the "solution set" in two ways.

First you look for exact matches: a peg of the right colour in the right place, and you mentally replace each of them with a black peg, removing them from any further consideration:
Solution:     R G B Y Y G
Guess:        P G Y Y G P
Black:        x B x B x x
Sol remain:   R B Y G
Guess remain: P Y G P

That's easy - just check colours in matching indexes!

Then you check what is left to find "white" matches: Right colour, wrong place:
Sol remain:   R B Y G
Guess remain: P Y G P
White:        x W W x

The simplest way to do that is to loop through each of the solution left, and look for a matching colour in what is left of the guess. If you find it, count a White and remove the colour from the Guess - then skip to the next solution index.

Try that on paper, and you'll see what I mean.
Get that all sorted in your head and it's pretty easy to code, and it works for any number of colours, and any number of pegs!
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900