why the score1 and score2 variables exceed the maximum value of 50?
Hello,
I have made this little game using the random function.
But there's a problem because the score1 and score2 exceeds the value of 50 contained in the while cycle.
I would like to understand why.
Thanks in advance...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int dice1, dice2, score1, score2;
srand((unsigned)time(NULL));
dice1 = dice2 = score1 = score2 = 0;
while(score1 < 50 || score2 < 50)
{
//player 1
dice1 = (rand()%6)+1;
dice2 = (rand()%6)+1;
if(dice1 == dice2)
{
printf("Player 1 scored double %d\n", dice1);
if(dice1 == 3)
score1 = 0;
else if(dice1 == 6)
score1 = score1 + 25;
else
score1 = score1 + 5;
printf("Player 1 = %d\n", score1);
}
//player 2
dice1 = (rand()%6)+1;
dice2 = (rand()%6)+1;
if(dice1 == dice2)
{
printf("Player 2 scored double %d\n", dice1);
if(dice2 == 3)
score2 = 0;
else if(dice2 == 6)
score2 = score2 + 25;
else
score2 = score2 + 5;
printf("Player 2 = %d\n", score2);
}
}
if(score1 == score2)
printf("Game is a tie\n");
else if(score1 < score2)
printf("Player 1 Wins\n");
else
printf("Player 2 Wins\n");
return 0;
}