-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonty Hall.c
209 lines (177 loc) · 5.87 KB
/
Monty Hall.c
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
/*
Written By D-VR
Copyright 2018 GPL-2.0 license
Info About the Monty Hall Problem:
https://en.wikipedia.org/wiki/Monty_Hall_problem
Become a Guest at a Tv Show and try to win your dream prize..
Oh and test the Monty Hall Problem
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
char validInput[] = "?y2n3r1q";
char getUserInput();
int opselect = 1;
int door = 0;
int choice123;
int wincount = 0;
int gamecount = 0;
float percentage = 0;
char newFrame[] = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
int main(int argc, char const *argv[])
{
srand(time(NULL));
for (;;)
{
int windoor;
int restart = 0;
char input = '0';
int rndint = 0;
choice123 = 1;
door = 0;
gamecount+=1;
int userdoor;
int HostDoor;
int choicedoor;
char gamemessage[] = "\n\nChoose a door!\n(Enter 1, 2 or 3)\n";
char winmessage[60];
rndint = rand() % 3;
windoor = rndint+1;
printf("%sWelcome to the Monty Hall Problem!\nYou are a Guest in a Tv Show\nYour task is it to Choose a door of three, behind which lies a prize\nThe Host then Opens a door\nYou are given the Option to switch", newFrame);
for (;;)
{
if(door == 1){
opselect = 3;
}
else
{
printf("%s", gamemessage);
input = getUserInput();
}
switch (opselect)
{
case 1:
printf("%sInput what is asked\nq to quit\nr to restart\n? for help\n", newFrame);
break;
case 2:
printf("quitting, thanks for playing\n");
return 0;
break;
case 3:
printf("%sYou Chose Door Nr. %d\n", newFrame, userdoor);
if((userdoor + windoor) == 4 && (userdoor != windoor)){
HostDoor = 2;
}
else
if((userdoor + windoor) == 5){
HostDoor = 1;
}
else
if((userdoor + windoor) == 3){
HostDoor = 3;
}
else
for(;;){
rndint = rand() % 3;
rndint+=1;
if(rndint != windoor){
HostDoor = rndint;
break;
}
}
printf("I have opened Door Nr. %d for you.. it was empty\n", HostDoor);
printf("Do you want to switch your Door? y/n\n");
char doorchoice = 'y';
doorchoice = getUserInput();
if(doorchoice == 'y'){
choicedoor = 6 - HostDoor - userdoor;
printf("%sYour door: %d, Winning Door: %d\n", newFrame, choicedoor, windoor);
if(choicedoor == windoor){
strcpy(winmessage, "Congrats you won");
wincount+=1;
}
else
strcpy(winmessage, "You have lost");
}
else
if(userdoor == windoor){
strcpy(winmessage, "Congrats you won");
wincount+=1;
}
else
strcpy(winmessage, "You have lost");
percentage = wincount / gamecount;
percentage*=100;
printf("Total Games: %d\n Total Wins: %d\nWin percentage: %.2f\n", gamecount, wincount, percentage);
printf("Do you want to play again? y/n\n\n");
doorchoice = getUserInput();
if(doorchoice == 'y'){
restart = 1;
}
else
return 0;
break;
case 4:
restart = 1;
break;
case 5:
door = 1;
choice123 = 0;
userdoor = input - '0';
break;
}
if(restart == 1){
break;
printf("%s", newFrame);
}
}
}
return 0;
}
char getUserInput()
{
for (;;)
{
char Input;
char line[256];
if (fgets(line, sizeof line, stdin) == NULL) {
printf("Input error.\n");
exit(1);
}
Input = line[0];
for (int i = -1; i < 8; ++i)
{
if (Input == validInput[i])
{
if(Input == '?'){
opselect = 1;
break;
}
else
if(Input == 'q'){
opselect = 2;
break;
}
else
if((Input == 'n' || Input == 'y') && (door == 1)){
opselect = 3;
return Input;
}else
if(Input == 'r'){
opselect = 4;
break;
}else
if((Input == '1' || Input == '2' || Input == '3') && choice123 == 1){
opselect = 5;
return Input;
}
}
else
if(i == 7){
printf("%sInvalid Input, do '?' for help.\n\n\n", newFrame);
}
}
}
}