|
|||||||
| Forum Home | Register | Members List | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | ||||||||
|
De-Registered User
|
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); } |
||||||||
|
|
|
#2 | ||||||||
|
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; } 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. |
||||||||
|
|
|
|
|
#3 | ||||||||
|
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; } |
||||||||
|
|
|
|
|
#4 | ||||||||
|
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. |
||||||||
|
|
|
|
|
#5 | ||||||||
|
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!
|
||||||||
|
|
|
|
|
#6 | ||||||||
|
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.
|
||||||||
|
|
|
|
|
#7 | ||||||||
|
Administrator
Location: UK |
There are a few standalone debuggers but VS's IDE's debugger is really, really good.
__________________
Steven Yau [Alix Games Blog] [Portfolio] [How I broke into the Games Industry] [Why I left my Games Job] [How to be a Games Tester] [Getting back into the Game] |
||||||||
|
|
|
![]() |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 10:37 PM.




























Linear Mode

