Page 1 of 1

C programming Dice game problem?

Posted: Thu Feb 04, 2010 4:27 am
by ashtonfarell
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);

}

Posted: Thu Feb 04, 2010 4:51 am
by Halifax
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):

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;
	}
}
What you want is an if-else-if construct:

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;
}
Which can be further simplified into:

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;
}
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++.

Posted: Thu Feb 04, 2010 4:51 am
by Lonesome Ducky

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;
} 
Should be:

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;
You have unnecessary brackets on the first if statement, which encloses all the others in it. So basically those only execute if the dice were 6, and obviously none of them would work :lol: Although a much more compact way of righting it is:

Code: Select all

if (RedDice == BlueDice) {
	Points += 5;
	if (RedDice == 6 || RedDice == 1)
		Points += 5;
}
EDIT: Dangit halifax, beat me to it

Posted: Thu Feb 04, 2010 5:16 am
by CuteAlien
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.
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*.

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

Posted: Thu Feb 04, 2010 5:56 am
by Ulf

Code: Select all

if (RedDice == BlueDice) {
   Points += 5;
   if (RedDice == 6 || RedDice == 1)
      Points += 5;
}
Nice code ducky. Very nice.

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..

Posted: Thu Feb 04, 2010 9:02 am
by hybrid
Well, he posted in the "Who is who" thread with correct values and without copying another thread. And he made correct posts in the other threads as well. I doubt that he's a bot. But maybe you scared him off now :wink:

Posted: Thu Feb 04, 2010 9:41 am
by CuteAlien
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.

Posted: Thu Feb 04, 2010 9:46 am
by Bate
What would be the point of "spamming" general programming questions? :)

Posted: Thu Feb 04, 2010 10:08 am
by CuteAlien
Bate wrote:What would be the point of "spamming" general programming questions? :)
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.

Posted: Thu Feb 04, 2010 8:59 pm
by Halifax
I apologize as well. I guess I was operating like CuteAlien especially with the recent increase in attacks. At any rate, sorry ashtonfarrell.

Posted: Sun Feb 07, 2010 6:23 am
by CuteAlien
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)

Posted: Sun Feb 07, 2010 6:34 am
by Halifax
I knew it. That's one awesome spambot though. :lol:

Posted: Sat Feb 13, 2010 8:24 am
by Ulf
It could have just been someone bored.

If it was a bot, it probably has generic comments and it's customized just for programming forums.

Posted: Tue Mar 30, 2010 6:44 am
by michelsteeve
I don't know you are bot or not. But dear when you are making program in C for the dice, i.e., multiple choice, you should use matrix and when try to getting help you should put comments so another person can understand which variable is for what? It's suggestion for you.

Posted: Tue Mar 30, 2010 7:58 am
by Dorth
A bot/shill replying to another? Lol...