C programming Dice game problem?
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);
}
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);
}
I'm not going to lie, you reek of being a spambot by your recent posts and the time in between each...I don't know. If you are one, you're a very good one.
At any rate, whenever posting code, put it in between a set of code brackets. Now in relation to your code, your logic is quite flawed. This is what your code is equivalent to (with brackets):
What you want is an if-else-if construct:
Which can be further simplified into:
There's a number of other things that I could talk about, but I'm just going to keep it at that. You'll learn all the other stuff in due time; you need to learn some more about C/C++.
At any rate, whenever posting code, put it in between a set of code brackets. Now in relation to your code, your logic is quite flawed. This is what your code is equivalent to (with brackets):
Code: Select all
if( RedDice == 6 && BlueDice == 6 )
{
Points = Points + 10;
if( RedDice == 1 && BlueDice == 1 )
{
Pounts = 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;
}
}
Code: Select all
if( RedDice == 6 && BlueDice == 6 )
{
Points = Points + 10;
}
else if( RedDice == 1 && BlueDice == 1 )
{
Pounts = 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;
}
Code: Select all
if( RedDice == 6 && BlueDice == 6 )
{
Points = Points + 10;
}
else if( ( RedDice == 1 && BlueDice == 1 ) )
{
Points = Points + 10;
}
else if( ( RedDice == 2 && BlueDice == 2 ) ||
( RedDice == 3 && BlueDice == 3 ) ||
( RedDice == 4 && BlueDice == 4 ) ||
( RedDice == 5 && BlueDice == 5 ) )
{
Pounts = Points + 5;
}
Last edited by Halifax on Thu Feb 04, 2010 4:56 am, edited 1 time in total.
TheQuestion = 2B || !2B
-
- Competition winner
- Posts: 1123
- Joined: Sun Jun 10, 2007 11:14 pm
Code: Select all
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;
}
Code: Select all
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;
Code: Select all
if (RedDice == BlueDice) {
Points += 5;
if (RedDice == 6 || RedDice == 1)
Points += 5;
}
Hm, just what I'm wondering atm. He posted in threads typically liked by spam-bots and the timing is that of a spam-bot. Also the code here is general and not Irrlicht specific - so would probably fit in most coder forums. But then again no spam-links so far. *sigh*.Halifax wrote:I'm not going to lie, you reek of being a spambot by your recent posts and the time in between each...I don't know. If you are one, you're a very good one.
So ashtonfarell - please show a life sign or we have to delete the account and send out our usual mafia contract to hunt you down and put a horse-head in your bed (nothing personal, just general spambot handling).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code: Select all
if (RedDice == BlueDice) {
Points += 5;
if (RedDice == 6 || RedDice == 1)
Points += 5;
}
BTW dudes, this has gotta be a spam bot.
I'd like to deliver the horses head.
I wonder why his names not Colin Farrell or Ashton Kutcher.
Instead it's Ashton Farell..
I can hear birds chirping
I live in the Eye of Insanity.
I live in the Eye of Insanity.
Yeah, sorry in that case. I guess having since several months a spambot in the forum which creates every morning a new user and posts 3-4 messages in a row within a few minutes sort of trained my brain to recognize that pattern as spam. But I guess occasionally a human might actually do the same. So don't worry - your horses are safe for now.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
I guess the basic idea is simply to get accounts - probably because in some forums you get more access to other users data when you have an account yourself. Usually spammers also add links (but not all do) - but I suspect sooner or later one will learn to add the spam-links a day later to make admin lives a little worse again.Bate wrote:What would be the point of "spamming" general programming questions? :)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
I take my apology back, user deleted, Ulf you're free to deliver the horse head. And we have to deal with a new kind of spammer which is able to fill out information correctly, write sane sentences, post general code into programming forums and adds his spam-link 3 days later to his signature (although not yet clever enough to realize that the url tag does not work in the signature).
(explanation: usb-flash-drive spam had been added to the signature)
(explanation: usb-flash-drive spam had been added to the signature)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- Posts: 3
- Joined: Fri Mar 26, 2010 9:05 am