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