/*Name : Mohamed Elassar
  ID   : 8074665
*/
#include <stdio.h>
#include <time.h>
int MontyHallGame (int,int);
int switchornoswitch(int,int,int,int);
int switchornoswitch2(int,int,int,int);


int  main()
{
    int option,chosendoor,winningdoor,remainingdoor,gameswon,total,c,L,N,switchornot,x,ctr;
    float z;

    srand(time(NULL));

do {

    printf("Enter one of the following options:\n1)Game Mode\n2)Research Mode\n3)Exit\n");
    scanf("%d", &option);
    if (option != 1 && option !=2 && option !=3)
        printf("Error\n");


    if (option==1)
{

    winningdoor = 1+srand()% 3;
    printf("You are presented 3 doors. Please chose a door\n");
    scanf("%d", &chosendoor);
    c=MontyHallGame (chosendoor,winningdoor);
    printf("You now have two doors, door %d which you chose and door %d\nIf you want to switch, press 1. If not, press 0\n", chosendoor,c);
scanf("%d", &switchornot);
 x=switchornoswitch(chosendoor,c,winningdoor,switchornot);




}
else
    if (option==2)
  {winningdoor=1+srand()% 3;
    printf("Please select one of the following strategies:\n1)Always switch 2)Never Switch\n\nPress 1 to chose strtegy 1, else press 2..");
    scanf("%d", &L);
    printf("Please enter the number of game plays to be played with above strategy: ");
    scanf("%d", &N);

    chosendoor=1;
    winningdoor = 1+rand()% 3;
    c=MontyHallGame(chosendoor,winningdoor);
     x=switchornoswitch2(chosendoor,c,L,N);




    }




else
if (option==3)
    return 0;
}

while (1);

}






int MontyHallGame(int chosendoor,int winningdoor)
{
    int revealeddoor,remainingdoor,switchornot,x,y;
    srand(time(NULL));

do
        {
            revealeddoor = rand() % 3 + 1;
        } while (revealeddoor == chosendoor || revealeddoor == winningdoor);

        do
        {
            remainingdoor = rand() % 3+1;
        } while (remainingdoor == chosendoor || remainingdoor == revealeddoor);




return remainingdoor;


}









int switchornoswitch(int chosendoor,int c,int winningdoor,int switchornot)
{

    int static gameswon=0,total=0;

    if (switchornot==0)
    {
        if (chosendoor==winningdoor)
        {printf("You Won\n");
         gameswon++;
         total++;
            }
         else
         {printf("You Lost\n");
            total++;
            }

    }
    else
        if (switchornot==1)
    {
        chosendoor=c;
        if (chosendoor==winningdoor)
            {printf("YOU WON\n");
            gameswon++;
            total++;}

        else
            {printf("YOU LOST\n");
        total++;}

    }
    else
        printf("ERROR");



printf("Games Won: %d  ", gameswon);
printf("Total games: %d\n", total);
return gameswon;


}




int switchornoswitch2(int chosendoor,int c,int L,int N)

{

srand(time(NULL));
   float gameswon=0,total=0;
   int ctr,winningdoor;
    float z;
for (ctr=0;ctr<=N;ctr++)

{
    winningdoor=1+rand()%3;
    if (L==2)
    {

        if (winningdoor==1)
        {
         gameswon++;
         total++;
            }
         else
        total++;
    }
    else
        if (L==1)
    {
        if (winningdoor==c)
            {
            gameswon=gameswon+2.0;
            total++;
            }
        else
        total++;
    }
}
z=(gameswon/N)*100.0;
printf("You played %d games and won %f\n", N,gameswon);
printf("The percentage of games won is %f\n\n", z);


}





