Question

Monty Hall Problem - Suppose you are going to be on a game show, and you...

Monty Hall Problem - Suppose you are going to be on a game show, and you will be given the choice of three doors:

  • Behind one door is a car;

  • behind the other two doors are goats.

You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?"

Is it to your advantage to switch your choice? You could use probability theory to figure out the best strategy, switch or no switch. But instead you decide to write a Java program to simulate millions of plays of the game (such simulation is called Monte Carlo simulation) to help you decide the best strategy.

A single game will involve repeating:

  • “hiding” the prize behind one of three doors (starting new game / resetting game),

  • Having three players (representing different strategies) making an initial guess SEPARATELY,

  • Having three players (representing different strategies) making a second door selection knowing which door is open (has no prize behind it). Opening that door is based on each player’s initial guess (in other words: each player can end up with a different door opened for them, but the prize is in the same place for all).

  • Write a Game class with:

    • Attribute/field prizeLocation that will hold a door number (integer from the set {1,2,3}) behind which the prize is hidden,

    • Non-parametrized constructor that will assign random number to prizeLocation,

    • Methods:

      • getPrizeLocation, that returns the value of prizeLocation attribute/field

      • reset, that will restart the game ? assign a new random integer to prizeLocation attribute,

      • getOpenDoor, which, given an integer argument initialGuess (Player’s FIRST guess), will “open one door” (NOT the door with the prize behind it and NOT the initialGuess door – the other one) and return its number (integer)

Using Java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Following is the output and the code for the solution as asked in the problem. The code is appropriately commented for better understanding and a GameDemo class has also been added to give an idea of how the game works.

Also, screenshots of the code have been attached for help on code indentation.

Output:

E:\Study\Java>java GameDemo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|                                                                      |
|   Welcome to the Game Show. Here you have a chance to win a car.     |
|                                                                      |
| In case you hit the jackpot, you get a chance to take home a goat!!! |
|                                                                      |
|                              Enter choice:                           |
|                              1 to play                               |
|                                 OR                                   |
|                              0 to exit                               |
1
|                                                                      |
|                              Make a guess!                           |
|                                                                      |
|                      The car is behind which door!                   |
|                                                                      |
|                              Enter choice:                           |
|                              1 for Door1.                            |
|                              2 for Door2.                            |
|                              3 for Door3.                            |
2
|                                                                      |
|                      The car is not behind door 3 !                  |
|                                                                      |
|         Enter new choice if you wish to change your choice:          |
|                                 OR                                   |
|                           Enter old choice:                          |
2
|                                                                      |
|                  Jackpot!!! You have won the goat.                   |
|                                                                      |
|                              Enter choice:                           |
|                              1 to play                               |
|                                 OR                                   |
|                              0 to exit                               |
0
|                                                                      |
|             Exiting game. Thank you for participating.               |
|                                                                      |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Code:

public class Game {

    private int prizeLocation;

    /* The game constructor which puts the

     * prize at a new location in the field

     * prizeLocation.

     *

     * The constructor only calls the reset

     * method.

     */

    public Game() {

        this.reset();

    }

    // Setter method for prizeLocation field.

    private void setPrizeLocation(int num) {

        this.prizeLocation = num;

    }

    /* Random number generator method.

     *

     * Math.random() method generates a double

     * (floating point value) between

     * 0.0 (included) and 1.0 (excluded).

     *

     * Multiplying the generated number by 3

     * gives a double value between 0.0 and

     * approx. 2.9.

     *

     * Casting the resulting value to int gives

     * one value out of 0, 1, and 2.

     *

     * Adding 1 to the int value gives us the

     * desired range: 1, 2, or 3.

     */

    private int getRandomNum() {

        int randomNum = (int)(Math.random() * 3) + 1;

        return randomNum;

    }

    // Getter method for prizeLocation field.

    public int getPrizeLocation() {

        return this.prizeLocation;

    }

    /* The reset method which generates a random

     * number and sets it to the prizeLocation

     * field.

     */

