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
|
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
// Function Prototypes
int DrawNum (int);
int Stay(int, int, int); // Stay strategy
int Switch(int, int, int); // Switch strategy
int Guess(int, int, int); // 50-50 strategy
int main()
{
// Variables
unsigned seed;
int goat1, goat2; // The two goats behind the doors
double staytotal = 0; // Holds the total amount of successes for each method
double switchtotal = 0;
double guesstotal = 0;
double stayratio = 0; // Hold the Win/Loss ratio for each method
double switchratio = 0;
double guessratio = 0;
int temp = 0;
const int MAX = 3;
// Initialize RNG
seed = static_cast<unsigned>(time(NULL));
srand (seed);
// Herding the goats...
goat1 = DrawNum(MAX);
goat2 = DrawNum(MAX);
while (goat1 != goat2)
{
goat2 = DrawNum(MAX);
}
// Picking A door...
int choice;
choice = DrawNum(MAX);
while (choice != goat1)
{
choice = DrawNum(MAX);
}
// 1,000 Attempts of Stay Method
cout << "-------------------------- The Stay Method --------------------------" << endl;
cout << "The 'Stay' Method is when the contestant stays with their choice even\n"
<< "after the host reveals the first goat. For example, if the contestant\n"
<< "picks Door #1, and the host reveals a goat in Door #2, the contestant\n"
<< "will still choose Door #1.\n";
for(int i = 0; i <= 1000; i++)
{
Stay(goat1, goat2, choice);
temp = Stay(goat1, goat2, choice);
staytotal = staytotal + temp;
}
stayratio = staytotal / 1000;
cout << "Games Played: 1000 ";
cout << "Games Won: " << staytotal << " ";
cout << "Win/Loss Ratio: " << stayratio << endl;
// 1,000 Attempts of Switch Method
cout << "\n-------------------------- The Switch Method -------------------------" << endl;
cout << "The 'Switch' Method is when the contestant switches their choice after\n"
<< "the host reveals the first goat. For example, if the contestant picks\n"
<< "Door #1, and the host reveals the goat in Door #2, the contestant now\n"
<< "switches their choice to Door #3.\n";
for (int j = 0; j <= 1000; j++)
{
Switch(goat1, goat2, choice);
temp = Switch(goat1, goat2, choice);
switchtotal = switchtotal + temp;
}
switchratio = switchtotal / 1000;
cout << "Games Played: 1000 ";
cout << "Games Won: " << switchtotal << " ";
cout << "Win/Loss Ratio: " << switchratio << endl;
// 1,000 Attempts of Guess Method
cout << "\n------------------------- The Guess Method -------------------------" << endl;
cout << "The 'Guess' Method is when the contest goes with a 50/50 guess on\n"
<< "which door to open. For example, if the contestant picks Door #1,\n"
<< "and the host picks Door #2, the contestant would now take a 50/50\n"
<< "guess on which door to open, which can either be to stick with Door\n"
<< "#1 or to switch to Door #2.\n";
for (int k = 0; k <= 1000; k++)
{
Guess(goat1, goat2, choice);
temp = Guess(goat1, goat2, choice);
guesstotal = guesstotal + temp;
}
guessratio = guesstotal / 1000;
cout << "Games Played: 1000 ";
cout << "Games Won: " << guesstotal << " ";
cout << "Win/Loss Ratio: " << guessratio << endl;
}
int DrawNum (int max)
{
double x = RAND_MAX + 1.0;
int y;
y = static_cast<int> (1 + rand() * (max / x));
return (y);
}
int Stay(int goat1, int goat2, int choice)
{
int success = 1;
int failure = 0;
// Stay Method
if (choice != goat2)
{
return success;
}
else if (choice == goat2)
{
return failure;
}
}
int Switch(int goat1, int goat2, int choice)
{
int success = 1;
int failure = 0;
// Switch Method
if (choice == 1)
{
if (goat1 == 2)
{
choice = 3;
}
else if (goat1 == 3)
{
choice = 2;
}
}
else if (choice == 2)
{
if (goat1 == 1)
{
choice = 3;
}
else if (goat1 == 3)
{
choice = 1;
}
}
else if (choice == 3)
{
if (goat1 == 1)
{
choice = 2;
}
else if (goat1 == 2)
{
choice = 1;
}
}
if (choice != goat2)
{
return success;
}
else if (choice == goat2)
{
return failure;
}
}
int Guess(int goat1, int goat2, int choice)
{
int success = 1;
int failure = 0;
int guess = 0;
// Guess Method
guess = DrawNum(2);
if (guess == 1)
{
if (choice == 1)
{
if (goat1 == 2)
{
choice = 3;
}
else if (goat1 == 3)
{
choice = 2;
}
}
else if (choice == 2)
{
if (goat1 == 1)
{
choice = 3;
}
else if (goat1 == 3)
{
choice = 1;
}
}
else if (choice == 3)
{
if (goat1 == 1)
{
choice = 2;
}
else if (goat1 == 2)
{
choice = 1;
}
}
if (choice != goat2)
{
return success;
}
else if (choice == goat2)
{
return failure;
}
}
else if (guess == 2)
{
if (choice != goat2)
{
return success;
}
else if (choice == goat2)
{
return failure;
}
}
}
|