#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define PLAY 1
#define RESEARCH 2
#define STOP 0

int gamesWon, gamesPlayed=0;

int game()
{
    int choice, winningDoor, losingDoor, doorSwitch, a=1, different, points=0;

    winningDoor=rand()%3+1;


    do
    {
        printf("Choose a door |1| |2| |3|\n");
        scanf("%d", &choice);
    }
    while((choice>3)||(choice<1));

    while(a<4)
    {
        if((a!=choice)&&(a!=winningDoor))
            losingDoor=a;
        a=a+1;
    }

    a=1;

    while(a<4)
    {
        if((a!=losingDoor)&&(a!=choice))
            different=a;
        a=a+1;
    }

    printf("What if I told you door %d was not the winner? Would you like to switch?\n", losingDoor);

    do
    {
        printf("Enter (1) for yes and (0) for no\n");
        scanf("%d", &doorSwitch);
    }
    while((doorSwitch<0)||(doorSwitch>1));

    if (doorSwitch==1)
        choice=different;

    {
        if (choice==winningDoor)
        {
            printf("Congratulations you won!\n\n", winningDoor, choice);
            points=1;
        }
        if (choice!=winningDoor)
            printf("You didn't win sorry :(\n\n");
    }



    return(points);

}


int research()
{
    int choice, winningDoor, losingDoor, doorSwitch, a=1, different, num, c=0, right=0;
    float percentage;



    do
    {
        printf("how many simulations would you like to run?\n");
        scanf("%d", &num);
    }
    while(num<1);

    do
    {
    printf("Would you like to switch doors for the simulations? enter(1) for yes and (0) for no\n");
    scanf("%d", &doorSwitch);
    }
    while((doorSwitch<0)||(doorSwitch>1));

    for(c=0; num>c; c++)
    {
        winningDoor=rand()%3+1;
        choice=rand()%3+1;

        while(a<4)
        {
        if((a!=choice)&&(a!=winningDoor))
            losingDoor=a;
        a=a+1;
        }

        a=1;

        while(a<4)
        {
            if((a!=losingDoor)&&(a!=choice))
                different=a;
            a=a+1;
        }

        if(doorSwitch==1)
            choice=different;

        if(choice==winningDoor)
            ++right;
        a=1;
}
    printf("based on your choice, you picked the right door %d out of %d times\n", right, num);
    percentage=(float)right/num*100;
    printf("you won %f%% of the time\n", percentage);

    return 0;
}



int main()
{

srand(time(NULL));
   int option=1;

   while (option!=0)
   {
   printf("Welcome to the monty hall game, choose an option\n\n");
   printf("\tGames Won=%d\t Games Played=%d\n\n", gamesWon, gamesPlayed);
   printf("\tTo play the game enter %d\n", PLAY);
   printf("\tTo conduct research enter %d\n", RESEARCH);
   printf("\tTo exit the monty hall game enter %d\n", STOP);



   scanf("%d", &option);

   switch(option)
   {
       case PLAY:
            gamesWon=gamesWon+game();
            ++gamesPlayed;
            printf("You have chosen to play the game\n");
            break;

       case RESEARCH:
            research();
       break;

       case STOP:
       break;

       default:
       printf("Could you please choose an actual option\n");
   }
   }
return 0;
}
