Question:
C programming Dice game problem?
Colin Hunter
2009-01-21 19:11:09 UTC
Hey everybody.
I'm working on a dice game using C. You roll two dice (Red and Blue). If you roll doubles, you get points. For doubles of 1 or 6, you get ten points. For doubles 2-5, you get 5 points.
My problem is, whenever I execute the game, I only ever get 0 points even when I get doubles (of anything).

Help please?
Here's the code:

#include
#include
#include

#define ROLL_DIE ((rand() % 6) +1)

int main(void)
{ int RedDice ;
int BlueDice ;
int Points ;

srand(time(NULL));
printf("Rolling Dice\n" );


printf ("Red Dice is %d\n", RedDice=ROLL_DIE);
printf ("Blue Dice is %d\n", BlueDice=ROLL_DIE);
if (RedDice ==6 && BlueDice == 6 )
{Points = Points +10;
if (RedDice ==1 && BlueDice ==1 )
Points = Points + 10;
if(RedDice == 2 && BlueDice == 2 )
Points = Points +5;
if(RedDice == 3 &&BlueDice == 3 )
Points = Points +5;

if(RedDice == 4 &&BlueDice == 4 )
Points = Points +5;
if(RedDice == 5 && BlueDice == 5 )
Points = Points +5;
}

printf("You have %d points\n", Points);

return(0);

}
Three answers:
cja
2009-01-22 07:52:39 UTC
The logic can be simpler than what you have. Try something like this:



/* roll dice, set RedDice and Blue Dice ... */



if (RedDice == BlueDice) {

      Points += 5;

      if ((RedDice == 1) || (RedDice == 6)) {

            Points += 5;

      }

}



The previous responder was correct to recommend you check your curly braces. It looks like the only time you'll set Points is for double sixes. All of your other 'if' statements have to evaluate to false, since they're inside the block for red = blue = 6.
2016-04-02 06:34:26 UTC
one problem is here: int rollDie();// this just a declaration -- you do not actually call the function here bzzzt^^^^ should be die1=rollDie(); die2=rollDie(); printf("the value of the first die is %d\n",die1); printf("the value of the second die is %d\n",die2); // this is also a problem printf("You have accumulated %d points total. \n", &totalroundpointsforplayer); you are using %d (decimal integer) to print the address of totalroundpointsforplayer drop the & --- you do not want its ADDRESS you want its VALUE. Another problem is here: printf("Please pick what option you want: 1, 2, or 3. \n"); scanf("%d",&playerchoice); if(playerchoice == '1') { totalroundpointsforplayer = totalroundpointsforplayer + option1;} bzzzt ----------------^^^ oops! you entered player choice as an integer BUT here you compare the choice to the CHARACTERS '1' '2' or '3" not the same! that is you re comparing the number 1 to the character code for '1' which in ASCII is 49 so if you punched in 49 you would get option 1 should be if(playerchoice == 1) totalroundpointsforplayer += option1; compare playerchoice to the value 1 NOT the character '1' and use the += operator. since it is only one statement the {} are not needed (but not wrong) if(playerchoice == '2') {totalroundpointsforplayer = totalroundpointsforplayer + option2;} if(playerchoice == '3') {totalroundpointsforplayer = totalroundpointsforplayer + option3;} bzzzt-----------------------------------... you never give option1 option2 or option3 any value. It just has the random value in memory that happened to be where the variable was put. also it would be less typing to say totalroundpointsforplayer += option1; // look up the += operator ;-)
2009-01-21 19:24:28 UTC
Try a switch statement if RedDice == BlueDice. And your construct will give you 0 points unless they both are 6 - check your curly brackets.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...