I'm currently coding the game Mastermind, the code-guessing game, and want to add in a feature that allows the user to undo their most recent guess. I ask the user whether they would like to undo their most recent guess (which requires user feedback) after a bunch of other commands and functions (as can be seen in the code screenshot).
For some reason, the question (AT BOTTOM: "Undo most recent guess? 1 for yes / 0 for no") is asked before all of the code above it. After you answer the question, only then will all the code above it run.
It seems to completely disrupt the sequential nature of code. I've tried re-writing the code multiple times, but I can't seem to catch a break. Anyone have any ideas?
I've re-written the method that undoes their previous guess, and tried to position it in different places in the code just to check whether it really does take priority over everything else, and lo and behold, it does.
userInput();
for(int i = 0; i < 4; i++) {
pins[i][v].setColour(currentGuess[i]);
pins[i][v].changeColour();
}
if(guessChecker == 0) {
push1(currentGuess);
currentNode.getColours();
}
else if(guessChecker > 0) {
push2(currentGuess, currentNode);
currentNode.getColours();
}
else {
System.out.println("error - guessChecker less than 1");
}
int check = checkGuess(currentGuess, goalCode);
if(check == 0) {
giveHints(currentGuess, goalCode);
}
else if (check == 1) {
System.out.println("You got it!");
Greenfoot.stop();
}
System.out.println("Undo most recent guess? 1 for yes / 0 for no");
int ans = scanner.nextInt();
undoGuess(ans);
public void userInput() {
for(int i = 0; i < 4; i++) {
System.out.println("Enter colour: ");
String cInput = scanner.nextLine();
int a = 0;
a = inputValidity(cInput, a);
if(a > 0) {
boolean bool = true;
while(bool) {
a = 0;
System.out.println("Invalid entry. Re-enter this guess: ");
System.out.println("Enter colour: ");
cInput = scanner.nextLine();
a = inputValidity(cInput, a);
if(a == 0) {
currentGuess[i] = cInput;
bool = false;
}
}
}
else {
currentGuess[i] = cInput;
}
}
}
I haven't included the inputValidity method as it is simply a method to check whether the input given by the user is a valid entry or not: no read-ins or any other command of that sort is executed within it, and I am sure it isn't a part of the issue I'm struggling with at the moment.
At this point, any opinions or takes are wanted. Just can't see what I'm doing wrong. If anything needs to be explained further / any new code included, please let me know, this is my first time posing a question on the site :)