Member Avatar for TheQuad

Hi I'm making the mastermind game but using numbers instead of colours. The one part that I'm stuck on is when the right number is guessed but in the wrong position, it seems to count it too many times. for example;
generated number: 4234
guess: 2122
real answer should be correct = 0, "wrong" = 1 but i get "wrong" equal to 3!
Am thinking I need some booleans in there or my comparing of array technique is a massive fail.
Here is my code;

package mastermind;

import java.util.Random;
import javax.swing.JOptionPane;

public class MastermindFrame extends javax.swing.JFrame {

    Random randomNumberGenerator = new Random();
    private int numberOfGuesses;
    private int maxGuesses = 10;
    int random[] = new int[4];
    int correctNumbers = 0;
    int wrongPosition = 0;

    /** Creates new form MastermindFrame */
    public MastermindFrame() {
        initComponents();
        setTitle("Mastermind Game");
    }

   
    private void newGameButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        objectOptions();
        firstNumberTextField.setText("");
        secondNumberTextField.setText("");
        thirdNumberTextField.setText("");
        fourthNumberTextField.setText("");
        clientTextArea.setText("");
        scoreTextField.setText("");
        generateSequence();
        numberOfGuesses = 0;
        guessesLabel.setText(numberOfGuesses + " of " + maxGuesses);
}                                             

    private void guessButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        numberOfGuesses++;
        guessesLabel.setText(numberOfGuesses + " of " + maxGuesses);
        if (numberOfGuesses >= maxGuesses) {
            guessButton.setEnabled(false);
            clientTextArea.append("You have failed to guess the sequence!" + "\n" + "Please start a new game!");
        }
        compareGuessNumbers();
}                                           

private void generateSequence() {
        int level = Integer.parseInt(difficultyTextField.getText()); // gets difficulty from text field after being selected from popup box
                                                                     // converts string to integer
        for (int i = 0; i < random.length; i++) {

            random[i] = randomNumberGenerator.nextInt(level) + 1; // creates random number based on difficulty selected
            cheatLabel.setText(random[0] + "" + random[1] + "" + random[2] + "" + random[3]); // makes the generated number viewable on the cheat tick box
            cheatLabel.setVisible(false); // is used so the user cannot see the random number to begin with

        }
    }

    private void compareGuessNumbers() {
        int level = Integer.parseInt(difficultyTextField.getText());
        correctNumbers = 0;
        wrongPosition = 0;
        int[] guess = new int[4];
        guess[0] = Integer.parseInt(firstNumberTextField.getText());
        guess[1] = Integer.parseInt(secondNumberTextField.getText());
        guess[2] = Integer.parseInt(thirdNumberTextField.getText());
        guess[3] = Integer.parseInt(fourthNumberTextField.getText());


   for (int i = 0, j = 0; i < guess.length; i++, j++){
        if (guess[i] == random[i]) {
            correctNumbers++;
        } else if ((guess[0] == random[1]) || (guess[0] == random[2]) || (guess[0] == random[3])) {
            wrongPosition++;
        } else if ((guess[1] == random[0]) || (guess[1] == random[2]) || (guess[1] == random[3])) {
            wrongPosition++;
        } else if ((guess[2] == random[1]) || (guess[2] == random[0]) || (guess[2] == random[3])) {
            wrongPosition++;
        } else if ((guess[3] == random[1]) || (guess[3] == random[2]) || (guess[3] == random[0])) {
            wrongPosition++;
        }
        }

any help is appreciated

Your application needs lot of cleaning, also you provided uncompleted code so nobody can try to run it and see where is the problem.

However from what you provided you better try to re-think this for loop for example

for (int i = 0, j = 0; i < guess.length; i++, j++){
        if (guess[i] == random[i]) {
            correctNumbers++;
        } else if ((guess[0] == random[1]) || (guess[0] == random[2]) || (guess[0] == random[3])) {
            wrongPosition++;
        } else if ((guess[1] == random[0]) || (guess[1] == random[2]) || (guess[1] == random[3])) {
            wrongPosition++;
        } else if ((guess[2] == random[1]) || (guess[2] == random[0]) || (guess[2] == random[3])) {
            wrongPosition++;
        } else if ((guess[3] == random[1]) || (guess[3] == random[2]) || (guess[3] == random[0])) {
            wrongPosition++;
        }
        }

Why did you initial initialized "j" if you are not using it. Can you explain what are your rules for comparing guess and random arrays?

Am thinking I need some booleans in there or my comparing of array technique is a massive fail.

Definitely not a massive fail - just call it a Beta version!
I think you're right about needing some booleans. Once you have "used" a number in Random for a match you need to flag it as "used" so you don't use it again for a different match. You could do that with a same-size array of booleans. The same requirement applies for the numbers in the guess, but you can handle that simply by continuing directly to the next pass of the loop as soon as the guess number matches anything.
You may need to break your loop in two so you check (and flag as "used") all the correct numbers before you start on the wrongPositions.

Dan Quadrozzi,
Do you think i dont monitor java forums? this could/will be classed as plagiarism if i find any of these suggestions in yours or any of your collegues code. you may want to warn other class mates of this.

regards

martin

Dan Quadrozzi,
Do you think i dont monitor java forums? this could/will be classed as plagiarism if i find any of these suggestions in yours or any of your collegues code. you may want to warn other class mates of this.

regards

martin

Being a teacher I would expect that you will know how to use capital letters in sentences. Besides this is not plagiarism since he did not come here asking "give code for this assignment" he came here with coding problem where he was failing to put in use compare logic.
If you are really this person teacher then you should be proud that he is working on his assignment. TheQuad came here with minor issue of logic that in real world often needs another set of eyes to spot where the problem is.
As you can see TheQuad did not received any coding help, just few written suggestions for which no school would punish (would you punish student because he was explained by his parent that his homework should be approached differently?). After initial posting he did not returned back. At last who is he to give message from teacher that can be considered prank or false. If you are teacher then tell them all in front of class that using forums is not acceptable and point out which rule they are apparently breaking if nay.

Be a part of the DaniWeb community

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