nvco
May 18, 2020, 10:24am
1
Dear Users,
At the moment I´m coding an "scissor stone paper" game.
int player1;
int player2;
char player1play;
char player2play;
void setup()
{
randomSeed(analogRead(A1));
Serial.begin(9600);
player1=random(1,3);
if (player1=1) const char player1play[]="Stein";
if (player1=2) const char player1play[]="Schere";
if (player1=3) const char player1play[]="Papier";
Serial.print(player1play[]); //error 1
player2=random(1,3);
if (player2=1) const char player2play[]="Stein";
if (player2=2) const char player2play[]="Schere";
if (player2=3) const char player2play[]="Papier";
Serial.println(player2play[]); //error 2
delay(200);
if (player1=player2) {
Serial.println("Ergebniss: Unentschieden!");
} else if (player1=1,player2=2){
Serial.println("Ergebniss: Player1 hat gewonnen!");
} else if (player1=1,player2=3){
Serial.println("Ergebniss: Player2 hat gewonnen!");
} else if (player1=2,player2=1){
Serial.println("Ergebniss: Player2 hat gewonnen!");
} else if (player1=2,player2=3){
Serial.println("Ergebniss: Player1 hat gewonnen!");
}
}
/* 1: Stein 2:Schere 3:Papier */
void loop(){
}
I´m still a beginner and not finished with this game but I get a error message and I don´t know how to fix this.
Error Message:
In function 'void setup()':
17:30: error: expected primary-expression before ']' token
25:30: error: expected primary-expression before ']' token
Hope someone can help me.
Best regards,
Nvco
(PS: I´m from germany, so my english isn´t the best)
if (player2 = 1) const char player2play[] = "Stein";
if (player2 = 2) const char player2play[] = "Schere";
if (player2 = 3) const char player2play[] = "Papier";
That is not how you do comparisons in C/C++
= is used to assign values
== is used to compare values
There may be other problems but I stopped looking when I found these
gcjr
May 18, 2020, 10:34am
3
don't need "[]", just player1play
Serial.print(player1play[]); //error 1
isn't there an error in the following line? should the "," be an " && "?
else if (player1=1,player2=2){
not an error, but probably not what you want. should "=" be " ==". in other lines too
if (player1=player2) {
nvco
May 18, 2020, 10:36am
4
UKHeliBob:
if (player2 = 1) const char player2play[] = "Stein";
if (player2 = 2) const char player2play[] = "Schere";
if (player2 = 3) const char player2play[] = "Papier";
That is not how you do comparisons in C/C++
= is used to assign values
== is used to compare values
There may be other problems but I stopped looking when I found these
Thanks for you quick answer. I fixed it.
But I still get the error message.
I fixed it.
Then please post your revised version
Do not be tempted to update your original post with the revised sketch
nvco
May 18, 2020, 10:41am
6
isn't there an error in the following line? should the "," be an " && "?
else if (player1=1,player2=2){
I just get another error message if I remove the "," and add a "&&"
gcjr:
don't need "[]", just player1play
Serial.print(player1play[]); //error 1
Thanks, the error message is away, but I still don´t get an output from player1play & player2play.
nvco
May 18, 2020, 10:47am
7
UKHeliBob:
Then please post your revised version
Do not be tempted to update your original post with the revised sketch
int player1;
int player2;
char player1play;
char player2play;
void setup()
{
randomSeed(analogRead(A1));
Serial.begin(9600);
player1=random(1,3);
if (player1==1) const char player1play[]="Stein";
if (player1==2) const char player1play[]="Schere";
if (player1==3) const char player1play[]="Papier";
Serial.print(player1play);
player2=random(1,3);
if (player2==1) const char player2play[]="Stein";
if (player2==2) const char player2play[]="Schere";
if (player2==3) const char player2play[]="Papier";
Serial.println(player2play);
delay(200);
if (player1==player2) {
Serial.println("Ergebniss: Unentschieden!");
} else if (player1==1, player2==2){
Serial.println("Ergebniss: Player1 hat gewonnen!");
} else if (player1==1, player2==3){
Serial.println("Ergebniss: Player2 hat gewonnen!");
} else if (player1==2, player2==1){
Serial.println("Ergebniss: Player2 hat gewonnen!");
} else if (player1==2, player2==3){
Serial.println("Ergebniss: Player1 hat gewonnen!");
}
}
/* 1: Stein 2:Schere 3:Papier */
void loop(){
}
You cannot assign cstrings / char arrays like you do - and you do not need to either. Try to look at this:
const char* MODES = {"Rock", "Paper", "Scissors"};
byte player1plays = random(0, 3); //Index from 0..2
Serial.print("Player 1 plays: ");
Serial.println(MODES[player1plays]);
nvco
May 18, 2020, 12:08pm
9
Danois90:
You cannot assign cstrings / char arrays like you do - and you do not need to either. Try to look at this:
const char* MODES = {"Rock", "Paper", "Scissors"};
byte player1plays = random(0, 3); //Index from 0..2
Serial.print("Player 1 plays: ");
Serial.println(MODES[player1plays]);
I just edited the code. But now I have another error message.
3:48: error: scalar object 'MODES' requires one element in initializer
In function 'void setup()':
12:20: error: 'Modes' was not declared in this scope
Here is my currently code:
int player1;
int player2;
const char MODES = {"Rock", "Paper", "Scissors"};
void setup()
{
randomSeed(analogRead(A1));
Serial.begin(9600);
byte player1 = random(1, 4);
Serial.print("Player1: ");
Serial.println(Modes[player1]);
byte player2 = random(1, 4);
Serial.print("Player2: ");
Serial.println(Modes[player2]);
delay(200);
if (player1==player2) {
Serial.println("Ergebniss: Unentschieden!");
} else if (player1==1, player2==2){
Serial.println("Ergebniss: Player1 hat gewonnen!");
} else if (player1==1, player2==3){
Serial.println("Ergebniss: Player2 hat gewonnen!");
} else if (player1==2, player2==1){
Serial.println("Ergebniss: Player2 hat gewonnen!");
} else if (player1==2, player2==3){
Serial.println("Ergebniss: Player1 hat gewonnen!");
}
}
/* 1: Stein 2:Schere 3:Papier */
void loop(){
}
You forgot a "*" and I forgot "[]", here is the correct declaration:
const char* MODES[] = {"Rock", "Paper", "Scissors"};
nvco
May 18, 2020, 12:25pm
11
Danois90:
You forgot a "*" and I forgot "[]", here is the correct declaration:
const char* MODES[] = {"Rock", "Paper", "Scissors"};
I added the "*" and the "[]" but I still get this error message:
In function 'void setup()':
12:20: error: 'Modes' was not declared in this scope
My currently code:
int player1;
int player2;
const char* MODES[] = {"Rock", "Paper", "Scissors"};
void setup()
{
randomSeed(analogRead(A1));
Serial.begin(9600);
byte player1 = random(1, 4);
Serial.print("Player1: ");
Serial.println(Modes[player1]);
byte player2 = random(1, 4);
Serial.print("Player2: ");
Serial.println(Modes[player2]);
delay(200);
if (player1==player2) {
Serial.println("Ergebniss: Unentschieden!");
} else if (player1==1, player2==2){
Serial.println("Ergebniss: Player1 hat gewonnen!");
} else if (player1==1, player2==3){
Serial.println("Ergebniss: Player2 hat gewonnen!");
} else if (player1==2, player2==1){
Serial.println("Ergebniss: Player2 hat gewonnen!");
} else if (player1==2, player2==3){
Serial.println("Ergebniss: Player1 hat gewonnen!");
}
}
/* 1: Stein 2:Schere 3:Papier */
void loop(){
}
"Modes" and "MODES" are not the same, case sensitivity..
EDIT: And "random(1, 4)" is going to cause you problems..
nvco
May 18, 2020, 3:30pm
14
Okay, thanks to everyone who helped! The whole game works for now.