0

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 :)

  • 4
    What is `userInput()`? Please post a minimum, complete and verifiable example. This snippet is not enough for us to run the code and reproduce the reported issue. – Elliott Frisch Aug 11 '19 at 19:14
  • Also, how did you determine that the read-in in executed before the "other" stuff? – Turing85 Aug 11 '19 at 19:16
  • @ElliottFrisch sorry, I'm new to this. userInput() is a method that simply asks the user to input four colours into the console, the inputs corresponding to their first guess at the hidden code. I'll edit the original post to include this method. Do you need to see all the other ones as well (giveHints, checkGuess etc)? – Jacob Kenning Aug 12 '19 at 10:31
  • @Turing85 I determined it because there is meant to be a change of the on-screen graphics that I have, specifically a row of circles are meant to change colours (from black) corresponding to the colours that the user typed in when 'userInput' was run. That's what the for loop is, that is meant to run just after userInput. There should also be other outputs to the console before the question is asked (these outputs are part of the giveHints method), however they only display on the console after the read-in at the bottom is executed. – Jacob Kenning Aug 12 '19 at 10:35
  • Would you mind to show us more code? Currently your excerpt is a mixture of snippets and a method. You could minimize your code as far as possible. Another hint: Put more `System.out.println()` in the code to see when which place is executed. The Greenfoot system doesn't have much magic, it's likely your understanding of Java. – the busybee Aug 31 '19 at 08:33

0 Answers0