Home Forums Programming C programming Dice game problem?

  • This topic has 3 replies, 4 voices, and was last updated 14 years ago by Anonymous.
Viewing 3 reply threads
  • Author
    Posts
    • #7603
      Anonymous
      Inactive

      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 <stdio.h>
      #include <time.h>
      #include <stdlib.h>

      #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);

      }

    • #45261
      Anonymous
      Inactive

      Right, some of the problems I’ve spotted:

      You’re not initialising Points to be 0. This means it’s most likely filled with garbage data when you’re doing a Points = Points + value;

      You can cut down on the if statements by doing

      [code:1:6a5b75c2d7]if(RedDice == BlueDice) {
      if(RedDice > 1 && RedDice < 6){
      Points = 10;
      } else {
      Points = 5;
      }
      }[/code:1:6a5b75c2d7]

      Because you’re not reusing Points at any stage, you don’t need to add to it, you can just assign.

    • #45286
      Anonymous
      Inactive

      If that is your exact code, you have your bracketing all wrong. There is an opening "{" after the first if and you only close it after the last if.

    • #45653
      Anonymous
      Inactive

      if (RedDice == BlueDice)
      {
      if (RedDice == 6 || RedDice == 1)
      Points = Points +10;
      else
      Points = Points +5;
      }

      is that make sence?

Viewing 3 reply threads
  • The forum ‘Programming’ is closed to new topics and replies.