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
|
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int prize, door_picked, door_opened, door_to_switch, finally_picked_door;
int num_game_played=0;
int num_game_won=0;
float num_won_simulation=0;
float num_simulation=0;
int assigned_door (int x int y);
int final_picked_door (int a, int b, int c);
void game_mode();
void research_mode();
void menu();
int main()
{
/*Generate a seed for generating random numbers in the program*/
srand(time(NULL));
printf("Welcome to the Monty Hall.\n");
menu();
return 0;
}
void game_mode ()
{ int switch_or_not;
/*Prompt the user to pick a door*/
printf("Please enter the door number you want to choose.\nDoor 1 Door 2 Door 3\n");
scanf("%d", &door_picked);
/*Put the prize randomly behind a door 1-3*/
prize=rand()%3+1;
/*Monty Hall opens a door and asks the user if he/she wants to swich*/
door_opened=assign_door (prize, picked_door);
printf("Monty Hall just opened Door %d, and there is no prize behind it.\n Would you like to pick another door? Press 1 for yes; 0 for no.\n", door_opened);
scanf("%d", &switch_or_not);
/*What to do after the user has decided to switch or not: */
door_to_switch = assign_door (picked_door, door_opened);
finally_picked_door=final_picked_door(switch_or_not, picked_door, door_to_switch); /*Assign finally_picked_door to door_to_switch*/
if(finally_picked_door==prize) /*Determine if the user has won and inform the user*/
{
num_game_won++; /*Count the number of wins*/
printf("The prize is behind the door you picked. You have won the game.\n");
}
else
printf("the prize is behind Door %d. You did not win.\n", prize);
num_game_played++; /*Count the number of games played*/
/*Inform the user how many game have been played and how many were won*/
printf("You have played the game %d times and won %d times.\n", num_game_played, num_game_won);
return;
}
void research()
{
int N,switch_in_research;
float chance_of_win=num_won_simulation*100.00/num_simulation;
num_won_simulation=0.0;
num_simulation=0.0;
/*Ask the user how many rounds to simulate*/
printf("Enter the number of game you want to simulate.\n");
scanf("%d", &N);
/*Ask the user what strategy to use*/
printf("Do you want to switch after Monty Hall opens a door? Yes-press 1. No-press 0.\n");
scanf("%d", &switch_in_research);
while(N>num_simulation)
{
prize=rand()%3+1;
door_picked=1;
door_opened=assign_door (prize, picked_door);
door_to_switch = assign_door (picked_door, door_opened);
finally_picked_door=final_picked_door(switch_in_research, picked_door, door_to_switch); /*Assign finally_picked_door to door_to_switch*/
if(finally_picked_door == prize)
num_won_simulation++;
}
if (switch_in_research==1) /*The user decided the switch*/
printf ("The chance of winning if you switch is %f%%\n", chance_of_win);
else/*The user decided not to switch*/
printf ("The chance of winning if you don't switch is %f%%\n", chance_of_win);
return;
}
void menu()
{ int user_choice, while_controller1, while_controller2;
while_controller1=1;
while(while_controller1)
{
printf("You have the following three options:\n1. Game Mode\n2. Research Mode\n3. Exit\n Enter the option you want(1-3).\n");
while_controller2=1;
while(while_controller2)
{
scanf("%d", &user_choice);
switch(user_choice)
{
case 1:
{
game_mode();
while_controller2=0;
break;
}
case 2:
{
research();
while_controller2=0;
break;
}
case 3:
{
while_controller1=0;
while_controller2=0;
break;
}
default:
{
printf("Please enter a valid option");
break;
}
}
}
}
return;
}
/*This function designate the door to be opened by Monty Hall and the door to switch to if the user chooses so*/
int assigned_door (int x int y)
{
int chosen_door=0;
do
{
chosen_door++;
}
while ((chosen_door==x) || (chosen_door==y) ) /*The program tries doors 1-3 until the assigned door is equal to neither of the doors represented by x and y*/
return chosen_door;
}
/*This function designate the door eventually picked by the user after he/she has decided to switch or not*/
int final_picked_door (int a, int b, int c) /*In the program, a will be replaced by the choice of the user on wheter to switch or not; b will be replaced by picked_door; c will be replaced by door_to_switch*/
{
if (a==1) /*The user has decided to switch*/
{
b=c; /*The finally picked door will be the door swtiched to*/
}
else /*The user has decided not to switch*/
{
b=b; /*The finally picked door is still the initially picked door*/
}
return b;
}
|