Hey, I wrote this program for school. Always choose the other door. Your chances improve for a 33% chance to 66% chance, on average.
Here's my source code.
edit:
stupid code box wont display everything.
edit:
alright, these forum code boxes suck!
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.Random;
/**
* Application to simulate the game show "Let's Make a Deal".
* Based on a chosen strategy, the simulation will give the
* win percentage out of a given number of games played.
*
* @author Dustin Zimnox
* @version 2003-12-5
*/
public class gameshow extends JFrame
{
private Random r = new Random(); //used to get a random number
private int[] doors = {0,0,0}; //The 3 doors
private int strategy; //stores the strategy used
private int numberOfGames; //Number of games to play
private double winPercentage; //percentage of winning
private JTextField StrategyResultField = new JTextField("Strategy: ", 20);
private JTextField numberOfGamesResultField = new JTextField("Number of games played: ", 20);
private JTextField winPercentageResultField = new JTextField("Win percentage: ", 20);
public gameshow() //constructor
{
JPanel panel = new JPanel();
JLabel strategyLabel = new JLabel("Strategy(1-3)");
JLabel numberOfGamesLabel = new JLabel("How many games to play?");
final JTextField strategyField = new JTextField(2);
final JTextField numberOfGamesField = new JTextField(6);
final JButton playButton = new JButton("Play"); //the PLAY button
final JButton resetButton = new JButton("Reset"); //the RESET button
class playButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if (source == playButton) //if PLAY button is pressed
{
strategy = Integer.parseInt(strategyField.getText()); //get strategy chosen
numberOfGames = Integer.parseInt(numberOfGamesField.getText()); //get number of games to be played
winPercentage = runGame(strategy, numberOfGames); //run the game, get percentage of wins
//update text fields containing the results of the simulation
StrategyResultField.setText("Strategy: " + strategy);
numberOfGamesResultField.setText("Number of games played: " + numberOfGames);
winPercentageResultField.setText("Win percentage: " + winPercentage);
}
else if (source == resetButton) //if RESET button is pressed
{
//clear all text fields
strategyField.setText("");
numberOfGamesField.setText("");
StrategyResultField.setText("Strategy: ");
numberOfGamesResultField.setText("Number of games played: ");
winPercentageResultField.setText("Win percentage: ");
}
}//end actionPerformed() method
}//end playButtonListener class
ActionListener listener = new playButtonListener(); //button listener
playButton.addActionListener(listener);
resetButton.addActionListener(listener);
//add all the junk to the panel
panel.add(strategyLabel);
panel.add(strategyField);
panel.add(numberOfGamesLabel);
panel.add(numberOfGamesField);
panel.add(playButton);
panel.add(resetButton);
panel.add(StrategyResultField);
panel.add(numberOfGamesResultField);
panel.add(winPercentageResultField);
setContentPane(panel);
}//end constructor
/**
* Main method
*/
public static void main()
{
gameshow window = new gameshow();
window.pack();
window.setSize(new Dimension(350,300)); //set application window size
window.show();
}//end main() method
/**
* Game loop method.
* Simulates the game show
*/
public int runGame(int strat, int nOG) //strategy number and number of games to play
{
int initialChoice;
int newChoice=0; //new picked door number after strategy applied
int oldStrat = strat; //store original strategy, incase strategy 2 is used
int wins=0; //number of games won
double percent; //percentage of games won
for (int i=0; i<nOG; i++)
{
doors[0]=0; doors[1]=0; doors[2]=0; //reset doors array
doors[r.nextInt(3)] = 1; //randomly position a car behind a door
//A zero represents a goat, and 1 for car
initialChoice = r.nextInt(2); //randomly pick the first choice
if (strat == 2) //strategy 2, randomly chooses to use strategy 1 or 3
{
if (r.nextInt(1) == 0) strat = 1;
else strat = 3;
}
if (strat == 1) //strategy 1, stick with initial choice
{
if (doors[initialChoice] == 1) wins++;
}
else if (strat == 3) //strategy 3, always pick the other door
{
if (initialChoice == 0)
if (doors[1] == 0) newChoice = 2;
else newChoice = 1;
if (initialChoice == 1)
if (doors[0] == 0) newChoice = 2;
else newChoice = 0;
if (initialChoice == 2)
if (doors[0] == 0) newChoice = 1;
else newChoice = 0;
if (doors[newChoice] == 1) wins++;
}
strat = oldStrat; //reset strat with original strategy (for when strategy 2 is used)
}
//calculate win percentage value
percent = ((double)wins/(double)nOG)*100.0;
return (int)percent; //return win percentage value, cutting off fractions part
}// end runGame() method
}//end gameshow() class
"eureka" - Archimedes