| Author |
Message |
|
ashtonfarell
noob
Joined: Thu Feb 04, 2010 2:10 am Posts: 4
Tech Knowledge: Average Understanding
|
 C programming Dice game problem?
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);
}
_________________ 16gb usb flash drive
|
| Thu Feb 04, 2010 2:21 am |
|
 |
|
JoJo2
super intense computer nerd
Joined: Sat Jan 03, 2009 8:03 pm Posts: 636 Location: PA & NY
Real Name: Greg
Tech Knowledge: Average Understanding
CPU: Pentium 4 / 2800Mhz
GPU: Geforce4 MX420/Core-250MHz/Mem-166MHz
Memory: DDR2 1GB/800Mhz
|
 Re: C programming Dice game problem?
um.... idk what is wrong. I ran your program and got 22xxxxxxx72 everytime. I rewrote it... Not sure what I changed... but now you get zero points unless you roll doubles. Here's my code... best of luck to you in the future. Lucky you caught me up at 4:40am writing C code for a remote control car micro-controller lol.... fml sometimes hehe 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" ); Points =0;
printf ("Red Dice is %d\n", RedDice=ROLL_DIE); printf ("Blue Dice is %d\n", BlueDice=ROLL_DIE); if (RedDice == 6 && BlueDice == 6 ) { Points +=10; } if (RedDice == 1 && BlueDice == 1 ) { Points +=10; } if(RedDice == 2 && BlueDice == 2 ) { Points +=5; } if(RedDice == 3 && BlueDice == 3 ) { Points +=5; } if(RedDice == 4 && BlueDice == 4 ) { Points +=5; } if(RedDice == 5 && BlueDice == 5 ) { Points +=5; }
printf("You have %d points\n", Points); system("PAUSE"); return(0);
}
edit: i just tested it again & i'm getting the points = 0 problem now... ugh edit2: fixed now for sure lmk if you have any other problems.
_________________ *Me* Dell 4500 || Ubuntu 9.04 || Pentium4/2.8Ghz || DDR2 (2x)512MB/400Mhz || Geforce4 MX 420/Core-250MHz/Mem-166MHz || 40GB (5400rpm) IDE HD || 200GB (7200rpm) IDE HD
Extra Parts: Pentium4/1.9Ghz || All-in-Wonder® X800 XT(AGP)/Core&Mem 500MHz || DDR2 256MB/400Mhz
|
| Thu Feb 04, 2010 4:45 am |
|
 |
|
JoJo2
super intense computer nerd
Joined: Sat Jan 03, 2009 8:03 pm Posts: 636 Location: PA & NY
Real Name: Greg
Tech Knowledge: Average Understanding
CPU: Pentium 4 / 2800Mhz
GPU: Geforce4 MX420/Core-250MHz/Mem-166MHz
Memory: DDR2 1GB/800Mhz
|
 Re: C programming Dice game problem?
I think I found your problem too... weird place for brackets if you ask me. ashtonfarell wrote: 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; }
oh and up above Main(void) watch your bracket there... usually I put it on the same line as "Main" or a blank line... not right before "int"
_________________ *Me* Dell 4500 || Ubuntu 9.04 || Pentium4/2.8Ghz || DDR2 (2x)512MB/400Mhz || Geforce4 MX 420/Core-250MHz/Mem-166MHz || 40GB (5400rpm) IDE HD || 200GB (7200rpm) IDE HD
Extra Parts: Pentium4/1.9Ghz || All-in-Wonder® X800 XT(AGP)/Core&Mem 500MHz || DDR2 256MB/400Mhz
|
| Thu Feb 04, 2010 4:57 am |
|
 |
|
sciguy
Administrator
Joined: Sat May 05, 2007 9:37 pm Posts: 3043 Location: New York, USA
Real Name: Jeremy Blum
Tech Knowledge: High Understanding
CPU: Intel Q9450/400MHz*8
GPU: NVidia GTX260/Core-710MHz/Mem-1130MHz
Memory: OCZ Reaper/4GB/5-7-7-22/960MHz
|
 Re: C programming Dice game problem?
