Hi, I'm making a simple "Rock, paper, scissors" game in C to practice the usage of pointers in functions to modify variables defined in the `main()' function. The problem is that when one of the players reach the score needed to win the game, the program will continue the loop instead of exiting it. Here's the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void PointsNeededToWin(int *points) {
do {
printf("How many points are needed to win? ");
scanf("%i", points);
if ((*points) <= 0) {
printf("It must be a positive number.\n");
}
} while ((*points) <= 0);
getchar();
printf("\n");
}
int RivalTurn() {
int element = rand() % 3;
return element;
}
void PlayerTurn(char elements[3][9], int *element) {
int check = 0;
char PlayerElement[8];
do {
printf("Enter your choice [rock/paper/scissors]: ");
fgets(PlayerElement, 8, stdin);
strtok(PlayerElement, "\n");
for (int i = 0; i < 3; i++) {
if (strncmp(PlayerElement, elements[i], 8) == 0) {
*element = i;
check = 1;
}
}
if (check == 0) {
printf("Invalid choice.\n");
}
} while (check == 0);
}
void ChangeScoreboard(char elements[3][9], int RivalElement, int PlayerElement, int *RivalScore, int *PlayerScore) {
printf("You chose %s, and the rival chose %s.\n", elements[PlayerElement], elements[RivalElement]);
if (PlayerElement == 0) {
if (RivalElement == 0) {
printf("Draw.\n");
}
else {
if (RivalElement == 1) {
printf("Paper beats rock.\n");
(*RivalScore)++;
}
else {
printf("Rock beats scissors.\n");
(*PlayerScore)++;
}
}
}
else {
if (PlayerElement == 1) {
if (RivalElement == 0) {
printf("Paper beats rock.\n");
(*PlayerScore)++;
}
else {
if (RivalElement == 1) {
printf("Draw.\n");
}
else {
printf("Scissors beats paper.\n");
(*RivalScore)++;
}
}
}
else {
if (RivalElement == 0) {
printf("Rock beats scissors.\n");
(*RivalScore)++;
}
else {
if (RivalElement == 1) {
printf("Scissors beats rock.\n");
(*PlayerScore)++;
}
else {
printf("Draw.\n");
}
}
}
}
printf("You've got %i point(s) y your rival has got %i point(s).\n\n", *PlayerScore, *RivalScore);
}
void winner(int NeededPoints, int PlayerScore) {
if (PlayerScore == NeededPoints) {
printf("ˇYou won!\n");
}
else {
printf("You lost...\n");
}
}
int main() {
char elements[3][9] = {"rock", "paper", "scissors"};
int NeededPoints, PlayerScore, RivalScore, PlayerElement, RivalElement;
PlayerScore = 0;
RivalScore = 0;
PointsNeededToWin(&NeededPoints);
srand(time(NULL));
while ((PlayerScore != NeededPoints) || (RivalScore != NeededPoints)) {
RivalElement = RivalTurn();
PlayerTurn(elements, &PlayerElement);
ChangeScoreboard(elements, RivalElement, PlayerElement, &RivalScore, &PlayerScore);
}
winner(NeededPoints, PlayerScore);
return 0;
}
Like I said, the `while' loop inside the `main()' function never stops even if one of the players achieve the required score. As you can see, I increment the players' scores using a function with two pointers, the players' elements and the array of the elements' names as arguments. I can't figure out why the loop never ends. If I print the variables' values below the `ChangeScoreboard()' it turns out they are being incremented successfully, so I can't find the reason why the `while' statement never evaluates to true.
Does anyone know why is it behaving like that?
Thanks in advance.