#include <stdio.h>
#include <stdlib.h>
void game_mode(); //prototypes
void research_mode();
int door_open();
int door_find();

int main()
{
   int a;
   while (a!=3) //if a is 3 the loop will end because the user wants to exit
{printf("Enter an option\n 1.Game mode\n 2.Research Mode\n 3.Exit\n\n\n");
scanf("%d", &a);

    switch (a)
{
   case 1:
        {
        game_mode();
        break;
        }
   case 2:
        {
        research_mode();
        break;
        }
   case 3:
    break;
}

}

return 0;
}

void research_mode()
{
    int gametype, numOfgames;
    int prizedoor, userpick, opendoor, choice, otherdoor, i;
    int numberofwins = 0;
do //loops until user enters 1 or 2
{
printf("Enter which game you would like: Choose 1 for always switching or 2 for never switching\n");
scanf("%d", &gametype);
    if (gametype!=1 && gametype!=2)
        printf("Try again\n");
}
while (gametype!=1 && gametype!=2);

printf("How many games would you like to play?\n");
scanf("%d", &numOfgames);


    switch(gametype)
 {
        case 1:
    {
        for(i=0; i<numOfgames; i++) //repearts until it has played the number of games user entered
        {
            prizedoor = 1+ rand()%3; //number between 1 and 3
            userpick = 1;

       if (prizedoor==1)
            continue;

        opendoor = door_open(userpick, prizedoor); //call function, assign to open dooor
        otherdoor = door_find(userpick, opendoor); //call function, assign to other find

            if (prizedoor== otherdoor)
                {
                 numberofwins = numberofwins + 1; //if prize == users new door, increase wins
                }

        }
printf("You won %d times and played %d times\n\n. Your percentage of winning is %.2f\n\n",numberofwins, numOfgames, (double)numberofwins/numOfgames*100);
            break;
    }
        case 2:
    {
            for(i=0; i<numOfgames; i++) //repeates until has looped 'number of games' times
        {
            prizedoor = 1+ rand()%3;
            userpick = 1;

            if(prizedoor == userpick)
                numberofwins=numberofwins+ 1;

        }
        printf("You won %d times. You played %d times. Your percent win is %.2f\n",numberofwins, numOfgames, (double)numberofwins/numOfgames *100);
            break;
    }
}

    return ;
}

void game_mode()
{
    int prizedoor, userpick, opendoor, choice, otherdoor;
    static int gamecounter=0; //makes function remember these when you return back from main
    static int numberofwins=0;
    prizedoor = 1+ rand()%3; //prize door random # from 1 to 3

       do //loops until user enters 1,2, or 3.
       {
           printf("Pick a door number, Door 1, Door 2, Door 3 \n");
        scanf("%d", &userpick);
        if (userpick <1 || userpick >3)
            printf("Try again. ");
       }
            while (userpick <1 || userpick >3);

        opendoor = door_open(userpick, prizedoor);
        otherdoor = door_find(userpick, opendoor);
    printf("\nI will now open door %d. It has no prize behind it\n", opendoor);
    printf("Would you like to change your option to door %d? Enter 1 for yes and 2 for no\n", otherdoor);

    do //loops until user enters 1 or 2
    {
        scanf("%d", &choice);
    if (choice != 1 && choice != 2)
        printf("Try again. Enter 1 for yes and 2 for no\n");
    }
    while (choice!= 1 && choice != 2);

       switch(choice)
    {
    case 1:
        {
            userpick= otherdoor; //changer users door to otherdoor

        break;
        }

    case 2:
        {
            break; //do nothing to users door
        }

    }

    printf("\nThe Prize door is Door number %d\n", prizedoor);
    printf("Your door is door %d\n", userpick);
    if (userpick==prizedoor)
        {printf("\nYou win!!\n");
        numberofwins= numberofwins + 1; //increase number of wins if userdoor is prizedoor
        }
    else
        printf("\nYou lose\n");

gamecounter = gamecounter + 1;

printf("You have won %d times. You have played %d times\n\n", numberofwins, gamecounter);
printf("Wanna play again? Or choose a different option\n");
 return ;
}

int door_open(int userpick, int prizedoor)
{
    int opendoor;

        do
            opendoor = 1+ rand()%3; //opendoor is random number from 1 to 3
        while (opendoor==userpick || opendoor== prizedoor); // if open door is userpick or prizedoor, loop to find new opendoor

        return opendoor; //return opendoor to function you were in
}

int door_find(int userpick,int opendoor)
{
     int otherdoor;
        do //loop until otherdoor isn't = userpick or opendoor.
            otherdoor = 1+ rand()%3;
        while (otherdoor==userpick || otherdoor == opendoor);
        return otherdoor; //return otherdoor to function you were in
}