You initialized points, but never gave it a starting value of zero. So the first time you get points = points + something, you have nothing to add it to...
It should be int points = 0;
_________________ PHOENIX Silverstone TJ09-BW Case eVGA x58 Classified Motherboard eVGA GTX470 Fermi Video Card Intel Core i7 930 @ 3.4GHz GB Triple Channel OCZ DDR3 RAM @ 2GHz 2TB WD Caviar Black SATA HDD 1TB WD Caviar Black SATA HDD Xigmatek Dark Knight CPU Cooler Scythe Slipstream 120mm Fans + Zalman Controller 1TB External HDD Silverstone 1kW Active PFC PSU DVD+RW Lightscribe Burners (x2) Sabrent Card Reader Windows 7 Professional x64
SAMSON Lian Li PC-A05NB Case eVGA 750i FTW Motherboard eVGA GTX260 Superclocked Intel Core 2 Quad Q9450 4 GB Dual Channel OCZ DDR2 RAM 1TB Samsung SpinPoint SATA 500GB Samsung SpinPoint SATA (x2) Ultra X3 800W PSU Asus DVD+RW Burner Windows 7 Pro x64
MERCURY Koolance Watercooled Case (CPU, GPU and RAM are watercooled) Asus P5K Deluxe eVGA GeForce 8800GTX Intel Core 2 Quad Q6600 4 GB Dual Channel Mushkin DDR2 RAM 1TB Raid 0 SATA 320GB SATA 750GB eSATA Creative Soundblaster Audigy 4 Coolmax 700W PSU DVD+RW DL Burners (x2) Vista Ultimate 32bit
*Subscribe to my YouTube Channel* *Check out my Blog* *Follow me on Twitter*
|
| Thu Feb 18, 2010 10:48 am |
|
 |
|
JoJo2
super intense computer nerd
Joined: Sat Jan 03, 2009 8:03 pm Posts: 636 Location: PA & NY
Real Name: Greg
Tech Knowledge: Average Understanding
CPU: Pentium 4 / 2800Mhz
GPU: Geforce4 MX420/Core-250MHz/Mem-166MHz
Memory: DDR2 1GB/800Mhz
|
 Re: C programming Dice game problem?
i set Points=0; right after printf("rolling dice");..... I already made sure the code I posted above works & You see those extra random brackets (in the original code) too? I bet those didn't help Good suggestion though. If I don't set something as a value my complier sets the integer to "2"... I have no idea why it picks 2 out of all possible numbers, but w\e. So setting an initial value is important.
_________________ *Me* Dell 4500 || Ubuntu 9.04 || Pentium4/2.8Ghz || DDR2 (2x)512MB/400Mhz || Geforce4 MX 420/Core-250MHz/Mem-166MHz || 40GB (5400rpm) IDE HD || 200GB (7200rpm) IDE HD
Extra Parts: Pentium4/1.9Ghz || All-in-Wonder® X800 XT(AGP)/Core&Mem 500MHz || DDR2 256MB/400Mhz
|
| Thu Feb 18, 2010 4:22 pm |
|
 |
|
sciguy
Administrator
Joined: Sat May 05, 2007 9:37 pm Posts: 3043 Location: New York, USA
Real Name: Jeremy Blum
Tech Knowledge: High Understanding
CPU: Intel Q9450/400MHz*8
GPU: NVidia GTX260/Core-710MHz/Mem-1130MHz
Memory: OCZ Reaper/4GB/5-7-7-22/960MHz
|
 Re: C programming Dice game problem?
2? That's weird... Oh well, I'm glad it's working....
_________________ PHOENIX Silverstone TJ09-BW Case eVGA x58 Classified Motherboard eVGA GTX470 Fermi Video Card Intel Core i7 930 @ 3.4GHz GB Triple Channel OCZ DDR3 RAM @ 2GHz 2TB WD Caviar Black SATA HDD 1TB WD Caviar Black SATA HDD Xigmatek Dark Knight CPU Cooler Scythe Slipstream 120mm Fans + Zalman Controller 1TB External HDD Silverstone 1kW Active PFC PSU DVD+RW Lightscribe Burners (x2) Sabrent Card Reader Windows 7 Professional x64
SAMSON Lian Li PC-A05NB Case eVGA 750i FTW Motherboard eVGA GTX260 Superclocked Intel Core 2 Quad Q9450 4 GB Dual Channel OCZ DDR2 RAM 1TB Samsung SpinPoint SATA 500GB Samsung SpinPoint SATA (x2) Ultra X3 800W PSU Asus DVD+RW Burner Windows 7 Pro x64
MERCURY Koolance Watercooled Case (CPU, GPU and RAM are watercooled) Asus P5K Deluxe eVGA GeForce 8800GTX Intel Core 2 Quad Q6600 4 GB Dual Channel Mushkin DDR2 RAM 1TB Raid 0 SATA 320GB SATA 750GB eSATA Creative Soundblaster Audigy 4 Coolmax 700W PSU DVD+RW DL Burners (x2) Vista Ultimate 32bit
*Subscribe to my YouTube Channel* *Check out my Blog* *Follow me on Twitter*
|
| Tue Feb 23, 2010 11:50 pm |
|
 |
|