问题
I am currently stuck with my RPS program in that it will not properly store the number of times the user loses, or ties with the computer. For instance, when I run the program and enter "q" to quit I get the following output:
Enter R, P, S, or Q (for quit)
q
You won 0 times, while the computer beat you 1900022269 times. You both tied 3 times.
Thank you for playing!
Note that I have played several games instead of running and quitting and have also gotten bad values for "l" and "t".
It seems to work though if I define the variables "w, l, t" as global variables; however, is there a way for this to work with those variables being within the scope of the declareWin function?
Code
int declareWin (int one, int two) {
int w, l, t;
if (one == 1 && two == 1) {
printf("You chose Rock, Computer chose Rock. Rock does not beat Rock.\n");
printf("It is a tie!\n");
t++;
}
else if (one == 1 && two == 2) {
printf("You chose Rock, Computer chose Paper. Paper covers Rock.\n");
printf("Computer wins!\n");
l++;
}
else if (one == 1 && two == 3) {
printf("You chose Rock, Computer chose Scissors. Rock smashes Scissors.\n");
printf("You win!\n");
w++;
}
else if (one == 2 && two == 1) {
printf("You chose Paper, Computer chose Rock. Paper covers Rock.\n");
printf("You win!\n");
w++;
}
else if (one == 2 && two == 2) {
printf("You chose Paper, Computer chose Paper. Paper does not beat Paper.\n");
printf("It is a tie!\n");
t++;
}
else if (one == 2 && two == 3) {
printf("You chose Paper, Computer chose Scissors. Scissors cuts Paper.\n");
printf("Computer wins!\n");
l++;
}
else if (one == 3 && two == 1) {
printf("You chose Scissors, Computer chose Rock. Rock smashes Scissors.\n");
printf("Computer wins!\n");
l++;
}
else if (one == 3 && two == 2) {
printf("You chose Scissors, Computer chose Paper. Scissors cuts Paper.\n");
printf("You win!\n");
w++;
}
else if (one == 3 && two == 3) {
printf("You chose Scissors, Computer chose Scissors. Scissors does not beat Scissors.\n");
printf("It is a tie!\n");
t++;
}
else if (one == 0) {
printf("You won %d times, while the computer beat you %d times. You both tied %d times.\n", w, l, t);
printf("Thank you for playing!");
}
else
;
}
Link
Here is a link to a pastebin of the entire program in case the problem is somewhere else: Link to pastebin
回答1:
These variables w, l, t;
will have random values taken from stack:
int w, l, t;
//...
t++; // random number increased
//...
l++; // random number increased
//...
w++; // random number increased
//
printf("You won %d times, while the computer beat you %d times. You both tied %d times.\n", w, l, t);
To correct your program you have to remember w, l, t;
. They have to survive the function call.
There are many solution. You can use brute force global declaration of w, l, t;
(not elegant) or you pass w, l, t;
as a parameters.
int declareWin (int one, int two, int *w, int *l, int* t)
{
//..
(*t)++;
//...
(*l)++;
//...
(*w)++;
//...
}
Edit:
I looked at your program. You were already thinking about global variables:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//int w, l, t;
As a quick try, I removed //
from the line above, commented out the declaration of w, l, t
in
//declareWin Function
int declareWin (int one, int two) {
//int w, l, t;
compiled and played the game:
r
p
s
s
r
q
Enter R, P, S, or Q (for quit)
You chose Rock, Computer chose Rock. Rock does not beat Rock.
It is a tie!
Enter R, P, S, or Q (for quit)
You chose Paper, Computer chose Paper. Paper does not beat Paper.
It is a tie!
Enter R, P, S, or Q (for quit)
You chose Scissors, Computer chose Paper. Scissors cuts Paper.
You win!
Enter R, P, S, or Q (for quit)
You chose Scissors, Computer chose Paper. Scissors cuts Paper.
You win!
Enter R, P, S, or Q (for quit)
You chose Rock, Computer chose Rock. Rock does not beat Rock.
It is a tie!
Enter R, P, S, or Q (for quit)
You won 2 times, while the computer beat you 0 times. You both tied 3 times.
Thank you for playing!
However, please learn how to pass variables using pointers. Avoid global variables.
回答2:
You need to initialize your variables before you use them. If you don't initialize them, the compiler will not necessarily set them to 0, so they can have almost any value.
来源:https://stackoverflow.com/questions/48995758/c-variable-increment-not-working-rock-paper-scissors