Please help with how to manage a yatzy game

Ok so I'm trying to write a yatzy game for my c++ class
but I'm really unsure almost clueless about how to manage with the part when to save dices and keep on rolling.
here is my very unfinished code!
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 #include <iostream>
#include <vector>
using namespace std;
class yazzie
{
int PlayerOne;
public:
void kast()
{
    srand((unsigned)time(0)) ;
    
    //Spelaren rullar träningarna
    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 ;
    }

    
}
void PlayerData()
{
    int score1 ; //the score
    int ones1, twos1, threes1, fours1, fives1, Yahtzee1 ; //Alla sortesr outcomes av träningarna
    
}
    void keep()
    {
        char answer;
        int keeps, keeper = 0, numKeep;
        cout << " Is there any dice that you would like to keep? " << endl;
        cout << " Press '1' for yes or '2' for no: " << endl;
        cin >> answer;
        if( answer == 1 )
        {
            for( int k=0; k<PlayerOne; k++ )
            {
                cout << "Would you like to keep dice " << k+1 << "? " << endl;
                cout << " Press '1' for yes or '2' for no: " << endl;
                cin >> keeps;
                cout << endl;
                
                if( keeps == 1 )
                {
                    numKeep+= 1;
                    for( int m=0; m<numKeep; m++)
                    {
                    }
                }
            }
        }
    }
    
    void introduction()
    {
        cout << "*     YAZZIE     *" << endl;
        cout << endl;
        cout << " Welcome to the game of Yahtzee! \n";
        cout << " Where you roll dice and try to \n";
        cout << " accumulate as many points as \n";
        cout << " possible. Have fun playing!!!";
        cout << endl << endl;
    }
};
int main()
{
    char svar, answer;
    yazzie k;
    k.introduction();
    cout << "So ya'll wanna play???";
    cout << "j/n ?"<<endl;
    cin >> svar;
    if( svar == 'j')
    {
        cout << " Lets get going then!"<<endl;
        k.kast();
    }
    else if( svar != 'j')
    {
        return 0;
    }
    k.keep();
}


As you can see the keep part is a total mess, but thats where I'm stuck at the moment.
And btw, look at my name... I'm pretty casual and still learning :b
Line 10: That's not the best place to call srand(). srand() should be called once at the beginning of main.

Line 6,13: Using the same variable name is confusing.

Line 25: Your vector of dice rolls goes out of scope.

As for keeping dice, if the answer is no, set the element of the dice array to 0. Then when you re-roll the dice, reroll only those entries where the value of the die is 0. You need to fix your dice vector going out of scope before this will work.

You're inconsistent with your use of yes/no. In one place it is j/n, in others it is 1/2. Be consistent. Consider writing a function that prompts for y/n and returns a bool result.

Line 6: PlayerOne is an uninitialized variable (garbage). Line 41, you're using this uninitialized variable as a termination condition.

Line 35: numKeep is an uninitialized variable. Line 50, you're incrementing this uninitialized variable.

Edit:
I would be inclined to have three classes. A Game class (yazzie), a Player class and a Hand class. A Game has one or more Players. A player has a score and a Hand. A Hand has 5 dice.

You might search this forum for poker games. There are several good examples that should give you some good ideas about scoring a Yahtzee hand since it's very similar to scoring a poker hand.
Last edited on
Topic archived. No new replies allowed.