#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void Game(void);
void Research(void);

int main()
{
    int x;
    printf("Please enter one of the following modes:\n1.Game Mode\n2.Research mode\n3.Exit\n");
    scanf("%d",&x);
    if (x==1)
        Game();
    if (x==2)
        Research();
    if (x==3)
       return 0;


}
void Game()
{
    int door[3],x,s;
    int static win=0, lost=0;
    srand((unsigned int)time(0));
    door[0]= rand()%2;
    if (door[0]==0)
    {
        door[1]=rand()%2;
        if (door[1]==0)
            door[2]=1;
        else
            door[2]=0;
    }
    else
    {
        door[1]=0;
        door[2]=0;
    }
    printf("Please choose your door 1,2 or 3\n");
    scanf("%d", &x);
    if ((x-1)==0)
    {
        if (door[1]==0)
        {
            printf("Door 2 is empty!");
            printf("You can choose to stay with the same door or switch, your choice of doors are 1 or 3\n");
            scanf("%d", &s);
            if (door[s-1]==1)
            {
                printf("You won!\n");
                win=win+1;
            }
            else
            {
                printf("You lost!\n");
                lost=lost+1;
            }
        }

    else if (door[2]==0)
    {
        printf("Door 3 is empty!\n You can choose to stay with the same door or switch, your choice of doors are 1 or 2\n");
        scanf("%d",&s);
        if (door[s-1]==1)
        {
            printf("You Won!\n");
            win=win+1;
        }
        else
            {
                printf("You lost!\n");
                lost=lost+1;
            }
    }
    }
    if ((x-1)==1)
    {
        if (door[2]==0)
        {
            printf("Door 3 is empty!\n You can choose to stay with the same door or switch, your choice of doors are 1 or 2\n");
            scanf("%d", &s);
            if (door[s-1]==1)
            {
                printf("You won!\n");
                win=win+1;
            }
            else
            {
                printf("You lost!\n");
                lost=lost+1;
            }
        }
        else if (door[0]==0)
        {
            printf("Door 1 is empty!\n You can choose to stay with the same door or switch, your choice of doors are 2 or 3");
            scanf("%d", &s);
            if (door[s-1]==1)
            {
                printf("You win!\n");
                win=win+1;
            }
            else
            {
            printf("You lost!\n");
            lost=lost+1;
            }
        }
    }
    if ((x-1)==2)
    {
        if (door[0]==0)
        {
            printf("Door 1 is empty!\n You can choose to stay with the same door or switch, your choice of doors are 2 or 3\n ");
            scanf("%d",&s);
            if (door[s-1]==1)
            {
                printf("You win!\n");
                win=win+1;

            }
             else
            {
            printf("You lost!\n");
            lost=lost+1;
            }

        }
        else if (door[1]==0)
        {
            printf("Door 2 is empty!\n You can choose to stay with the same door or switch, your choice of doors are 1 or 3 ");
            scanf("%d",&s);
            if (door[s-1]==1)
            {
                printf("You win!\n");
                win=win+1;
            }
             else
            {
            printf("You lost!\n");
            lost=lost+1;

            }
    }

    }
    printf("win=%d lost=%d\n", win, lost);
    main();
}

void Research (void)
{
    int p, GAMES;

    printf("Choose between 1.always-switch strategy and 2.never-switch strategy: ");
    scanf("%d", &p);
    if (p==1)
        {
            printf("Enter an integer N to determine number of plays: ");
        scanf("%d", &GAMES);
    {
    unsigned i, j, k, choice, winsbyswitch=0, door[3];

    srand(time(NULL));
    for(i=0; i<GAMES; i++){
        door[0] = (!(rand()%2)) ? 1: 0;
        if(door[0]) door[1]=door[2]=0;
        else{ door[1] = (!(rand()%2)) ? 1: 0; door[2] = (!door[1]) ? 1: 0; }
        choice = rand()%3;


        if(((!(door[((choice+1)%3)])) && (door[((choice+2)%3)])) || (!(door[((choice+2)%3)]) && (door[((choice+1)%3)]))) winsbyswitch++;
    }
    printf("\nAfter %u games, I won %u by always switching.  That is %f%%. ", GAMES, winsbyswitch, (float)winsbyswitch*100.0/(float)i);
    }
        }
    if (p==2)
    {
        printf("Enter an integer N to determine number of plays: ");
        scanf("%d", &GAMES);
    {
    unsigned i, j, k, choice, winsbynotswitch=0, door[3];

    srand(time(NULL));
    for(i=0; i<GAMES; i++){
        door[0] = (!(rand()%2)) ? 1: 0;
        if(door[0]) door[1]=door[2]=0;
        else{ door[1] = (!(rand()%2)) ? 1: 0; door[2] = (!door[1]) ? 1: 0; }
        choice = rand()%3;


        if(((!(door[((choice+1)%3)])) && (door[((choice+2)%3)])) || (!(door[((choice+2)%3)]) && (door[((choice+1)%3)]))) winsbynotswitch++;
    }
    printf("\nAfter %u games, I won %u by never switching.  That is %f%%. ", GAMES, winsbynotswitch, (float)winsbynotswitch*100.0/(float)i);
    }
    }


}
