C programming Dice game problem?

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
ashtonfarell

C programming Dice game problem?

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

}
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post 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++.
Last edited by Halifax on Thu Feb 04, 2010 4:56 am, edited 1 time in total.
TheQuestion = 2B || !2B
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post 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
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post 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).
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
Ulf
Posts: 281
Joined: Mon Jun 15, 2009 8:53 am
Location: Australia

Post 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..
I can hear birds chirping
:twisted:

I live in the Eye of Insanity.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post 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:
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post 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.
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
Bate
Posts: 364
Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany

Post by Bate »

What would be the point of "spamming" general programming questions? :)
Never take advice from someone who likes to give advice, so take my advice and don't take it.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post 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.
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
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post 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.
TheQuestion = 2B || !2B
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post 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)
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
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

I knew it. That's one awesome spambot though. :lol:
TheQuestion = 2B || !2B
Ulf
Posts: 281
Joined: Mon Jun 15, 2009 8:53 am
Location: Australia

Post 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.
I can hear birds chirping
:twisted:

I live in the Eye of Insanity.
michelsteeve
Posts: 3
Joined: Fri Mar 26, 2010 9:05 am

Post 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.
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

A bot/shill replying to another? Lol...
Post Reply