SpotReview 0 Newbie Poster

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:

public void program(int [] peg, int amtpegs,int amtcolors) {
    Random generator = new Random();
    KeyboardReader reader = new KeyboardReader();
    int color = 0, guess = 1,pegs = 0;
    int [] guesses = new int[10];
    for(int i = 0; i<peg.length;i++) {

          //Repeat till the player wins
            //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]){

            //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]){
            if (guesses[1]==peg[1] || guesses[1]==peg[0] || guesses[1]==peg[2]){
            if (guesses[2]==peg[0] || guesses[2]==peg[1] || guesses[2]==peg[2]){
            if (guesses[3]==peg[3] || guesses[3]==peg[1] || guesses[3]==peg[2]){

            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");
            //Increment guess count
            //Reset the color and peg value


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:

  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]){



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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.