#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int game()
{
    int winningDoor, choice, alternate, losingdoor,change, correct=0;
    winningDoor=rand()%3+1; // chooses a winning door
    printf("\n\nplease choose a door |1| |2| |3|\n");
    scanf("%d", &choice); // prompts user to choose a door
    for(losingdoor=1;losingdoor<4;losingdoor++)
    {
        if ((losingdoor!=winningDoor)&&(losingdoor!=choice)) // finds the door that is not the winning door or the users choice
            break;
    }
    for(alternate=1;alternate<4;alternate++)
    {
        if ((alternate!=choice)&&(alternate!=losingdoor)) //finds the door that is not the users choice or the winning door
            break;
    }
    printf("if i told you door %d had no prize would you switch to door %d?\n(1 for yes, 0 for no)\n", losingdoor,alternate);
    scanf("%d", &change); //asks the user if they would like to switch doors
    if(change==1)
        choice=alternate;

    if(choice==winningDoor) // checks to see if the user has chosen the correct door
    {
        correct=1;
        printf("you have chosen the correct door!\n\n");
    }
    else
        printf("sorry the winning door was %d! maybe next time!\n\n", winningDoor);
    return(correct); //returns the points to be added to the overall score
}

int research()
{
    int winningDoor, choice, alternate, losingdoor,change, correct=0,n,p=0;
    printf("how many games would you like to simulate?\n");
    scanf("%d", &n);

    printf("alright %d games will be simulated. would you like the program to switch doors in the simulation?(1 for yes, 0 for no)\n", n);
    scanf("%d", &change);

    for(p=0;p<n;++p)
    {
        winningDoor=rand()%3+1;
        choice=rand()%3+1;
        for(losingdoor=1;losingdoor<4;losingdoor++)
        {
            if ((losingdoor!=winningDoor)&&(losingdoor!=choice))
                break;
        }
        for(alternate=1;alternate<4;alternate++)
        {
            if ((alternate!=choice)&&(alternate!=losingdoor))
                break;
        }
        if(change==1)
            choice=alternate;

        if(choice==winningDoor)
            ++correct;
    }
    printf("%d games where won out of the %d simulated!\n\n", correct,n);
    printf("that is a winning percentage of %f !\n\n", ((float)correct/n*100));
}

int main()
{
    srand(time(NULL));

    int mode=-1, gamesPlayed=0, gamesWon=0;

    while (mode!= 3)
    {
        printf("Welcome to the monty hall program! \n games played: %d \t games won: %d \n", gamesPlayed, gamesWon);
        printf("which mode you would like to play? \n(Main menu: 0, game: 1,research: 2, or exit: 3):");
        scanf("%d", &mode);

        switch (mode)
            {
                case 1:
                   gamesWon=gamesWon+game();
                   gamesPlayed=gamesPlayed+1;
                   break;
                case 2:
                    research();
                    break;
                case 3:
                    mode=3;
                    break;
                default:
                    printf("\nsorry invalid entry please try again.\n\n");
                    break;
            }
    }
}


