| View previous topic :: View next topic |
| Author |
Message |
ashtonfarell Guest
|
Posted: Sat Jan 30, 2010 7:03 am Post subject: C programming Dice game problem? |
|
|
Hey everybody
I am 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);
} |
|
| Back to top |
|
 |
shaddack

Joined: Mon Mar 17, 2008 6:03 pm Posts: 70 Location: Gothenburg, Sweden
|
Posted: Sat Jan 30, 2010 7:51 am Post subject: |
|
|
You will only enter the if-statement if you get double sixes. Further you have to use the else-statement to catch all the cases. Somtehing like this instead:
#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;
}
else if (RedDice ==1 && BlueDice ==1 )
{
Points = Points + 10;
}
else if(RedDice == 2 && BlueDice == 2 )
{
Points = Points +5;
}
else if(RedDice == 3 &&BlueDice == 3 )
{
Points = Points +5;
}
else if(RedDice == 4 &&BlueDice == 4 )
{
Points = Points +5;
}
else if(RedDice == 5 && BlueDice == 5 )
{
Points = Points +5;
}
printf("You have %d points\n", Points);
return(0);
} |
|
| Back to top |
|
 |
JPGargoyle

Joined: Fri Jul 29, 2005 12:29 am Posts: 20
|
Posted: Thu Feb 04, 2010 12:55 pm Post subject: simpler version |
|
|
Or even simpler:
| 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 == BlueDice) {
Points += 5;
if (RedDice == 1 || RedDice == 6) {
Points += 5;
}
}
printf("You have %d points\n", Points);
return(0);
}
|
Best regards. |
|
| Back to top |
|
 |
donoreo LXF regular

Joined: Mon Apr 11, 2005 2:49 pm Posts: 788 Location: Toronto, Ontario, Canada
|
Posted: Sun Feb 07, 2010 4:42 pm Post subject: |
|
|
Sounds like a school homework assignment. _________________ I cannot deny anything that I did not say. |
|
| Back to top |
|
 |
Rhakios Moderator

Joined: Thu Apr 07, 2005 12:18 am Posts: 7473 Location: Midlands, UK
|
Posted: Sun Feb 07, 2010 9:15 pm Post subject: |
|
|
| donoreo wrote: | | Sounds like a school homework assignment. |
The only reason this thread is still here is because someone troubled to answer the question, the question itself was copied and pasted from another forum as a mask for some spam. Hence why the original questioner is listed as Guest, when we don't actually allow guest posting.
Thanks to Mike for clearing that one up for me. _________________ Bye, Rhakios |
|
| Back to top |
|
 |
JPGargoyle

Joined: Fri Jul 29, 2005 12:29 am Posts: 20
|
Posted: Tue Feb 23, 2010 1:10 pm Post subject: |
|
|
Using code to spam!!!
Is there nothing sacred anymore? |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|