Assignments:MontyHall
From FANG
Contents |
The Problem
In 1963 a new game showed aired called Let's Make a Deal. There were lots of silly parts to the game, but one intriguing one was when the contestants would try to guess the door that had a prize behind it: door 1, 2, or 3. Here's the way the game goes (commonly called the Monty Hall Problem):
- The contestant chooses a door.
- Monty Hall, the host, opens a door that neither has the prize behind it nor is it the door the contestant chose.
- The contestant gets the opportunity to keep their original choice, or change to the other unopened door.
- Monty Hall opens the door the contestant chose in the previous step and awards the prize or a gag gift like a giant rocking horse.
Each time the game is played, there is only one door that has the prize behind it and the prize door is always chosen at random before the game begins.
What would you do if you were playing the game, stay with your original choice, or switch? Does it matter? Visit here to learn more about what you should do and why.
Your task is not to win the prize but it is instead to write your own version of the game using the FANG Engine.
This assignment assumes you have some basic knowledge of the FANG Engine. If you don't have any prior experience with the FANG Engine, try completing Wackadot or getting other experience with the FANG Engine first because this assignment does not try to teach you the basics of the FANG Engine.
Requirements
Be sure to read through all of the requirements below before starting to code. It's very important to fully understand the problem before you start trying to solve it! Also, make life easier on yourself by also reading the hints at the bottom of the page. If you choose to disregard these suggestions, large amounts of frustration may be in your immediate future.
Basic Requirements
- When the game starts, the player should see three labeled doors and the instructions to pick a door.
- When the player picks a door, another door should open. That door should not have a prize behind it and should not be the door the player picked.
- The player should be given instructions to stay or switch.
- When the player chooses to stay or switch, all doors open revealing the position of the prize. Every door should have something behind it, but only one has the prize.
- The player should be told whether she/he won or lost (i.e. display "You Win!" or "You Lose!" or similar).
Suggested Steps to Completing Basic Requirements
It's far easier to complete lots of simpler tasks than one large complex task. The following ordered list is a suggested set of steps toward completing the game. Feel free to use it or create your own plan of attack. The goal is to always keep a working program, and test as frequently as possible in order to reduce the number of places you have to look to find errors.
- Make a rectangle appear in the middle of the screen.
- Make the rectangle disappear when the player clicks on it.
- Make three rectangles appear on the screen.
- Make each rectangle disappear when the player clicks on it.
- Add the labels 1, 2, and 3 to the rectangles (as StringSprites).
- Make both the rectangle and label disappear when the player clicks on the door.
- Put a green dot behind door 1 and red dots behind doors 2 and 3.
- Write a method called canOpenDoor that receives three int paramaters and returns a boolean. The three integers are the number of the door chosen, the number of the prize door, and the number of the door to open. The method returns true if it is permissable to open the desired door. Remember, the first door opened neither has the prize nor is the door chosen.
- Write a method called getOtherDoor that receives two int paramaters and returns an int. The two integer parameters are the door number of the prize and door number the user has chosen. The method returns a door number that the neither was chosen nor has the prize. Hint - call the canOpenDoor method with each of the three doors to determine which is suitable.
- When the player clicks on door 2, make door 3 open to reveal the red dot. Do this by calling getOtherDoor method and make sure it returns 3 as it should.
- When the player clicks on door 3, make door 2 open to reveal the red dot. Do this by calling getOtherDoor method and make sure it returns 2 as it should.
- When the player clicks on door 1, make either door 2 or 3 disappear at random. Do this by calling getOtherDoor method and make sure it returns either 2 or 3. You may need to modify the getOtherDoor method to make it choose a random door when the player initially chooses the prize door.
- Randomly pick which door has the green dot behind it, and make the other two have red dots behind them.
- When the player clicks on any door, another door is opened, but not the one with the green dot behind it.
- Make it so that the player cannot click on any door after clicking on one door. Here's one way to do this: make an int instance variable called stage and initialize it to zero when the game begins. In the method that processes input, check to see which stage you are in before responding to the input. The best way to do this is with the if/else if/else structure where you check first for stage is zero, second for stage is one and the only remaining possibility is that stage is two. Put all of the original logic within the first if statement where it checks for stage equals zero. It will of course be zero since that is what you set it to initially. As soon as any door is opened, set stage to one. This will prevent the player from being able to open another door.
- Make it so that the second door clicked on opens directly. You can do this by putting the logic into the section that checks to see if stage is equal to 1. Once the door opens, you should set stage to 2.
- Add StringSprites telling the user what to do:
- Click on a door.
- Click on the same or different door.
- Display "You Win!" or "You Lose!".
Enhancements - Choose and Implement at least 3 Enhancements
- Use ImageSprites for what is behind the doors.
- Use ImageSprites for the doors.
- Play a sound when a door opens.
- Play a sound when the user wins and a different sound when the player loses.
- Record yourself reading the player instructions aloud. Play this when the instructions are displayed on the screen.
- Display a splash screen at the beginning.
- Allow the player to start over at the end of the game.
Hints
- Read all the hints before you start coding.
- Make sure the game works after each change. Don't write too much without testing it.
- The FANG Engine has a built-in random number generator called random. You can use this to generate random numbers. For example, the code below randomly assigns 1, 2, or 3 to the local variable x:
int x=random.nextInt(3)+1;
- You can only use the random number generator within method bodies. For example, do not do the following:
private int x=random.nextInt(3)+1;
- Once you have the random number, you can use it to do different things depending upon the outcome:
if(x==1)
{
//do something here
}
else if(x==2)
{
//do something else here
}
else
{
//do another option here
}
- It is probably a good idea to make an instance variable that keeps track of the winning door. To do this, declare your instance variable at the top of your class body:
private int winningDoor;
- Initialize it in the startGame method:
winningDoor=random.nextInt(3)+1;
- Use winningDoor in the advanceFrame method when you want to do something depending on where the prize door is.
- Work with a partner if your teacher allows it. Talk to each other and make sure you both know what is being added to the code and why. In this way you can both learn from each other and prevent each other from making as many mistakes.
- Look at examples of similar code. For example, if you did something like this before, see if you can reuse some of that code in solving this problem.
- Make sure your methods are not too long. Any method longer than 50 lines is too long. Break it up into helper methods that you call.
- Look for similar repeated code. Group this similar code in a method and provide parameters where the code is only slightly different. If you do this, you can make this program shorter, simpler, and easier to debug.
- You will need to use lots of if statements.
- Keep an instance variable that tells what step of the game you are currently on. For example, you could declare the instance variable stage:
private int stage;
and initialize it to zero in your startGame method:
stage=0;
- Now update it as events happen in your game:
stage=1;
then
stage=2;
- In your advanceFrame method, put in an if statement and do different things depending on which stage you are in:
if(stage==0)
{
doStage1Stuff();
}
else if(stage==1)
{
doStage2Stuff();
}
else if(stage==2)
{
doStage3Stuff();
}
- This page was last modified on 15 September 2008, at 12:04.
- This page has been accessed 1,678 times.
- Privacy policy
- About FANG
- Disclaimers
- Powered by MediaWiki!











