# Yahtzee game (C programming)

Page 1 of 1

## 3 Replies - 6642 Views - Last Post: 27 February 2011 - 09:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=219148&amp;s=2c0c4d3f1cabb3f99ee09203feccb5ec&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 evdim

Reputation: 0
• Posts: 3
• Joined: 27-February 11

# Yahtzee game (C programming)

Posted 27 February 2011 - 11:41 AM

hey. I have an assignment to make a yahtzee game and im really stuck. I cant figure out what to do for this part of the code:
(5) (10 pts) Roll the five dice and display the face values of each die; enumerate each die with
a number 1 - 5; add 1 to the total number of rolls for this round
(6) (10 pts) If the total number of rolls for this round is less than three,
then ask the player (Y/N) if he/she wants to use the roll for one of the game combinations
otherwise a combination must be selected

I did number 5, but i dont know why or how to "add 1 to the total number of rolls". I also dont know what 6 means. I mean we just rolled five dice in step 5 and now it says if the number of rolls are less than 3??!!

here is my code so far. It completely compiles and im getting no errors so i am not writing the details of functions.

```#include "function.h"

int main(void)
{
int choice = 0, die1 = 0, die2 = 0, die3 = 0, die4 = 0, die5 = 0, press9 = 0;

srand(time(NULL));

switch (choice)
{
case 1:
game_rules();
break;
case 2:
press();
die1 = roll_die();
printf ("die1: %d\n",die1);
die2 = roll_die();
printf ("die2: %d\n",die2);
die3 = roll_die();
printf ("die3: %d\n",die3);
die4 = roll_die();
printf ("die4: %d\n",die4);
die5 = roll_die();
printf ("die5: %d\n",die5);
break;
case 3:
printf ("Goodbye!\n");
break;
default:
break;
}

return 0;
}
```

This post has been edited by evdim: 27 February 2011 - 11:44 AM
Reason for edit:: Please use code tags!!!

Is This A Good Question/Topic? 0

## Replies To: Yahtzee game (C programming)

### #2 apw5020

Reputation: 78
• Posts: 666
• Joined: 26-March 09

## Re: Yahtzee game (C programming)

Posted 27 February 2011 - 11:56 AM

Maybe a roll counter, since each player gets more than one roll of the dice per round? I forget all the rules of Yahtzee.

```int rollCount = 0;
while(rollCount < 3)
{
//...
rollCount++;
//...
}

```

This post has been edited by apw5020: 27 February 2011 - 11:58 AM

### #3 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Yahtzee game (C programming)

Posted 27 February 2011 - 12:15 PM

apw5020, on 27 February 2011 - 08:56 PM, said:

Maybe a roll counter, since each player gets more than one roll of the dice per round?

Wikipedia said:

The dice can be rolled up to three times in a turn to try to make one of the thirteen possible scoring combinations.

http://en.wikipedia.org/wiki/Yahtzee

### #4 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Yahtzee game (C programming)

Posted 27 February 2011 - 09:05 PM

Quote

im trying to write a code for yahtzee in C (not C++). right now, im doing the functions. but I cant get it to work. i am trying to access all the die values, and if they are equal to '1', then add them together. this is what i have so far, but when i run it, i dont get the right results( for example my die values are: die1 = 1, die2 = 2, die3 = 1, die4 = 5, die5 = 1; and while i want to get the answer of 3 (1+1+1), i just get a 1 instead.)

```int ones (int die1, int die2, int die3, int die4, int die5)
{
int a = 0;
for ( a < 5; a ++;)/>
{
if (die1 == 1)
{
a += 1;
}
else if (die2 == 1)
{
a += 1;
}
else if (die3 == 1)
{
a += 1;
}
else if (die4 == 1)
{
a += 1;
}
else if (die5 == 1)
{
a += 1;
}
else
{
a += 0;
}

}
return a;
}

```

Why do you need the for loop?

You use if else statements, that means that only one if section will be executed.

If you pass the value you are looking for, you could use the function to check for other numbers not just one.