Hi everybody, I'm fairly new to GUI and even newer to Java and I was wondering if someone can help me find my problem and how to fix it. The problem is I'm doing the game mastermind and I'm checking the guess but everytime I click the button a second time the black and white part(where it says if it's correct numbers and correct placement and vice versa) it goes up, but it works the first time. For example the correct numbers are 1234 and they entered 1254 the first time I press the button to check, it gives me the correct number of black and white but the second time I press it, it just adds 1-3 to black eg.was 3 correct numbers and the next time I pressed with a different guess it just went to 5black instead of changing according to the guess And I don't know how to correct this problem. Here's the code not all of it, just the part that I have the problem with. Any help would be appreciated. Sorry if the problem is obvious or that I have done something completely wrong. Thanks again!
if(event.getSource()==oneGuessButton)
{
if(guessI.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Please do not leave this field blank","Error", JOptionPane.ERROR_MESSAGE);
}
else
{
unGuess = Integer.parseInt(guessI.getText());
MyCheck();
SoAnnoyed();
LengthCheck();//if there are numbers in the field the appropriate methods are called on
//after LengthCheck has finished the next methods are called on to respectively check the sequence of numbers
numberCheck();
Congrats();
yourGuess.setText("Your guess was " + unGuess + " ---> Result is: black = " + black + " white = " + white);
// MyCheck();
guessI.setText("");
}
}
}
private void SoAnnoyed()
{
int secret=1234;
char[] ug = Integer.toString(unGuess).toCharArray();
char[] sc= Integer.toString(secret).toCharArray();
boolean[] used = new boolean[ug.length];
for (int i = 0; i < ug.length; i++){
used[i] = false;
}
for (int i = 0; i < ug.length; i++){
if (ug[i] == sc[i]){
black++;
used[i]=true;
}
}
if (black < sc.length){
for (int i = 0; i < ug.length; i++){
for (int j = 0; j < sc.length; j++){
if (!used[i]){
if (ug[i] == sc[j]){
white++;
used[i] = true;
}
}
}
}
}
}
private int[] checkEnteredNumber(int unGuess, int secret) {
int black = 0; // the number of well placed numbers
int white = 0; // the number of misplaced numbers
char[] ug = Integer.toString(unGuess).toCharArray(); // conversion into an array of characters
char[] sc = Integer.toString(secret).toCharArray(); //
// create and intialize an array of boolean with the same length than ug
boolean[] used = new boolean[ug.length];
for (int i = 0; i < ug.length; i++) {
used[i] = false;
}
// count the well placed numbers
for (int i = 0; i < ug.length; i++) {
if (ug[i] == sc[i]) {
black++;
used[i] = true;
}
}
// count the misplaced remaining numbers
if (black < sc.length) {
for (int i = 0; i < ug.length; i++) {
for (int j = 0; j < sc.length; j++) {
if (!used[i]) {
if (ug[i] == sc[j]) {
white++;
used[i] = true;
}
}
}
}
}
int[] response = new int[2]; // initialize the response
response[0] = black;
response[1] = white;
return response;
}
private void MyCheck()
{
int secret = 1234;
int enteredNumber = Integer.parseInt(guessI.getText());
int[] result = checkEnteredNumber(enteredNumber,secret); // result will contain response from the method
if (result[0] == 1234) { // check if the user has guessed the secret
JOptionPane.showMessageDialog(null, "Congratulations you have guessed the correct numbers ","Congratulations!", JOptionPane.ERROR_MESSAGE);
} else {
answerReal.setText(""+secret+"");
}
}
}