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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

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

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

  4. #4
    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?

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

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