How could I create a yahtzee game?

Pages: 12
Can anyone help me create a yahtzee game? I have the very first part, but I need some help with the rest.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
   srand((unsigned)time(0)) ;
   int roll_one=rand()%6+1 ;
   int roll_two=rand()%6+1 ;
   int roll_three=rand()%6+1 ;
   int roll_four=rand()%6+1 ;
   int roll_five=rand()%6+1 ;

Now what should I do?
Well, you are going to want to keep track of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    Two or more users;
    A scoring grid, one for each user;
    Logic to make sure that only the product of the number of dice are allowed into a box in the
upper section ( 3 Threes = 9 points only);
    Logic to make sure the user can't change a value after scoring;
    Math to total up the top section to see if the user has made the upper bonus;
    Logic to score the exact number of points for 3-of-a-kind, 4-of-a-kind, full house, etc;
    Math to total up the bottom section;
    Logic and math to total up the user's score;


That should keep you busy!

You need arrays or vectors for this, so you should cover these first if you haven't already.
Having six individual variables certainly isn't the way to go.
Last edited on
Based on that code you should look up recursion, it's not anything you have to study per say but it's useful to cut down on redundency like you have there.

I'm not trying to insult you mcqueen you seem to have great ideas and a passion for C++ or at least a drive to learn it and that is a great thing. But you are getting a little a head of yourself.

That block you wrote could be written a number of ways. But to keep to a format I'm confident you'll recognize:
1
2
3
4
5
6
srand(time(NULL));
int NoD = 4; /*Number of Dice*/
int roll[NoD];

for(int i = 0; i <= NoD; i++)
{roll[i] = rand()%6+1;}
Off the top of my head, my have errors.
One question. How do you use vectors, I know how, then forgot. I looked it up and this time I don't get it. This kind of thing has happened before. Just could you give me a tip n vectors and remind me how they work? Thanks!
Once you remind me on vectors, I'll have a couple ideas how to start out taking this. I'm trying my best to stay away from just coding for now, until I know for sure what I'll use. I've got a strategy to keep my ideas in order. So, like I asked before, I just need to be reminded on how vectors work.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <vector>
using namespace std;

int main()
{
  vector<int> vec;
  for (int i=0;i<10;i++)vec.push_back(i*2); //adding elements
  for (int i=0;i<10;i++)cout << vec[i] << endl; //accessing elements
}


See also:
http://www.cplusplus.com/reference/stl/vector/
Here's what I've decided to use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    srand((unsigned)time(0)) ;
    
    //Player One rolling dice
    vector<int> PlayerOne ;
    int roll11=rand()%6+1 ;
    int roll21=rand()%6+1 ;
    int roll31=rand()%6+1 ;
    int roll41=rand()%6+1 ;
    int roll51=rand()%6+1 ;
    int dice1[]={roll11, roll21, roll31, roll41, roll51} ;
    for (int i=0; i<5; i++)
    {
        PlayerOne.push_back(dice1[i]) ;
    }
    cout<<"Player One:" <<endl ;
    for (int i=0; i<5; i++)
    {
        cout<<PlayerOne[i] <<endl ;
    }
    
    //Player Two rolling dice
    vector<int> PlayerTwo ;
    int roll12=rand()%6+1 ;
    int roll22=rand()%6+1 ;
    int roll32=rand()%6+1 ;
    int roll42=rand()%6+1 ;
    int roll52=rand()%6+1 ;
    int dice2[]={roll12, roll22, roll32, roll42, roll52} ;
    for (int i=0; i<5; i++)
    {
        PlayerTwo.push_back(dice2[i]) ;
    }
    cout<<"Player Two:" <<endl ;
    for (int i=0; i<5; i++)
    {
        cout<<PlayerTwo[i] <<endl ;
    }
    
    system("PAUSE") ;
}

Now, how should I make it so the user's can decide which dice/die to roll again?
Last edited on
What should I do next?
i would suggest making a structure that would keep track of one players, then use an array of that structure to keep track of all players.

example:

1
2
3
4
5
6
7
8

struct Playerdata
{
//scores
int ones, twos, threes;
//etc
}


There is no reason to use all these variables.
1
2
vector<int> PlayerOne;
for (int i=0; i<5; i++)PlayerOne.push_back(rand()%6+1);

is entirely sufficient.
Thanks Athar and my ideas for the next part definitely will contain your idea Zap. Thanks!
Here's what I got now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    srand((unsigned)time(0)) ;
    
    //Player One rolling dice
    vector<int> PlayerOne ;
    for (int i=0; i<5; i++)
    {
        PlayerOne.push_back(rand()%6+1) ;
    }
    cout<<"Player One:" <<endl ;
    for (int i=0; i<5; i++)
    {
        cout<<PlayerOne[i] <<endl ;
    }
    
    //Player Ones Data Structure
    struct PlayerOneData
    {
           int score1 ; //The Score
           int ones1, twos1, threes1, fours1, fives1, sixes1, ToAK1, FoAK1, Fullhouse1, Sstraight1, Lstraight1, Chance1, Yahtzee1 ; //All Actions
    };
    
    
    //Player Two rolling dice
    vector<int> PlayerTwo ;
    for (int i=0; i<5; i++)
    {
        PlayerTwo.push_back(rand()%6+1) ;
    }
    cout<<"Player Two:" <<endl ;
    for (int i=0; i<5; i++)
    {
        cout<<PlayerTwo[i] <<endl ;
    }
    
    //Player Two Data Structure
    struct PlayerTwoData
    {
           int score2 ; //The Score
           int ones2, twos2, threes2, fours2, fives2, sixes2, ToAK2, FoAK2, Fullhouse2, Sstraight2, Lstraight2, Chance2, Yahtzee2 ; //All Actions
    }; 
    
    system("PAUSE") ;
}
Last edited on
Now I create the functions for actions, create function for re-rolling dice, right?
I've decided to make this yahtzee with no re-rolls. That makes it tough.
That also makes it not Yahtzee :p. You could do that as a 'checkpoint' for production but don't abandon the project at that point just because.
Well maybe later, when i"m done i'll add it on. But it'll make it a bit tougher that way.
Well, actually, it's more like that it turns the game from a 90% luck-based game to a 100% luck-based game.

There's some more things to do before you can continue, though.
This: int ones1, twos1, threes1, fours1, fives1, sixes1
is again a case where an array or vector should be used. You can use one of the vector constructors to create the vector with a predefined number of elements (which are 0 by default, which is probably what you want).
1
2
3
4
5
6
7
struct PlayerOneData
{
  PlayerOneData() : eyeCount(6) {}
  int score;
  vector<int> eyeCount;
  [...]
};


And the entire point of classes is so you can create several objects that are of the same type.
Therefore, you should have only one class called Player and create two instances of it.

After that, you can start counting the eyes on the dice, which will then allow you to calculate the score for each player.
Your way is quicker, but I will keep all the defined variables because I've been really working on it and I'm close to a part in which I might have to reuse them. Thanks anyway!
luck? there is no such a thing in Yahtzee! http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=30&page=show_problem&problem=1090

About the re-roll.
1
2
cin >> dice;
player[dice] = rand()%6+1;
Now, because the player can choose several dice to re-roll, you need to establish a communication protocol.
By instance:
_ The first number indicates how many dices the player wants to re-roll
_ End the sequence with a 0 or a line break
_ Introduce 5 values that could be '1' (re-roll) or '0' (no re-roll)
Pages: 12