    public void reset() {

        int randomNum = getRandomNum();

        this.setPrizeLocation(randomNum);

    }

    /* This method takes an argument, which is the

     * initial guess of the player.

     *

     * This method checks the actual prize location

     * and the player's guess and returns another

     * door value, which does not have the car behind

     * it.

     */

    public int getOpenDoor(int initialGuess) {

        int otherDoor = getRandomNum();

        while(otherDoor == initialGuess || otherDoor == this.prizeLocation) {

            otherDoor = getRandomNum();

        }

        return otherDoor;

    }

}

===================================================================================

import java.util.Scanner;

/* Demo class to run the game for a single player.

* This class is an intefrace for playing the game

* for a single player.

*

* This class is only written to give you an idea

* of how the game works.

*/

public class GameDemo {

    private static Scanner scanner = new Scanner(System.in);

    private static int mainMenuOption = 1;

    public static void main(String[] args) {

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

        System.out.println("|                                                                      |");

        System.out.println("|   Welcome to the Game Show. Here you have a chance to win a car.     |");

        System.out.println("|                                                                      |");

        System.out.println("| In case you hit the jackpot, you get a chance to take home a goat!!! |");

        startGame();

    }

    public static void startGame() {

        Game game;

        while(mainMenuOption != 0) {

            showMainMenu();

            mainMenuOption = scanner.nextInt();

            if(mainMenuOption == 0) {

                break;

            } else if(mainMenuOption == 1) {

                game = new Game();

                playGame(game);

            } else {

                System.out.println("|                                                                      |");

                System.out.println("|                 Invalid option. Please retry.                        |");

            }

        }

        exitGame();

    }

    public static void showMainMenu() {

        

        System.out.println("|                                                                      |");

        System.out.println("|                              Enter choice:                           |");

        System.out.println("|                              1 to play                               |");

        System.out.println("|                                 OR                                   |");

        System.out.println("|                              0 to exit                               |");

    }

    public static void playGame(Game game) {

        System.out.println("|                                                                      |");

        System.out.println("|                              Make a guess!                           |");

        System.out.println("|                                                                      |");

        System.out.println("|                      The car is behind which door!                   |");

        System.out.println("|                                                                      |");

        System.out.println("|                              Enter choice:                           |");

        System.out.println("|                              1 for Door1.                            |");

        System.out.println("|                              2 for Door2.                            |");

        System.out.println("|                              3 for Door3.                            |");

        int guess = scanner.nextInt();

        int openDoor = game.getOpenDoor(guess);

        System.out.println("|                                                                      |");

        System.out.println("|                      The car is not behind door " + openDoor + " !                  |");

        System.out.println("|                                                                      |");

        System.out.println("|         Enter new choice if you wish to change your choice:          |");

        System.out.println("|                                 OR                                   |");

        System.out.println("|                           Enter old choice:                          |");

        int secondGuess = scanner.nextInt();

        if(secondGuess == game.getPrizeLocation()) {

            System.out.println("|                                                                      |");

            System.out.println("|                Congratulations. You have won the car.                |");

        } else {

            System.out.println("|                                                                      |");

            System.out.println("|                  Jackpot!!! You have won the goat.                   |");

        }

    }

    public static void exitGame() {

        scanner.close();

        System.out.println("|                                                                      |");

        System.out.println("|             Exiting game. Thank you for participating.               |");

        System.out.println("|                                                                      |");

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

    }

}

Screenshots:

Game.java file:

GameDemo.java file:

