Are you a spammer

Please note, that the first 3 posts you make, will need to be approved by a forum Administrator or Moderator before they are publicly viewable.
Each application to join this forum is checked at the Stop Forum Spam website. If the email or IP address appears there when checked, you will not be allowed to join this forum.
If you get past this check and post spam on this forum, your posts will be immediately deleted and your account inactivated.You will then be banned and your IP will be submitted to your ISP, notifying them of your spamming. So your spam links will only be seen for an hour or two at most. In other words, don't waste your time and ours.

This forum is for the use and enjoyment of the members and visitors looking to learn about and share information regarding the topics listed. It is not a free-for-all advertising venue. Your time would be better spent pursuing legitimate avenues of promoting your websites.

C programming Dice game problem?

Source code I have written openly published for your viewing pleasure.


C programming Dice game problem?

Postby jobypollard » Wed Jan 27, 2010 7:13 am

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

}
jobypollard
U.E. Newbie
U.E. Newbie
 
Posts: 4
Joined: Wed Jan 27, 2010 6:54 am
Operating System: Ultimate Edition 3.2 32 BIT



Re: C programming Dice game problem?

Postby red_team316 » Wed Feb 03, 2010 9:24 pm

It is always good programming practice to initialize your variables when they are declared. With your code, I got this output:
Code: Select all
me@dev-box:~/Desktop/dice$ ./dice
Rolling Dice
Red Dice is 1
Blue Dice is 3
You have 32767 points


There are plenty of resources online to learn more about programming. It can be tricky at first but you'll get the hang of it if you try hard enough :)

Try this out:
Compile in terminal and then run program
Code: Select all
gcc -o dice dice.c
./dice


dice.c
Code: Select all
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define ROLL_DIE ((rand() % 6) +1)

int rollDice(void)
{
    int Points = 0;
    int RedDice = ROLL_DIE;
    int BlueDice = ROLL_DIE;

    if(RedDice == BlueDice)
    {
        if(RedDice <= 0 || RedDice > 6)
        {
            printf("Dice out of range: %d\n", RedDice);
            return(0);
        }
        else if(RedDice == 1 || RedDice == 6)
            Points = 10;
        else
            Points = 5;
    }
    printf("Red: %d, Blue: %d, Points: %d\n", RedDice, BlueDice, Points);
    return(Points);
}

int main(void)
{
    int TotalPoints = 0;
    int Times = 3;       /* Let's roll 3 times */
    int i = 0;           /* used for looping */

    srand(time(NULL));

    printf("Rolling Dice...\n" );
    for(i = 0; i < Times; i++)
    {
        printf("Roll #%d ", i+1);
       
        TotalPoints += rollDice();
    }

    printf("You have %d total points\n", TotalPoints);
    return(0);
}



Core i7 920(working on a decent OC), x58 ASUS P6T Deluxe V2, 6GB DDR3 1600, EVGA 8800GTS512, Silencer 750W PSU, CoolerMaster V8, Red Antec900
Image
User avatar
red_team316
U.E. College Professor
U.E. College Professor
 
Posts: 288
Joined: Mon Jan 07, 2008 12:37 am
Age: 39
Operating System: Ultimate Edition 3.2 64 BIT



Re: C programming Dice game problem?

Postby JOHNNYG » Sun Feb 07, 2010 2:14 am

HEY !!! I want to play !!! <BREW> :lol: :D
JOHNNYG
Image
Ultimate Eddiction<
Ultimate Edition STUDIO
Ultimate-E
Onyx 64
Pentium 4 processor
2x512 Kingston DDR memory
Maxtor Diamondmax 500 gig
Maxtor Maxline 80 gig, Split for testing only !
Maxtor Diamondmax 500 gig, Storage
Booting 8 OS's, No MS !
ATI 9600 series (RV350 AQ)graphics card
Sony DVD/CD Rewritable Drive DOUBLE LAYER DRU-820A/Sony DRU-800A CD DVD±RW Dual DVD Recorder
Team Leader at
http://www.ultimateeditionoz.com/
Image
Image
Image
User avatar
JOHNNYG
Site Admin
 
Posts: 1456
Joined: Mon Apr 13, 2009 12:02 am
Location: U.S.A. Illinois
Operating System: Ultimate Edition 3.2 32 BIT


Return to Programming

Who is online

Users browsing this forum: No registered users and 5 guests