#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    srand(time(NULL));
    int chosen,price,other,other2,a,again=1,won=0,lose=0,total=0;
    float freq;

    printf("Welcome! Today we are going to play a game called the Monty Hall game, let's get started!");

    while(again==1)
    {
        total++;
        price=rand()%3+1;

        printf("\nChoose a door from 1 to 3:");
        scanf("%d",&chosen);

        if (chosen!=1&&chosen!=2&&chosen!=3)
            printf("\nYou have entered a wrong number!");
        else
        {
        printf("\nOkay! You have chosen door %d",chosen);

        do
        {
            other=rand()%3+1;
        }while (other==price||other==chosen);

        do
        {
            other2=rand()%3+1;
        }while (other2==chosen||other2==other);

        printf("\n\nI am going to open door %d that has no price in it!",other);

        printf("\n\nNow you know that door %d has no price in it, do you want to switch to door %d or remain on door %d?\n1 for Yes and 0 for No!",other,other2,chosen);
        scanf("%d",&a);

        if(a==1)
            {
                printf("\nOkay! You switched from door %d to door %d",chosen,other2);
                chosen=other2;
            }

        if (chosen==price)
            {   won++;
                printf("\n\n\t\tCONGRATSYOU WIN!!! AND THERES IS YOUR PRICE!\n\n\n");
            }
        else
        {
            lose++;
            printf("\n\n\t\tOHHHHHHH!!!\n\nI am sorry but you chose the wrong door....\n\n\n");
        }

        }
        printf("\nThe price is behind door %d",price);

        printf("\nDo you want to play again? 1 for yes and 0 for no.");
        scanf("%d",&again);
    }
        freq=(float)won/total;

    printf("\nYou played %d games in total.",total);
    printf("\nWon: %d  \tLose: %d",won,lose);
    printf("\nThe frequency of winning is %f per game",freq);

    return 0;
}

