Help with Yahtzee program please

Well, so far I have just threw this thing together because I wasn't expecting to have to turn it in for another 2 weeks. I am a beginner at this, but have a fairly decent understanding of how it all works. On this line:

 
keepDice.push_back[keeper];


I get an error stating a pointer to a bound function may only be used to call a function, but, it's not a function. I know arrays and vectors act as a "pointer" to elements in the array/vector. But I'm having trouble finding my issue.

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
#include "stdafx.h"
#include <iostream>
#include <istream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace std;

void introduction()
{
	cout << "********************" << endl;
	cout << "*     YAHTZEE      *" << endl;
	cout << "********************" << 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()
{
	introduction();//Was going to do this all in functions, but had to cut back because of the complicity of it all.


	vector<int> rollDice;
	vector<int> keepDice(5);
	const int NUMROLLS = 3;
	int iRand;
	int answer = 0;
	int keeps = 0;
	int numKeep = 0;
	int keeper = 0;

	srand(time(NULL));

	for( int i=0; i<5; i++){
		iRand = (rand() % 6 +1 );
		rollDice.push_back(iRand);
	}

	cout << "The dice rolled were: " << endl;
	for( int j=0; j<5; j++ ){
		cout << rollDice[j] << endl;
	}

	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<5; k++ )
		{
			cout << "Would you like to keep dice " << k+1 << "? " << endl;
			cout << " Press '1' for yes or '2' for no: " << endl;
			cin >> keeps;

			if( keeps == 1 )
			{
				numKeep+= 1;
				for( int m=0; m<numKeep; m++)
				{

					keeper = rollDice[k];
					keepDice.push_back[keeper]; //This is the error!

				}
				
			}
			keeper = 0;
		}
	}
	
	//just an output to see if vector stored correctly
	for( int l=0; l<numKeep; l++ ){
		cout << keepDice[l] << endl;
	}
	
	

	system("pause");
	return 0;
}


Any help, thoughts, opinions, or suggestions are much appreciated!!!
What exactly are you trying to do there?
You just used the wrong type of braces, that's all.
As for other suggestions: you should avoid repetitive literals and magic numbers in general. In particular, the recurring 5 should get its own named constant. The other thing is that you should keep variables as local as possible. You shouldn't declare a bunch of variables at the beginning when most of them aren't used until much later. Declare them at the point of first use.
Well, as stated, it is a Yahtzee program. So, I'm trying to accomplish the simplest version of Yahtzee ever, at least coding-wise.

Thanks a lot Athar, I'll take all the advice I can get.

Any other suggestions for this sloppy code?

**EDIT**

Yea, I'm doing something wrong here, because though that fixed the error and it stores the correct amount of elements, it sets them all to zero. This coding stuff is fun, but certainly not easy.
Last edited on
Bump.
It looks like that because you create keepDice with an initial 5 elements, which are all set to zero and you're just appending some more values after that.
Ahhh, I see. Thanks a lot Athar.

This is due tomorrow at noon and I'm almost positive I'm not going to get it all done.

I am pretty fucked on this one.
OH MY GOD!!!

This is why you don't code under pressure, let alone a time limit.

PLEASE HELP ME!!!!

It does actually work, well kind of. And yes, I know, it is terrible practice to code like this, but I wouldn't under normal conditions.

This, my friends, is a Frankenstein...

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "stdafx.h"
#include <iostream>
#include <istream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace std;

void introduction()
{
	cout << "********************" << endl;
	cout << "*     YAHTZEE      *" << endl;
	cout << "********************" << 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()
{
	introduction();//Was going to do this all in functions, but had to cut back because of the complicity of it all.


	vector<int> rollDice;
	vector<int> rollTwo;
	vector<int> rollThree;
	const int NUMROLLS = 3;
	const int NUMDICE = 5;
	int iRand;
	int answer = 0;
	int keeps = 0;
	int keeper = 0;
	int numKeep = 0;
	int score = 0;

	srand(time(NULL));

	for( int i=0; i<NUMDICE; i++){
		iRand = (rand() % 6 +1 );
		rollDice.push_back(iRand);
	}

	cout << "The dice rolled were: \n" << endl;
	for( int j=0; j<NUMDICE; j++ ){
		cout << rollDice[j] << ", ";
	}
	cout << endl << endl;

	cout << " Is there any dice that you would like to keep? " << endl;
	cout << " Press '1' for yes or '2' for no: " << endl;
	cin >> answer;
	cout << endl;
	
	if( answer == 1 )
	{
		for( int k=0; k<NUMDICE; 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++)
				{
					keeper = rollDice[k];
					rollTwo.push_back(keeper); 	
				}
				
			}
			keeper = 0;
		}
	}
	else
	{
		cout << " Congratulations!!! You rolled a " << rollDice[0] << " " << rollDice[1] << " " << rollDice[2] << " " << rollDice[3]
		<< " and a "<< rollDice[4] << endl;

		score = rollDice[0] + rollDice[1] + rollDice[2] + rollDice[3] + rollDice[4];
		cout << " For a total score of " << score << endl;
		
		if( rollDice[0] == rollDice[1] == rollDice[2] == rollDice[3] == rollDice[4] )
		{
			score = rollDice[0] + rollDice[1] + rollDice[2] + rollDice[3] + rollDice[4] + 50;
			cout << " You got a YAHTZEE!!! Congratulations!!! " << endl;
			cout << " For a total score of " << score << endl;
		}
		system("pause");
		return 0;
	}


	
	rollDice.clear();

	for( int n=0; n<5-numKeep; n++ )
	{
		iRand = (rand() % 6 +1 );
		rollDice.push_back(iRand);
	}

	cout << " Your new roll is " << endl;
	for( int l=0; l<numKeep; l++)
	{
		cout << rollTwo[l] << ", ";
	}

	for( int p=0; p<5-numKeep; p++ )
	{
		cout << rollDice[p] << ", ";
	}
	cout << endl;

	rollDice.clear();

	for( int z=0; z<numKeep; z++ )
	{
		rollThree.push_back(rollTwo[z]);
	}	
	for( int y=0; y<5-numKeep; y++)
	{			
		rollThree.push_back(rollTwo[y]);
	}
	
	numKeep = 0;

	cout << " Is there any dice that you would like to keep? " << endl;
	cout << " Press '1' for yes or '2' for no: " << endl;
	cin >> answer;
	cout << endl;
	
	if( answer == 1 )
	{
		for( int q=0; q<NUMDICE; q++ )
		{
			cout << "Would you like to keep dice " << q+1 << "? " << endl;
			cout << " Press '1' for yes or '2' for no: " << endl;
			cin >> keeps;
			cout << endl;

			if( keeps == 1 )
			{
				numKeep+= 1;
				for( int u=0; u<numKeep; u++)
				{
					keeper = rollThree[q];
					rollDice.push_back(keeper); 	
				}
				
			}
			keeper = 0;
		}
	}
	
	rollTwo.clear();
	
	for( int r=0; r<5-numKeep; r++ )
	{
		iRand = (rand() % 6 +1 );
		rollTwo.push_back(iRand);
	}

	cout << " Your new roll is " << endl;
	for( int s=0; s<numKeep; s++)
	{
		cout << rollDice[s] << ", ";
	}

	for( int t=0; t<5-numKeep; t++ )
	{
		cout << rollTwo[t] << ", ";
	}
	cout << endl;
	

	system("pause");
	return 0;
}
	


