Hi, I came with yet another program. I've done all the requirements my professor asked of me which is to write a Monty Hall program with research mode and game mode and also to have modules. I've did all that, but I'm not sure whether I can further improve this program (add more modules, get rid of stuff, etc.) or not.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int WINNING (int win);
float PERCENTAGE (float win,float playedgames);
int RANDOM ();
int main (void)
{
int choice, choice2, Ngames, Ngames2,chosendoor,chosendoor2;
int gamesplayed,gamesplayed2,winningdoor,winningdoor2,revealeddoor;
int switcheddoor, revealeddoor2,stayeddoor, doorchoice,winningdoor3;
int revealeddoor3, choice3, switcheddoor2;
float percent,percent2;
float win = 0,win2 = 0;
int gamesplayed3 = 0,win3 = 0;
srand(time(NULL)); //randomizes based on time
while (choice!=3)
{
printf ("This programs lets you either play or research the Monty Hall game.\n");
printf ("Please choose an option.\n(enter the integer near the option)\n1. Research mode\n2. Game mode\n3. Exit\n\n");
scanf ("%d", &choice);
switch(choice)
{
case 1:
printf ("\nYou have chosen research mode.\n");
printf ("Please indicate whether you will always switch doors or never switch doors.\n");
printf ("1. Always switch doors\n2. Never switch doors\n\n");
scanf ("%d", &choice2);
switch (choice2)
{
case 1:
printf ("\nYou have chosen to always switch doors.\n");
printf ("How many games would you like to play?\n\n");
scanf ("%d", &Ngames);
for (gamesplayed = 0;gamesplayed <= Ngames;gamesplayed++)
{
winningdoor = RANDOM();
chosendoor = 1;
do
{
revealeddoor = RANDOM();
} while (revealeddoor == chosendoor || revealeddoor == winningdoor);
do
{
switcheddoor = RANDOM();
} while (switcheddoor == chosendoor || switcheddoor == revealeddoor);
if (switcheddoor == winningdoor)
{
win = WINNING(win);
}
}
percent = PERCENTAGE(win,gamesplayed);
printf ("\nYou have chosen the winning door %f percent of the time.\n\n", percent);
win = 0;
break;
case 2:
printf ("\nYou have chosen to never switch doors.\n");
printf ("How many games would you like to play?\n\n");
scanf ("%d", &Ngames2);
for (gamesplayed2 = 0;gamesplayed2 <= Ngames2; gamesplayed2++)
{
winningdoor2 = RANDOM();
chosendoor2 = 1;
do
{
revealeddoor2 = RANDOM();
} while (revealeddoor2 == winningdoor2 || revealeddoor2 == chosendoor2);
do
{
stayeddoor = RANDOM();
} while (stayeddoor != chosendoor2);
if (stayeddoor == winningdoor2)
{
win2 = WINNING(win2);
}
}
percent2 = PERCENTAGE(win2,gamesplayed2);
printf ("\nYou chose the winning door %f percent of the time.\n\n", percent2);
win2 = 0;
break;
default:
printf ("You have not entered a correct value.");
break;
}
break;
case 2:
printf ("\nYou have chosen game mode.\n");
winningdoor3 = RANDOM();
printf ("Which door will you choose? (Door 1,2, or 3)\n\n");
scanf ("%d", &doorchoice);
if (doorchoice < 1 || doorchoice > 3)
{
printf ("That value is not a door.\n\n");
break;
}
do
{
revealeddoor3 = RANDOM();
} while (revealeddoor3 == doorchoice);
printf ("\nDoor %d was opened and there was no prize behind it.\n", revealeddoor3);
printf ("Would you like to:\n1. Switch doors?\n2. Stay with your current door?\n\n");
scanf ("%d", &choice3);
switch (choice3)
{
case 1:
printf ("\nYou have chosen to switch doors.\n\n");
do
{
switcheddoor2 = rand()%2 + 1;
} while (switcheddoor2 == doorchoice);
if (switcheddoor2 == winningdoor3)
{
printf ("Good choice! You have won!\n\n");
win3 = WINNING(win3);
}
else
{
printf ("Unfortunately, you lost.\n\n");
}
gamesplayed3 = gamesplayed3 + 1;
printf ("You have played a total of %d games and you won a total of %d games.\n", gamesplayed3, win3);
break;
case 2:
printf ("\nYou have decided to stay with your current door.\n");
if (doorchoice == winningdoor3)
{
printf ("Good choice! You have won!\n\n");
win3 = WINNING(win3);
}
else
{
printf ("Unfortunately, you lost.\n\n");
}
gamesplayed3 = gamesplayed3 + 1;
printf ("You have played a total of %d games and you won a total of %d games.\n\n", gamesplayed3, win3);
break;
default:
printf ("You have not entered a correct value.\n\n");
break;
}
break;
case 3:
exit(0);
default:
printf ("You have not entered a correct value.\n\n");
break;
}
}
return 0;
}
int WINNING (int win)
{
int newwin;
newwin = win + 1;
return (newwin);
}
float PERCENTAGE (float win,float playedgames)
{
float percentage;
percentage = (win/playedgames)*100;
return (percentage);
}
int RANDOM ()
{
int random;
random = rand()%3;
return (random);
}
I'm also worried that when I play game mode, I lose a bit too much when I switch doors, but I see no problem in the code.