This program uses functions to simulate Plinko. Our menu function should error check but doesnt do it when a string is entered. So how would I override a string so that it continues to loop out our prompt?
Input:
-13
junk
-1
234
0
Expected Output:
Welcome to the Plinko simulator!
Menu: Please select one of the following options:
0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot
3 - Drop multiple chips into each slot
Enter your selection now:
Invalid selection. Please enter 0, 1, 2 or 3
Enter your selection now:
Invalid selection. Please enter 0, 1, 2 or 3
Enter your selection now:
Invalid selection. Please enter 0, 1, 2 or 3
Enter your selection now:
Invalid selection. Please enter 0, 1, 2 or 3
Enter your selection now:
Goodbye!
Our Output:
Welcome to the Plinko simulator!
Menu: Please select one of the following options:
0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot
3 - Drop multiple chips into each slot
Enter your selection now:
Invalid selection. Please enter 0, 1, 2 or 3
Goodbye!
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
const double PRIZE_0 = 100.00;
const double PRIZE_1 = 500.00;
const double PRIZE_2 = 1000.00;
const double PRIZE_3 = 0.00;
const double PRIZE_4 = 10000.00;
const double PRIZE_5 = 0.00;
const double PRIZE_6 = 1000.00;
const double PRIZE_7 = 500.00;
const double PRIZE_8 = 100.00;
const double CHANGE_LEFT_RIGHT = 0.5;
const int ROWS_OF_PEGS = 12;
const int PRECISION = 1;
const int SLOT_0 = 0;
const int SLOT_1 = 1;
const int SLOT_2 = 2;
const int SLOT_3 = 3;
const int SLOT_4 = 4;
const int SLOT_5 = 5;
const int SLOT_6 = 6;
const int SLOT_7 = 7;
const int SLOT_8 = 8;
//MENU FUNCTION
void OutputMenu() {
cout << "Menu: Please select one of the following options:" << endl << endl;
cout << "0 - Quit the program" << endl;
cout << "1 - Drop a single chip into one slot" << endl;
cout << "2 - Drop multiple chips into one slot" << endl;
cout << "3 - Drop multiple chips into each slot" << endl << endl;
cout << "Enter your selection now: " << endl;
//cin >> menuChoice;
}
bool menuError(int& menuChoice) {
//int menuChoice = 0;
while (menuChoice > 3 || menuChoice < 0)
{
cout << "Invalid selection. Please enter 0, 1, 2 or 3" << endl; //invalid selection change
cin >> menuChoice;
}
return false;
}
/*
bool MenuError(int menuChoice) {
do {
//(menuChoice < 0 || menuChoice > 3)
cout << "Enter your selection now: " << endl;
cin >> menuChoice;
if(menuChoice < 0 || menuChoice > 3) {
cout << "Invalid selection. Please enter 0, 1, 2 or 3" << endl;
return true;
}
else {
return false;
}
//cout << "Enter your selection now: " << endl;
} while(menuChoice < 0 || menuChoice > 3);
}
*/
/*void SlotNumberError(int slotNumber) {
double slotNumberDouble = 0.0;
double chipLocation = 0.0;
double totalWinnings = 0.0;
if (slotNumber > 8 || slotNumber < 0) {
cout << endl;
cout << "WRONG: FIXME!" << endl;
//break;
}
}*/
// void OutputPath (double chipLocation);
double DropChip(int chipLocation, bool outputPath);
double DropMultipleChips(int chipLocation, int numChips);
double ComputeWinnings(int chipLocation);
//REWARDS FUNCTION
double ComputeWinnings(int chipLocation) {
double winnings = 0;
switch (chipLocation) { // The prize won by user
case SLOT_0:
case SLOT_8:
winnings = PRIZE_0;
break;
case SLOT_1:
case SLOT_7:
winnings = PRIZE_1;
break;
case SLOT_2:
case SLOT_6:
winnings = PRIZE_2;
break;
case SLOT_3:
case SLOT_5:
winnings = PRIZE_3;
break;
case SLOT_4:
winnings = PRIZE_4;
break;
return winnings;
}
}
//MAIN FUNCTION
int main() {
srand(42);
double slotNumberDouble = 0.0;
double chipLocation = 0.0;
double chipLocation2 = 0.0;
double totalWinnings = 0.0;
double averageWinningsPerChip = 0.0;
int menuChoice = 0;
int slotNumber = 0;
int randomNumber = 0;
int finalLocationChip1 = 0;
int finalLocationChip2 = 0;
int chipNumber = 0;
int slotNumber2 = 0;
cout << "Welcome to the Plinko simulator!" << endl << endl;
do { // The menu options
do {
OutputMenu();
cin >> menuChoice;
//MenuError(menuChoice);
} while (menuError(menuChoice));
switch (menuChoice) {
case 1: // If user enters menu option 1
cout << "*** Drop single chip ***" << endl << endl;
cout << "Which slot do you want to drop the chip(s) in (0-8)? ";
cin >> slotNumber;
//SlotNumberError(slotNumber);
slotNumberDouble = slotNumber;
cout << fixed << setprecision(1) << endl;
//cout << "*** Dropping chip into slot " << slotNumber << " ***";
cout << endl;
//cout << "Path: [" << slotNumberDouble;
chipLocation = slotNumber;
totalWinnings = DropChip(chipLocation, true);
break;
case 2: // If menu option 2 is chosen
cout << "*** Drop multiple chips ***" << endl << endl;
cout << "How many chips do you want to drop (>0)? " << endl;
cin >> chipNumber;
if (chipNumber < 1) {
cout << "Invalid number of chips." << endl << endl;
break;
}
else {
cout << "Which slot do you want to drop the chip(s) in (0-8)? " << endl;
cin >> slotNumber2;
if (slotNumber2 > 8 || slotNumber2 < 0) {
cout << "Invalid slot.";
cout << endl << endl;
break;
}
else {
totalWinnings = 0;
for (int i = 0; i < chipNumber; i++) { // The path of the multiple chips
chipLocation = slotNumber2;
totalWinnings += DropChip(chipLocation, false);
}
cout << fixed << setprecision(2);
cout << "Total winnings on " << chipNumber << " chips: " << totalWinnings << endl;
averageWinningsPerChip = totalWinnings / chipNumber;
cout << "Average winnings per chip: " << averageWinningsPerChip << endl << endl;
}
}
break;
/* case 3: //multiple chips in multiple slots
cout << "3 - Drop multiple chips into each slot" << */
case 0: // If menu option 0 is chosen
cout << "Goodbye!";
return 0;
break;
//default:
//cout << "Invalid selection. Please enter 0, 1 or 2" << endl << endl;
//break;
}
} while (true);
return 0;
}
double DropChip(int chipLocation, bool outputPath = false) {
double winnings = 0;
int randomNumber = 0;
double pathChipLocation = chipLocation;
string path = "";
if (outputPath) {
//cout << "[";
}
for (int i = 0; i < ROWS_OF_PEGS; i++) { // The path of the chip
if (pathChipLocation == 0) {
pathChipLocation += CHANGE_LEFT_RIGHT;
}
else if (pathChipLocation == 8) {
pathChipLocation -= CHANGE_LEFT_RIGHT;
}
else if (pathChipLocation > 0 || chipLocation < 8) {
randomNumber = rand() % 2;
if (randomNumber == 0) {
pathChipLocation -= CHANGE_LEFT_RIGHT;
}
else {
pathChipLocation += CHANGE_LEFT_RIGHT;
}
}
else {
pathChipLocation = pathChipLocation;
}
if (outputPath) {
//cout << " " << fixed << setprecision(PRECISION) << pathChipLocation;
}
}
if (outputPath) {
//cout << "]";
}
chipLocation = static_cast<int>(pathChipLocation);
switch (chipLocation) { // The prize won by user
case SLOT_0:
case SLOT_8:
winnings = PRIZE_0;
break;
case SLOT_1:
case SLOT_7:
winnings = PRIZE_1;
break;
case SLOT_2:
case SLOT_6:
winnings = PRIZE_2;
break;
case SLOT_3:
case SLOT_5:
winnings = PRIZE_3;
break;
case SLOT_4:
winnings = PRIZE_4;
break;
}
if (outputPath) {
cout << endl;
cout << "Winnings: $" << fixed << setprecision(2) << winnings << endl << endl;
}
return winnings;
}
|