user1742525
user1742525

Reputation: 45

Monty Hall Game: How to allow user to choose door instead of program automatically randomizing choice?

#include <stdio.h>
#include <time.h>
int main(void)
{
    int games = 0;
    int stayWins = 0;
    int switchWins = 0;
    int chosenDoor;
    int remainingDoor;
    int revealedDoor;
    int winningDoor;
    int option;

    srand (time(NULL));

    do
    {
    chosenDoor = rand() % 3 + 1;
    winningDoor = rand() % 3 + 1;
        do
        {
            revealedDoor = rand() % 3 + 1;
        } while (revealedDoor == chosenDoor || revealedDoor == winningDoor);

        do
        {
            remainingDoor = rand() % 3+1;
        } while (remainingDoor == chosenDoor || remainingDoor == revealedDoor);

        option = rand() % 2 + 1;
        if (option == 1)
        {
            if (chosenDoor == winningDoor)
            {
                stayWins++;
            }
        }
        if (option == 2)
        {
            chosenDoor = remainingDoor;
            if (chosenDoor == winningDoor)
            {
                    switchWins++;
            }
        }
        games++;
    } while (games < 10000);

printf("Out of 10,000 games, the contestant won %d times by staying with his/her original choice and won %d times by switching his/her choice.",stayWins,switchWins);

    return 0;
}

Evening guys, Here is a completed Monty Hall Problem code that prints the results of 10,000 games. The code will choose the chosen door for the user. How can I change it so the program will allow the user to choose himself? As well, how can I modify it so the program won't randomize values of '1' or '2' for the program but instead allows me to make the option of switching? My progress... Instead of this:

chosenDoor = rand() % 3 + 1;

Use this, where only acceptable input is 1, 2, or 3 :

printf("Choice:");
scanf("%d",&chosenDoor);

Is this the right track? I'm aware at this point the user will then need to input his choice 10,000 times before the program 'finishes', so is there a way I can apply the first choice to the other 9,999 trials?

Upvotes: 0

Views: 3424

Answers (1)

user529758
user529758

Reputation:

Is there a way I can apply the first choice to the other 9,999 trials?

Move the

printf("Choice: ");
scanf("%d", &chosenDoor);

part of the code outside the loop.

Upvotes: 1

Related Questions