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

  1. #1
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29

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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29
    Quote Originally Posted by matsp View Post
    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. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29
    Quote Originally Posted by matsp View Post
    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. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fsx View Post
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by fsx View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    May 2009
    Location
    Slovenia
    Posts
    5
    Hello.
    Quote Originally Posted by MK27 View Post
    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.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tadeboro View Post
    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).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tadeboro View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29
    Quote Originally Posted by matsp View Post
    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
    I have added

    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. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29
    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! =)
    Last edited by fsx; 05-13-2009 at 07:29 AM.

Popular pages Recent additions subscribe to a feed