-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridgeGame.cpp
More file actions
161 lines (149 loc) · 4.33 KB
/
Copy pathBridgeGame.cpp
File metadata and controls
161 lines (149 loc) · 4.33 KB
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
#include <iostream>
#include <vector>
#include <numeric>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
/* Return true 1/2 of the time */
bool fiftyPercent() {
if (rand() % 2 != 0) {
return(true);
}
else {
return(false);
}
}
/* Return true 2/3 of the time */
bool twoThirds() {
if (rand() % 3 != 0) {
return(true);
}
else {
return(false);
}
}
/* Return true 1/3 of the time */
bool oneThird() {
if (rand() % 3 == 0) {
return(true);
}
else {
return(false);
}
}
/* Run simulation with a probability of 1/2 */
void runFirstSimulation() {
vector<int> results; // Store the results of each simulation
int trialNumber = 0; // What section (step) the simulation is on
int totalSteps = 18; // How many steps are in the bridge
int success = 0; // How many times a player steps on a green tile
int failure = 0; // How many times a player steps on a red tile
int repeat = 10000; // Run the simulation this many times
for (int x = 0; x < repeat; x++) {
while (trialNumber != totalSteps) {
if (fiftyPercent()) {
success = success + 1;
}
else {
failure = failure + 1;
}
trialNumber++;
}
cout << "Total successes: " << success << endl;
cout << "Total failures: " << failure << endl;
results.push_back(failure);
success = 0;
failure = 0;
trialNumber = 0;
}
float average = (float)(accumulate(results.begin(), results.end(), 0)) / (float)(repeat);
cout << "Average number of players needed to get across: " << average << endl;
}
/* Run simulation with a probability of 2/3 */
void runSecondSimulation() {
vector<int> results; // Store the results of each simulation
int trialNumber = 0; // What section (step) the simulation is on
int totalSteps = 18; // How many steps are in the bridge
int success = 0; // How many times a player steps on a green tile
int failure = 0; // How many times a player steps on a red tile
int repeat = 10000; // Run the simulation this many times
for (int x = 0; x < repeat; x++) {
while (trialNumber != totalSteps) {
if (twoThirds()) {
success = success + 1;
}
else {
failure = failure + 1;
}
trialNumber++;
}
cout << "Total successes: " << success << endl;
cout << "Total failures: " << failure << endl;
results.push_back(failure);
success = 0;
failure = 0;
trialNumber = 0;
}
float average = (float)(accumulate(results.begin(), results.end(), 0)) / (float)(repeat);
cout << "Average number of players needed to get across: " << average << endl;
}
/* Run simulation with a probability of 1/3 */
void runThirdSimulation() {
vector<int> results; // Store the results of each simulation
int trialNumber = 0; // What section (step) the simulation is on
int totalSteps = 18; // How many steps are in the bridge
int success = 0; // How many times a player steps on a green tile
int failure = 0; // How many times a player steps on a red tile
int repeat = 10000; // Run the simulation this many times
for (int x = 0; x < repeat; x++) {
while (trialNumber != totalSteps) {
if (oneThird()) {
success = success + 1;
}
else {
failure = failure + 1;
}
trialNumber++;
}
cout << "Total successes: " << success << endl;
cout << "Total failures: " << failure << endl;
results.push_back(failure);
success = 0;
failure = 0;
trialNumber = 0;
}
float average = (float)(accumulate(results.begin(), results.end(), 0)) / (float)(repeat);
cout << "Average number of players needed to get across: " << average << endl;
}
/* Driver code */
int main() {
srand(time(0));
int userInput = 10;
while (userInput != 4) {
cout << "Choose an option: \n"
<< "1 - Simulation 1\n"
<< "2 - Simulation 2\n"
<< "3 - Simulation 3\n"
<< "4 - End program\n";
cin >> userInput;
while (!cin) { // Make sure the input is not a string
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid option, please enter an integer\n";
cin >> userInput;
}
if (userInput == 1) {
runFirstSimulation();
}
else if (userInput == 2) {
runSecondSimulation();
}
else if (userInput == 3) {
runThirdSimulation();
}
else if (userInput != 4) {
cout << "Invalid option. Please enter an integer between 1 and 4\n";
}
}
}