Imagine yourself on the set of a game show. You\'re given the choice of three do
ID: 3570929 • Letter: I
Question
Imagine yourself on the set of a game show. You're given the choice of three doors. Behind one of the
doors is a car you can drive home in if you guess correctly. Behind the other two doors are goats.
After you initially guess the door, the host of the game show (who knows the door holding the car)
opens one of the other doors revealing a goat. He gives you the choice: stick with your original door,
or choose the other unopened door.
This problem is reminiscent of a Sixties game show hosted by an individual named Monty Hall. For
this assignment you will write a program simulating this game and the results of either choice. You
will ask the user how many times they wish to run the simulation. If the user enters 1000 for instance,
your program will run through 1000 simulations where the player sticks with the original door and
1000 simulations where the player chooses the other unopened door. After the program is completed
execution, the winning percentages will then be displayed for each strategy. (Be sure to make the
initial selection, Monty's selection, and the second-chance selection happen randomly.)
Be sure to divide your program into functions which perform each major task. (The purpose of this
assignment is to find out whether or not it's to the player's advantage to change their selection.)
Explanation / Answer
Lets understand what your code does step by step with an example
import java.util.Random;
import java.util.Scanner;
public class GameShow {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
Random generator = new Random();
// Initialize Variables
int user_door,
open_door,
other_door,
prize_door;
// Generate random value 1-3
prize_door = generator.nextInt(3)+1;
Lets assume the generator assigns 2 to price_door
open_door = prize_door;
while(open_door == prize_door){
open_door = generator.nextInt(3)+1;
}
Lets assume after the loop the generator assigns 1 to open_door
other_door = open_door;
while (other_door == open_door || other_door == prize_door){
other_door = generator.nextInt(3)+1;
}
Lets assume after the loop the generator assigns 3 to other_door
// Begin Game
System.out.println("*** Welcome to the game show! ***");
System.out.println("Select the door (1, 2, or 3): ");
Lets recall the values of the variables open_door = 1 other_door = 3 prize_door = 2
user_door = scan.nextInt();
// User Validation
if (user_door > 3 || user_door < 0) {
System.out.println("Please select door 1, 2, or 3");
user_door = scan.nextInt();
} else if(user_door == 1 || user_door == 2 || user_door == 3) {
Lets assume user enters 1 user_door = 1
//Continue Game
System.out.println(" In a moment, I will show you where the prize is located,");
System.out.println("but first I will show you what is behind one of the other doors");
//Continue Dialogue
System.out.println(" Behind door number " + open_door + " are goats!");
Here you are opening Door 1 (open_door = 1) which is the same as the user_door = 1
Which is wrong according to the logic of the game
System.out.println("You selected door number " + user_door);
System.out.println(" Would you like to switch your door(y/n)? ");
//User Input Yes or No
char userReply = scan.next().charAt(0);
//If statement with nested while statements for user input
if (userReply == 'y'){
user_door = other_door;
} while(userReply != 'y' && userReply != 'n')
{
//User Validation
System.out.println("Please enter either y/n");
userReply = scan.next().charAt(0);
}
System.out.println("The prize is behind door number: " + prize_door);
//Check to see if user won or lost
if(user_door == prize_door){
System.out.println("Congratulations! You won the prize!");
} else {
System.out.println("Sorry. You lost.");
}
}
}
}
You need change the selection of doors based on the user's input because as a host you know where the car/gold is and where the goat is which missing in the program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.