void loop() won't loop?

Hi there! I posted about this project a few weeks ago and solved that round of problems, so thank you all for help with that! After tinkering and adding a few more segments to the game project I’m working on, I’ve run into a problem with a while statement towards the end of my code, listed here:

int done = 1;
           while (done > 0){
              okToPrint == true;
            while (okToPrint == true) {
          Serial.println(F("Roll the dice by typing 'y'!"));
          okToPrint = false;
            }
            while(Serial.available()){
                Serial.read();
                }
            while (Serial.available() == 0) {}  
                  char begg = Serial.read();
                  if(begg == 'y'){
            yourRoll = random(1,7);
            if(yourRoll >= diceVal) {
              Serial.print(F("You rolled a "));
              Serial.print(yourRoll);
              Serial.print(F(". You did "));
              Serial.print(atP);
              Serial.println(F(" damage to the illness!"));
              Serial.print(F("It has "));
              Serial.print(defenseVal - atP);
              Serial.println(F(" health left"));
                if ((defenseVal - atP) <= 0) {
                Serial.print(F("Good Job! You've defeated "));
                Serial.print(monster);
                Serial.println(F("! That's the end of this fighting round!"));
                Serial.println("");
                done = 0;
                }
                else if ((defenseVal - atP) != 0){
                Serial.println(F("You did not kill the illness yet."));
                  if ((defP - attackVal) <= 0){
                  Serial.println(F("I'm sorry. You were defeated. You may try again next turn, but for now, your turn is over. Good try."));
                  Serial.println("");
                  done = 0;     
                  }
                  else if ((defP - attackVal) > 0){     
                  Serial.println(F("You didn't defeat it yet, but you still have a chance! Keep going! Remember your health and the illness's health!"));
                  Serial.println("");
                  done = 1;
                  }
                }
            }
            else if(yourRoll < diceVal) {
              Serial.println("");
              Serial.print(F("You rolled a "));
              Serial.print(yourRoll);
              Serial.print(F(". You did not do any damage! You have "));
              Serial.print(defP - attackVal);
              Serial.println(F(" health left!"));
              if ((defenseVal - atP) <= 0) {
                Serial.print(F("Good Job! You've defeated "));
                Serial.print(monster);
                Serial.println(F("! That's the end of this fighting round!"));
                Serial.println("");
                done = 0;
                }
                else if ((defenseVal - atP) != 0){
                Serial.println(F("You did not kill the illness yet."));
                  if ((defP - attackVal) <= 0){
                  Serial.println(F("I'm sorry. You were defeated. You may try again next turn, but for now, your turn is over. Good try."));
                  Serial.println("");
                  done = 0;     
                  }
                  else if ((defP - attackVal) > 0){    
                  Serial.println(F("You didn't defeat it yet, but you still have a chance! Keep going! Remember your health and the illness's health!"));
                  Serial.println("");
                  done = 1;
                  }
                }
            }
            
          }
        }

I’m unsure if the rest of my code is necessary for any aid, but upon request I can definitely post it. (It’s a little long, though, so I figured I’d just post this part for now and see.) I’ve been stumped on this loop, which is supposed to loop the attack of a monster until the monster is dead (it has no more defense points). I’m sure I’m making a stupid mistake somewhere, but I’m pretty new to all this.
Any help at all is appreciated!

*Edit, to clarify, it does go through the process once, but upon the need to loop if neither the user nor the monster are dead, it just stops the code, or gets stuck somewhere

test2.ino (23.5 KB)

I'm unsure if the rest of my code is necessary for any aid, but upon request I can definitely post it. (It's a little long, though, so I figured I'd just post this part for now and see.) I've been stumped on this loop, which is supposed to loop the attack of a monster until the monster is dead (it has no more defense points). I'm sure I'm making a stupid mistake somewhere, but I'm pretty new to all this.
Any help at all is appreciated!

Yep, we sure prefer full codes. At least me, personally. If it's too long to post it in code tags, it's fine to attach it.

Cheers!

linearity64:
Yep, we sure prefer full codes. At least me, personally. If it's too long to post it in code tags, it's fine to attach it.

Cheers!

Should be there now.

Okay, just finished checking your code. It DOES LOOP. It just doesn't print the "Roll the dice by typing 'y'!" message, but you can still type y and it will loop. The reason?

int done = 1;
    while (done > 0) {
      okToPrint == true;             // THIS LINE HERE, use '=' for assignment!
      while (okToPrint == true) {
        Serial.println(F("Roll the dice by typing 'y'!"));
        okToPrint = false;
      }

I also found other problem the rest of the logic. Say defenseVal is 9, atP is 5. Then I rolled a dice and let's say dice roll went through. Your code, takes defenseVal, subtract by atP, and print it. So answer is 4. Next time it loops, it still takes the SAME defenseVal and atP, and you get 4. Over and over and over.

Also, checking defenseVal - atP on missed dice roll is terrible. Say atP is 9, defenseVal is 4. Even if I miss the roll, it checks for defenseVal-atP, which gives -5, and instantly wins me the game.

Lastly, the game does also loop towards the beginning, only when it loops, okToPrint is last set to low, which means no printing first 2 lines.

linearity64:
I also found other problem the rest of the logic. Say defenseVal is 9, atP is 5. Then I rolled a dice and let’s say dice roll went through. Your code, takes defenseVal, subtract by atP, and print it. So answer is 4. Next time it loops, it still takes the SAME defenseVal and atP, and you get 4. Over and over and over.

Also, checking defenseVal - atP on missed dice roll is terrible. Say atP is 9, defenseVal is 4. Even if I miss the roll, it checks for defenseVal-atP, which gives -5, and instantly wins me the game.

Lastly, the game does also loop towards the beginning, only when it loops, okToPrint is last set to low, which means no printing first 2 lines.

Addressing the second logic problem: The idea is that as you progress through the game, you collect attack and defense values while starting low. The monsters get harder, the players get stronger. Obviously after play-testing, we’d work out some kinks with difficulty.

Addressing the rest, thank you. I fixed my logic and it’s working well now.

Last question is if I wanted to make this whole code (everything in loop() ) reset, can I call loop from within loop?

Last post on the subject, I swear!

My final problem in this project is that the code works perfectly… once. The void loop() goes through but doesn’t reset after the code finishes (or maybe it does and there’s a larger issue with the code). It plays the game and that is all that happens. I have to reupload every time I want it to start up at the beginning. Anyone out there know why this is, and maybe even how I can fix it?

Appreciate the help, everyone!

test2.ino (23 KB)

duplicate post

There was no need to start a new topic on this.

you can call loop from within loop but it is a bad idea because each time you call loop from loop more RAM gets reserved for local variables.

If you just add serial-debug-output everywhere in your code you should be able to find the place where your code gets stuck.

You have a strange coding style using while-loops in this way

okToPrint = true;
    while (okToPrint == true) {
      Serial.println(F("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"));
etc. etc, etc
.      okToPrint = false;

which will do always exactly the same deleting all the while-ing and just use

      Serial.println(F("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"));
etc, etc, etc

for all the rest to be really honest: I don't like to go through so many lines of code in a single function using so many while-loops checking each and every loop for correct placed opening and closing curly brackets

I would code this as a set of functions to get an overview again

best regards Stefan

My fault. Figured the questions were different enough to warrant a new post.

Topics merged to avoid duplication of effort