C programming Dice game problem?

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

C programming Dice game problem?

Postby ashtonfarell » Sat Jan 30, 2010 7:03 am

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

}
ashtonfarell
 

Postby shaddack » Sat Jan 30, 2010 7:51 am

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

}
User avatar
shaddack
 
Posts: 71
Joined: Mon Mar 17, 2008 6:03 pm
Location: Gothenburg, Sweden

simpler version

Postby JPGargoyle » Thu Feb 04, 2010 12:55 pm

Or even simpler:

Code: Select all
#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.
User avatar
JPGargoyle
 
Posts: 20
Joined: Thu Jul 28, 2005 11:29 pm

Postby donoreo » Sun Feb 07, 2010 4:42 pm

Sounds like a school homework assignment.
I cannot deny anything that I did not say.
User avatar
donoreo
LXF regular
 
Posts: 788
Joined: Mon Apr 11, 2005 1:49 pm
Location: Toronto, Ontario, Canada

Postby Rhakios » Sun Feb 07, 2010 9:15 pm

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
User avatar
Rhakios
Moderator
 
Posts: 7634
Joined: Wed Apr 06, 2005 11:18 pm
Location: Midlands, UK

Postby JPGargoyle » Tue Feb 23, 2010 1:10 pm

:shock:

Using code to spam!!!

Is there nothing sacred anymore?
User avatar
JPGargoyle
 
Posts: 20
Joined: Thu Jul 28, 2005 11:29 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests