Get the latest Education e-news
 

Go Back   Game Career Guide Forums > Programming
Forum Home Register Members List Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 01-27-2010, 10:23 PM   #1
ashtonfarell
De-Registered User

Activity Longevity
0/20 0/20
Today Posts
0/0 sssssssss
Default 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).

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

}
  Reply With Quote
Old 01-28-2010, 12:15 AM   #2
DTR
Member

Activity Longevity
0/20 17/20
Today Posts
0/11 sssssss94
Location: Helsinki, Finland
Default

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


There is your problem.
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.
DTR is offline   Reply With Quote
Old 01-28-2010, 09:51 PM   #3
jwalters
Senior Member

Activity Longevity
0/20 15/20
Today Posts
0/11 ssssss142
Location: Apex NC, USA
Default

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
jwalters is offline   Reply With Quote
Old 02-10-2010, 06:29 PM   #4
RXD
Banned

Activity Longevity
0/20 12/20
Today Posts
0/11 ssssssss3
Default

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.
RXD is offline   Reply With Quote
Old 02-11-2010, 05:05 AM   #5
kbaxter
Member

Activity Longevity
0/20 13/20
Today Posts
0/11 sssssss90
Default

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!
kbaxter is offline   Reply With Quote
Old 12-13-2010, 05:07 PM   #6
fredrick
Junior Member

Activity Longevity
0/20 10/20
Today Posts
0/11 ssssssss5
Default

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.
fredrick is offline   Reply With Quote
Old 06-20-2011, 01:48 AM   #7
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
1/20 19/20
Today Posts
1/11 sssss2244
Location: UK
Default

There are a few standalone debuggers but VS's IDE's debugger is really, really good.
yaustar is offline   Reply With Quote
Reply


Thread Tools
Display Modes

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


Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 10:30 AM.






UBM Tech