Get the latest Education e-news

 Game Career Guide Forums C programming Dice game problem?
 Forum Home Register Members List Mark Forums Read

01-27-2010, 10:23 PM   #1
ashtonfarell
De-Registered User

C programming Dice game problem?

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).

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

}

01-28-2010, 12:15 AM   #2
DTR
Member

Location: Helsinki, Finland

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

Only valid case in this program would be 6,6. All the following comparisons are meaningless as the execution never reaches them except with two sixes.

Removing the brackets would bring happiness and joy.

01-28-2010, 09:51 PM   #3
jwalters
Senior Member

Location: Apex NC, USA

Darn, DTR beat me to it! So just for fun, a simplification:

if (RedDice == BlueDice)
{
if (RedDice == 1 || RedDice == 6)
Points += 10;
else
Points += 5;
}
__________________
Jeromie L. Walters
http://gamedeveloperjourney.blogspot.com

02-10-2010, 06:29 PM   #4
RXD
Banned

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.

02-11-2010, 05:05 AM   #5
kbaxter
Member

What IDE are you using for your programming? Many have a debugger built in, which can help you find things like this. It lets you step through the code as its running, line by line, to see exactly what code it's executing in what order, and check what the values of your variables are as it does it. When you can actually see exactly what code is executing and why, it's often much quicker to figure out what's wrong!

12-13-2010, 05:07 PM   #6
fredrick
Junior Member

The code looks pretty good to me but Why would you opt for such a lengthy procedure instead of using random function. It would be much better and easy. Plus i think it will execute further. The syntax being random(). Generates number randomly. Anyways it is a bright idea.

06-20-2011, 01:48 AM   #7
yaustar

Location: UK

There are a few standalone debuggers but VS's IDE's debugger is really, really good.

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off