Code:
#include <stdio.h>
#include <stdlib.h>
void game()
{
int door[3]={0,0,0};
int choice, i, newdoor, originaldoor, openeddoor, randomnumber;
if (originaldoor==1)
{
openeddoor=rand() % 3;
}
else
{
openeddoor=0;
}
printf("Door %d does not have the prize\n", openeddoor);
printf("Would you like to switch your door? 1 for yes 2 for no.\n");
scanf("%d" , &choice);
while ((choice<0) || (2<choice))
{
printf("You entered an invalid number\n");
break;
}
if (choice==1)
newdoor = choice;
else if (choice==2)
newdoor = originaldoor;
if (newdoor==1)
printf("You have won the prize!\n");
else
printf("You lose!\n");
}
int main(void)
{
int door[3]={0, 0, 0};
int x, prize, randomnumber, originaldoor, choice, newdoor, y;
prize=1;
while (1)
{
srand (time(NULL));
rand() % 3 + 1;
door[randomnumber] = 1;
printf("Which door do you pick 1, 2, or 3? 0 to exit.\n");
scanf("%d", &originaldoor);
game(originaldoor);
if ((0>=originaldoor) || (originaldoor>3))
{
break;
}
}
return(0);
}