Add a comment
Know the answer?
Add Answer to:
Monty Hall Problem - Suppose you are going to be on a game show, and you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5. Consider the Monty Hall Problem. A game show host shows you three doors, and indicates...

    5. Consider the Monty Hall Problem. A game show host shows you three doors, and indicates that one of them has a car behind it, while the other two have goats. You win a car if you end up choosing a door with a car behind it. The game is conducted as follows: • You pick an initial door out of the three available. • The game show hosts then opens a door (out of the remaining two doors) with...

  • Question 1: Consider the following Monty Hall problem. Suppose you are on a game show, and...

    Question 1: Consider the following Monty Hall problem. Suppose you are on a game show, and you are given the choice of three doors. Behind one door is a car, behind the others, goats. You pick a door, say #1, and the host, who knows what is behind the doors, opens another door, say #3, which has a goat. Here we assume that the host cannot open the door to expose the car and when he can open either of...

  • In the three-door Monty Hall problem, there are two stages to the decision, the initial pick...

    In the three-door Monty Hall problem, there are two stages to the decision, the initial pick followed by the decision to stick with it or switch to the only other remaining alternative after the host has shown an incorrect door. An extension of the basic problem to multiple stages goes as follow. Suppose there are four doors, one of which is a winner. The host says: You point to one of the doors, and then I will open one of...

  • 1.3 Cars and goats: the Monty Hall dilemma On Sunday September 9, 1990, the following question...

    1.3 Cars and goats: the Monty Hall dilemma On Sunday September 9, 1990, the following question appeared in the "Ask Marilyn" column in Parade, a Sunday supplement to many newspapers across the United States: Suppose you're on a game show, and you're given the choice of three doors; behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3,...

  • A debate recently erupted about the optimal strategy for playing a game on the TV show...

    A debate recently erupted about the optimal strategy for playing a game on the TV show called "Let's Make a Deal." In one of the games on this show, the contestant would be given the choice of prizes behind three closed doors. A valuable prize was behind one door and worthless prizes were behind the other two doors. After the contestant selected a door, the host would open one of the two remaining doors to reveal one of the worthless...

  • The Game: Suppose you're on a game show, and you're given the choice of 3 doors....

    The Game: Suppose you're on a game show, and you're given the choice of 3 doors. Behind one door is a car, behind the others, goats. You start by choosing a door, say number 1, which remains closed for now. The game show host, who knows what's behind the doors, opens another door, say number 3, which reveals a goat. He says to you, "You've already chosen door number 1, now that I've shown you a goat behind door number...

  • Probability puzzle 2: The Game Show Paradox Discussions List View Topic Settini Probability Puzzle 2: The...

    Probability puzzle 2: The Game Show Paradox Discussions List View Topic Settini Probability Puzzle 2: The Game Show Paradox Subscribe Let's say you are a contestant on a game show. The host of the show presents you with a choice of three doors, which we will call doors 1. 2. nd 3. You do not know what is behind each door, but you do know that behind two of the doors are beat up 1987 Hyundai Excels, and behind one...

  • Please help me write these in R script / Code 1, Suppose you're on a game...

    Please help me write these in R script / Code 1, Suppose you're on a game show, and you're given the choice of three doors. Behind one door is a car; behind the others, goats. You pick a door, say #1, and the host, who knows what's behind the doors, opens another door, say #3, which has a goat. He then says to you, "Do you want to pick door #2?" What is the probability of winning the car if...

  • 126. An article entitled "Behind Monty Hall's Doors: Puzzle, De- bate and Answer?" appeared in the...

    126. An article entitled "Behind Monty Hall's Doors: Puzzle, De- bate and Answer?" appeared in the Sunday New York Times on July 21, 1991. The article discussed the debate that was raging among mathematicians, readers of the "Ask Marilyn" column of Parade Magazine and the fans of the TV game show "Let's Make a Deal." The argument began in Septem- ber, 1990 when Ms. Vos Savant, who is listed in the Guinness Book of World Records Hall of Fame for...

  • I need help fixing my python3 code. I am trying to get my Monty hall game...

    I need help fixing my python3 code. I am trying to get my Monty hall game working correctly. Below is my code. When I run the code it repeats the first question and doesn't work until the 3rd attempt or sometimes more than that. How do I fix this? I also need to be able to make it run 5 times in a row.(like ask the user at least 5 times, so it is re-playable. I added a image of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT