I posted a Yahtzee program about two days ago and modified to work properly.
The program will roll 5 dice on command and then ask the user if they wish to reroll. Answering "y" makes the 5 dice reroll. However, Answering "n" will bring up a message asking if the user wants to roll each dice individually. Saying no to that terminates the program, but saying yes initiates a selective reroll and then loops the program back to ask if the user wants to reroll all 5 dice at once.
It is a project that has been coded to meet requirements, such as creating a file, using arrays, loops, and functions.
Understandably, it is hard to keep track of everything going on in this program. I can't really shrink it down. The code will compile as is.
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
#include <iostream>
#include <fstream> //for files
#include <stdlib.h> //for rand/srand
#include <time.h> //for time
using namespace std;
int rollDice();
int straight = 0; // A counter to track how many straights have been rolled
void display(int num);
bool askToReroll();
bool singleReroll();
bool response1();
bool response2();
bool response3();
bool response4();
bool response5();
int valueT;
int valueU;
int valueV;
int valueW;
int valueX;
int main() {
srand(time(NULL));
int total;
bool rollAgain;
bool singleRoll;
bool rollRepeat;
bool roll1;
bool roll2;
bool roll3;
bool roll4;
bool roll5;
cout << "Welcome to the Yahtzee challenge. See if you can roll 5 of a kind." << endl;
do{
do {
if (rollAgain == false && singleRoll == true) display(total);
else {
total = rollDice();
display(total); }
rollAgain = askToReroll();
} while (rollAgain == true);
singleRoll = singleReroll();
if (singleRoll == true) {
int t = valueT;
int u = valueU;
int v = valueV;
int w = valueW;
int x = valueX;
int rollSingle(int t, int u, int v, int w, int x);
int die[5] = {t, u, v, w, x};
roll1 = response1();
if (roll1 == true) {
int t = 0;
t += rand() % 6 + 1;
cout << "New 1st number: " << t << endl;
die[0] = t; }
else {
die[0] = valueT;
cout << "1st number unchanged: " << t << endl; }
roll2 = response2();
if (roll2 == true) {
int u = 0;
u += rand() % 6 + 1;
cout << "New 2nd number: " << u << endl;
die[1] = u; }
else {
die[1] = valueU;
cout << "2nd number unchanged: " << u << endl; }
roll3 = response3();
if (roll3 == true) {
int v = 0;
v += rand() % 6 + 1;
cout << "New 3rd number: " << v << endl;
die[2] = v; }
else {
die[2] = valueV;
cout << "3rd number unchanged: " << v << endl; }
roll4 = response4();
if (roll4 == true) {
int w = 0;
w += rand() % 6 + 1;
cout << "New 4th number: " << w << endl;
die[3] = w; }
else {
die[3] = valueW;
cout << "4th number unchanged: " << w << endl; }
roll5 = response5();
if (roll5 == true) {
int x = 0;
x += rand() % 6 + 1;
cout << "New 5th number: " << x << endl;
die[4] = x; }
else {
die[4] = valueX;
cout << "5th number unchanged: " << x << endl; }
int y = t+u+v+w+x;
total = y;
}
// feed the program back into the AskToReroll function
// if the single roll has been completed
// otherwise, make a file and terminate
// Creation of Yahtzee file
ofstream out("Yahtzee.txt");
out << "Thank you for taking the Yahtzee challenge." << endl;
out.close();
return 0;
}
int rollDice() {
int die[5] = {0};
int t = 0;
t += rand() % 6 + 1;
cout << "1st number: " << t << endl;
die[0] = t;
valueT = t;
int u = 0;
u += rand() % 6 + 1;
cout << "2nd number: " << u << endl;
die[1] = u;
valueU = u;
int v = 0;
v += rand() % 6 + 1;
cout << "3rd number: " << v << endl;
die[2] = v;
valueV = v;
int w = 0;
w += rand() % 6 + 1;
cout << "4th number: " << w << endl;
die[3] = w;
valueW = w;
int x = 0;
x += rand() % 6 + 1;
cout << "5th number: " << x << endl;
die[4] = x;
valueX = x;
int y = t+u+v+w+x;
return t, u, v, w, x, y;
}
void yahtzee(int t, int u, int v, int w, int x, int y) {
if (t==u && u==v && v==w && w==x && x==y)
cout <<"Yahtzee! " << endl;
int straight = straight + 1;
}
void display(int num) {
cout << "Your total is: " << num << endl;
cout << "You have rolled straights " << straight << " times." << endl;
}
bool askToReroll() {
char yesno;
cout << "Roll again? (y/n): ";
cin >> yesno;
if (yesno == 'y')
return true;
return false;
}
bool singleReroll() {
char yesno;
cout << "Pick the dice you wish to roll instead? (y/n): ";
cin >> yesno;
if (yesno == 'y')
return true;
return false;
if (singleReroll() == 0) {return 0; }
}
// 5 boolean functions to roll the dice separately
bool response1() {
char yesno;
cout << "Roll the 1st number again? (y/n): ";
cin >> yesno;
if (yesno == 'y')
return true;
return false;
}
bool response2() {
char yesno;
cout << "Roll the 2nd number again? (y/n): ";
cin >> yesno;
if (yesno == 'y')
return true;
return false;
}
bool response3() {
char yesno;
cout << "Roll the 3rd number again? (y/n): ";
cin >> yesno;
if (yesno == 'y')
return true;
return false;
bool response4() {
char yesno;
cout << "Roll the 4th number again? (y/n): ";
cin >> yesno;
if (yesno == 'y')
return true;
return false;
}
bool response5() {
char yesno;
cout << "Roll the 5th number again? (y/n): ";
cin >> yesno;
if (yesno == 'y')
return true;
return false;
}
| |
The problem begins if you try to roll the dice individually twice or more. One The values do not remain unchanged between rolls.
Say a user rolls 1, 2, 3, 4, 5, and then wants to reroll dice 3. Then they get 1, 2, 6, 4, 5.
However, asking to do another individual reroll somehow makes all the numbers change regardless of what the user wants to do. I am not sure where my error is.