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!