# Thread: why the score1 and score2 variables exceed the maximum value of 50?

1. ## 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.

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;
}```

2. Code:
`score1 < 50 || score2 < 50`
will be true if score1 or score2 is less than 50 - so it will continue until BOTH are 50 or more.

--
Mats

3. Originally Posted by matsp
Code:
`score1 < 50 || score2 < 50`
will be true if score1 or score2 is less than 50 - so it will continue until BOTH are 50 or more.

--
Mats
I tried with && and it still continues over the limit. There's something I can't understand. =P

4. The loop exits when ONE value is over 50 (when using &&). If you do not want to add a value to exceed 50, then you need to check BEFORE you add if it's breaking the limit or not.

--
Mats

5. Originally Posted by matsp
The loop exits when ONE value is over 50 (when using &&). If you do not want to add a value to exceed 50, then you need to check BEFORE you add if it's breaking the limit or not.

--
Mats
so I should use that construct called DO ... WHILE which I haven't studied yet! =P

6. Originally Posted by fsx
so I should use that construct called DO ... WHILE which I haven't studied yet! =P
I just compiled and tested the OP using "&&" and it does not go over the limit. Perhaps you made some other change afterward; anyway, if you change || to && it will work the way you intend.

7. Originally Posted by fsx
so I should use that construct called DO ... WHILE which I haven't studied yet! =P
Makes little difference, I expect.

You need to check BEFORE the add if the sum is going to be too large.

--
Mats

8. Hello.
Originally Posted by MK27
I just compiled and tested the OP using "&&" and it does not go over the limit. Perhaps you made some other change afterward; anyway, if you change || to && it will work the way you intend.
Actually, this depends. For example, if score1 is 45 before the last dice throw and you get two 6, the final score will be 70.

Hello.
Actually, this depends. For example, if score1 is 45 before the last dice throw and you get two 6, the final score will be 70.
Okay. This would seem a slightly different issue (from the one whereby no one could win until both players top 50).

Hello.
Actually, this depends. For example, if score1 is 45 before the last dice throw and you get two 6, the final score will be 70.
And to solve that, you need to add a check that validates the total value is still under 51 - or say "bust" or whatever.

--
Mats

11. Originally Posted by matsp
And to solve that, you need to add a check that validates the total value is still under 51 - or say "bust" or whatever.

--
Mats

Code:
```		if(score1 >= 50 || score2 >= 50)
break;```
after the while, but it stills won't work. Probably I should rework it completely. Is there a way to reuse my code?

12. I don't know exactly what you the rules for the game is. But if we assume that the rules are "if the sum gets above 50, no score", then something like this:

Code:
```int throwscore = 0;
...
if (dice1 == dice2)
{
...
if (dice1 == 6)
throw_score = 25;
else
throw_score = 5;
if (score1 + throw_score <= 50)
score1 += throw_score;
}```
You may need to adjust this to match your actual scoring rules, but something like that.

--
Mats

13. Final run:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int die1, die2, score1, score2, throwscore;
srand((unsigned)time(NULL));

die1 = die2 = score1 = score2 = throwscore = 0;

while(score1 < 50 && score2 < 50)
{
//player 1
die1 = (rand()%6)+1;
die2 = (rand()%6)+1;

if(die1 == die2)
{
printf("Player 1 scored double %d\n", die1);
if(die1 == 3)
throwscore = score1 = 0;
else if(die1 == 6)
throwscore = 25;
else
throwscore = 5;

if(score1 + throwscore <= 50)
score1 += throwscore;

printf("Player 1 = %d\n", score1);
}

//player 2
die1 = (rand()%6)+1;
die2 = (rand()%6)+1;
if(die1 == die2)
{
printf("Player 2 scored double %d\n", die1);
if(die1 == 3)
throwscore = score2 = 0;
else if(die2 == 6)
throwscore = 25;
else
throwscore = 5;

if(score2 + throwscore <= 50)
score2 += throwscore;

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;
}```
It works! Thanks matsp, you cleared my ideas! =)