I believe it's just an off-by-1 error. I know it's a mess, but please help...

I have a HUGE headache.
Last edited on
BUMP
closed account (D80DSL3A)
What's off by one? Please describe the problem more carefully.
I'm not too sure, I've spent like 4 hours trying to figure it out.

The problem is:

Well, compile and run it. It works up until I try to replace rollThree with the new roll.
closed account (D80DSL3A)
It looks like the main problem is with how the dice are saved between rolls.
1
2
3
4
5
6
7
8
9
if( keeps == 1 )
{
	numKeep+= 1;
	for( int u=0; u<numKeep; u++)// why is this for loop here?
	{
		keeper = rollThree[q];
		rollDice.push_back(keeper); 	
	}				
}


Are you required to use vectors? They're cool but in this problem the # of elements is fixed at 5 so vectors add unneeded complexity (personal opinion).
You could simplify all this by using one array of integers for the 5 dice and a couple of functions to assign values to its elements.
Using int rollDice[5] = {0}; to replace all 3 vectors, this function could be used to roll all 5 dice:
1
2
3
4
5
6
7
8
9
10
11
void rollAll(int dice[], int size)
{
	for( int i=0; i<size; i++)
		dice[i] = rand() % 6 +1;	

	cout << "The dice rolled were: \n" << endl;
	for( int j=0; j<size; j++ )
		cout << dice[j] << ", ";
	cout << endl << endl;
	return;
}


This function could be used to query the user about saving dice:
1
2
3
4
5
6
7
8
9
10
11
bool anyToKeep()
{
	int answer = 0;
	cout << " Is there any dice that you would like to keep? " << endl;
	cout << " Press '1' for yes or '2' for no: " << endl;
	cin >> answer;
	cout << endl;
	
	if( answer == 1 )return true;
	return false;
}

And this function to save some dice and reroll the rest:
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
void saveDiceAndReRoll(int dice[], int size)
{
	int numKept = 0, k = 0;
	int savedDice[5] = {0};
	int keeps = 0;

	for(k=0; k<size; 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 )											
			savedDice[ numKept++ ] = dice[k];// store the saved dice here		
	}
	for(k=0; k<numKept; k++ )// copy the saved rolls
		dice[k] = savedDice[k];
	for(k=numKept; k<size; k++ )// add the new rolls
		dice[k] = rand() % 6 +1;
	cout << " Your new roll is " << endl;
	for( int l=0; l<5; l++)	
		cout << dice[l] << ", ";// show new roll	
	cout << endl;
	return;
}


Now, many of your variables are local to functions so that in main() it becomes this simple:
Variables needed:
1
2
3
int rollDice[5] = {0};
const int NUMDICE = 5;
int score = 0;


Code for initial dice roll:
1
2
// 1st roll
rollAll(rollDice, NUMDICE);

Code for other dice rolls:
1
2
3
4
if( anyToKeep() )
	saveDiceAndReRoll(rollDice, NUMDICE);
else
	rollAll(rollDice, NUMDICE);

The way you had it coded doesn't follow the actual game well though.
If the user opts to keep all 5 dice then no more rolls occur. The play should be scored after 3 rolls or when the user chooses to keep all 5 dice, whichever comes 1st.
Idea: Modify the saveDiceAndReRoll() to return the # of dice kept instead of a void result.
Test for a return value of 5 to see if the turn should end before the 3rd roll.

Hope this helps!
Topic archived. No new replies allowed.