I've been working on a rock paper scissors project and I am very confused about a bug: "break statement not within loop or switch"
Here's the code:
void loop(){
if ((digitalRead(buttonRock) == LOW) && (digitalRead(buttonPaper) == LOW) && (digitalRead(buttonScissor) == LOW)) {
if (digitalRead(buttonRock) == LOW){
myMove = "Rock";
}
if (digitalRead(buttonPaper) == LOW){
myMove = "Paper";
}
if (digitalRead(buttonScissor) == LOW){
myMove = "Scissor";
}
int rand_choice = (int)(random() * 3);
if (rand_choice == 0) {
opponentMove = "Rock";
digitalWrite(opponentRock, HIGH);
}
if (rand_choice == 1) {
opponentMove = "Paper";
digitalWrite(opponentPaper, HIGH);
}
if (rand_choice == 2) {
opponentMove = "Scissor";
digitalWrite(opponentScissor, HIGH);
}
if (myMove == opponentMove){
digitalWrite(tie, HIGH);
break;
}
}
}
I know this is long but if you have any help, that would be great. TIA
Simple Google search found this:
[
break - Arduino Reference
https://www.arduino.cc › structure › control-structure
](break - Arduino Reference )
Description. break is used to exit from a for , while or do…while loop, bypassing the normal loop condition. It is also used to exit from a switch case statement.
Paul
Koepel
May 29, 2021, 11:53pm
3
Welcome to this forum. Could you edit your post and put a complete sketch between lines with three backslash-single-quotes.
```
Your sketch
```
Seems self-explanatory, this section of your improperly-posted code is neither a loop nor a switch construct:
if (myMove == opponentMove){
digitalWrite(tie, HIGH);
break;
}
i wanted to break from the void loop(){}
Then return is what you need.
i just dont know how to make it only break from void loop when myMove == opponentMove. im used to coding on python, so this is just weird for me.
nvm i just found a work around
How about posting the working sketch with the solution?
I just removed the break entirely because I realized the game worked better without it.
system
Closed
September 27, 2021, 6:00pm
11
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.