Im making a Rock/Paper/Scissors game and in that i have created two variables which display the scores but they are not getting incremeneted….
Basically They are the variables which get incremented according to which player wins.
Just like Rock wins against paper and scissors so whichever player choose rock will win so i created a variable for player 1 which will be incremented when the player 1 will win.
Same goes with the player 2 but turns out they are not getting incremented….
Code:-
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// Globally declared variables here
int n;
int Pl1choice, Pl2choice;
int Player1 = 0, Player2 = 0;
// Declarations of the functions here
void generatearandomnumber(void);
void Player1choicedisp(void);
int gamelogic(int Pl1choice, int Pl2choice);
void pl1choices(int Pl1choice);
void pl2choices(int Pl2choice);
int main()
{
printf("Hey! You are the player no.1 and Computer is Player no.2n");
for (int i = 1; i <= 3; i++)
{
Player1choicedisp();
scanf("%d", &Pl1choice);
generatearandomnumber();
pl1choices(Pl1choice);
pl2choices(Pl2choice);
// For invalid number inputs.
if (Pl1choice == 0 || Pl1choice == 1 || Pl1choice == 2)
{
continue;
}
else
{
printf("You entered an invalid number so you have to restart the game.n");
break;
}
gamelogic(Pl1choice, Pl2choice);
}
if(Player1 > Player2)
{
printf("Winner is Player1n");
}
else if(Player1 == Player2)
{
printf("Its a tien");
}
else
{
printf("Winner is Player2n");
}
printf("Scores :-n");
printf("Player1 = %dn", Player1);
printf("Player2 = %dn", Player2);
return 0;
}
// For generating Rock/Paper/Scissors.
void generatearandomnumber(void)
{
srand(time(NULL));
Pl2choice = rand() % 3;
}
// For Player1 to enter his choice.
void Player1choicedisp(void)
{
printf("Enter your choice for the Rock/Paper/scissorsn");
printf("Enter 0 for ROCKn");
printf("Enter 1 for PAPERn");
printf("Enter 2 for SCISSORSn");
}
// Main logic for the game.
int gamelogic(int Pl1choice, int Pl2choice)
{
if (Pl2choice == 0 && Pl1choice == 1 || Pl1choice == 2)
{
Player2++;
return Player2;
}
if (Pl1choice == 0 && Pl2choice == 1 || Pl2choice == 2)
{
Player1++;
return Player1;
}
if (Pl2choice == 1 && Pl1choice == 2)
{
Player1++;
return Player1;
}
if (Pl2choice == 2 && Pl1choice == 1)
{
Player2++;
return Player2;
}
}
//For displaying what both of them have chosen.
void pl1choices(int Pl1choice)
{
switch(Pl1choice)
{
case 0:
{
printf("Player 1 chose ROCKn");
break;
}
case 1:
{
printf("Player 1 chose PAPERn");
break;
}
case 2:
{
printf("Player 1 chose SCISSORSn");
break;
}
}
}
void pl2choices(int Pl2choice)
{
switch(Pl2choice)
{
case 0:
{
printf("Player 2 chose ROCKn");
break;
}
case 1:
{
printf("Player 2 chose PAPERn");
break;
}
case 2:
{
printf("Player 2 chose SCISSORSn");
break;
}
}
}
I tried declaring them locally and globally as well but turns out they hold 0 as their value till the end of the program..
2
Answers
Your code is never reaching the gamelogic function because of the continue in your loop:
A continue causes execution to return to the start of the loop. It doesn’t mean continue on from here.
Player1 and Player2 are incremented in the gamelogic function, but it is never called, because Pl1choice must be either 0, 1, or 2, otherwise you have a break.
You have two problems in you code:
gamelogic
can’t be called because it’s at the end of afor
loop always preceded bybreak
orcontinue
, you should written:The code of
gamelogic
is strange (but in your question, you say that rocks always wins, so I may be wrong here):If player 2 choose rock, he win agains paper and scissor, same for player 1.
You have 9 cases to deal with, from player 1 point of view, 3 ties, 3 loss, 3 wins. Pseudo code to do that could look like:
Bonus problem:
You don’t use
scanf
correctly. If player1 choose a first valid choice and after enter garbage, the first valid choice will be kept for secand and third rounds.You should at least read what
scanf
returned to see if an error happened.If -in your case-
scanf
returned1
, that means that it succeeded reading one integer.See beginners-guide-away-from-scanf to learm more about
scanf
.Moreover, you should
srand
only once at the program beginning and not each time you callrand
#define
or betterenum
to work with names likerock
,scissors
andpaper
instead of0
,1
and2
gamelogic
returns a integer, but it’s not used (you don’t read it) and it’s value is not really usable. Consider make it returning nothing (void
)