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
|
#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();
int main() {
srand(time(NULL));
int total;
bool rollAgain;
bool singleRoll;
cout << "Welcome to the Yahtzee challenge. See if you can roll 5 of a kind." << endl;
do {
total = rollDice();
display(total);
rollAgain = askToReroll();
} while (rollAgain == true);
do {
int rollSingle(int t, int u, int v, int w, int x);
display(total);
singleRoll = singleReroll();
} while (rollAgain == false && singleRoll == true);
// 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 << t << endl;
die[0] = t;
int u = 0;
u += rand() % 6 + 1;
cout << u << endl;
die[1] = u;
int v = 0;
v += rand() % 6 + 1;
cout << v << endl;
die[2] = v;
int w = 0;
w += rand() % 6 + 1;
cout << w << endl;
die[3] = w;
int x = 0;
x += rand() % 6 + 1;
cout << x << endl;
die[4] = x;
int y = t+u+v+w+x;
return y;
}
int rollSingle(int t, int u, int v, int w, int x) {
int die[5] = {t, u, v, w, x};
int response1;
cout <<"Roll the first number again? Type 1 to roll again, type anything else to decline." <<endl;
cin >> response1;
if (response1 = 1) {
int t = 0;
t += rand() % 6 + 1;
cout << t << endl;
die[0] = t;
}
else {
cout << t << endl;
}
int response2;
cout <<"Roll the second number again? Type 2 to roll again, type anything else to decline." <<endl;
cin >> response2;
if (response2 = 2) {
int u = 0;
u += rand() % 6 + 1;
cout << u << endl;
die[1] = u;
}
else {
cout << u << endl;
}
int response3;
cout <<"Roll the third number again? Type 3 to roll again, type anything else to decline." <<endl;
cin >> response3;
if (response3 = 3) {
int v = 0;
v += rand() % 6 + 1;
cout << v << endl;
die[2] = v;
}
else {
cout << v << endl;
}
int response4;
cout <<"Roll the fourth number again? Type 4 to roll again, anything else to decline." <<endl;
cin >> response4;
if (response4 = 4) {
int w = 0;
w += rand() % 6 + 1;
cout << w << endl;
die[3] = w;
}
else {
cout << w << endl;
}
int response5;
cout <<"Roll the fifth number again? Type 5 to roll again, anything else to decline." <<endl;
cin >> response5;
if (response5 = 5) {
int x = 0;
x += rand() % 6 + 1;
cout << x << endl;
die[4] = x;
}
else {
cout << x << endl;
}
int y = t+u+v+w+x;
return 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 << "Roll one number instead? (y/n): ";
cin >> yesno;
if (yesno == 'y')
return true;
return false;
}
| |