Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C programming Dice game problem?

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
ashtonfarell
Guest





PostPosted: Sat Jan 30, 2010 7:03 am    Post subject: C programming Dice game problem? Reply with quote

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: 71
Location: Gothenburg, Sweden

PostPosted: Sat Jan 30, 2010 7:51 am    Post subject: Reply with quote

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
View user's profile Send private message
JPGargoyle



Joined: Fri Jul 29, 2005 12:29 am
Posts: 20

PostPosted: Thu Feb 04, 2010 12:55 pm    Post subject: simpler version Reply with quote

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
View user's profile Send private message
donoreo
LXF regular


Joined: Mon Apr 11, 2005 2:49 pm
Posts: 788
Location: Toronto, Ontario, Canada

PostPosted: Sun Feb 07, 2010 4:42 pm    Post subject: Reply with quote

Sounds like a school homework assignment.
_________________
I cannot deny anything that I did not say.
Back to top
View user's profile Send private message
Rhakios
Moderator


Joined: Thu Apr 07, 2005 12:18 am
Posts: 7632
Location: Midlands, UK

PostPosted: Sun Feb 07, 2010 9:15 pm    Post subject: Reply with quote

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
View user's profile Send private message
JPGargoyle



Joined: Fri Jul 29, 2005 12:29 am
Posts: 20

PostPosted: Tue Feb 23, 2010 1:10 pm    Post subject: Reply with quote

Shocked

Using code to spam!!!

Is there nothing sacred anymore?
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast