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
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:


Monty Hall Problem - Suppose you are going to be on a game show, and you...
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...
1. Game simulation, Java programming. In the game show Let’s Make a Deal, a contestant is presented with three doors. Behind one of them is a valuable prize. After the contestant chooses a door, the host opens one of the other two doors (never revealing the prize, of course). The contestant is then given the opportunity to switch to the other unopened door. Should the contestant do so? Intuitively, it might seem that the contestant’s initial choice door and the...
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 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 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 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...
please help in java Monty Hall was a television game show host years ago. The contestant would have 3 doors to choose from. There was always a prize behind one door and goats behind the other two doors. The contestant would choose a door. Monty Hall would then open a door following these rules: Not a door the contestant chose and not the winning door. Monty ALWAYS showed a goat. He'd then ask the contestant if they wanted to change...
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 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 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...