[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
You have a choice of one of 3 doors. One of the doors has a
new car behind it, the other two have goats as prizes. After you select a door, the host will open one of the other
doors revealing a goat. You then have the choice of sticking with your original choice or switching doors.
|A TDoor object was defined to keep track of whether or not the door has been selected by the player, which prize is behind the door, whether or not it is open, and a pointer to the TShape rectangle that represents the door. If it were a real visual component, it could have been a descendant of TShape, but this is much simpler for casual applications - just set a pointer to the "door" Tshape rectangle as a property at the time that we create each TDoor. The form has a Doors array containing the 3 TDoor objects.|
|I had some fun animating the doors. I decided to "raise" them vertically to reveal the hidden prize, a whole lot easier than designing swinging doors. That part was easy - just reduce the height property of a rectangular Tshape object (the door) in a loop. Oh, and set form property doublebuffered to true to stop that flicker problem.|
|The harder part was getting the pictures of the prizes rearranged behind each door. I wrote a Shuffle routine that uses the idea commonly used to programmatically shuffle a deck of cards. Load the cards numbers, picture numbers in this case, into an array and then run though the array exchanging each entry with one randomly selected from those above (or below) it. The effect is of moving randomly selected cards from the un-shuffled portion of the deck to the bottom (or top) of the deck. The resulting arrangement of picture numbers is used to draw each picture behind the appropriate door. The "behind" part is also easy. I loaded the pictures originally so that they exactly overlapped the door images. A right click and "Send to back" on each moved it to the top of the the Z-order list. Windows draws screens by processing this list from top down, so high stuff is drawn first and then can be covered by objects lower in the Z-order list. So the doors automatically hide the prizes.|
|The actual door selection is handled in a MouseDown event exit. If no door has been selected yet, mark the clicked door as selected and randomly find an unselected closed door hiding a goat for the host to open. If a door has already been selected, then this is the player's second click and we can complete the game. That mainly means identifying if he switched doors and if he won or lost and adding the results to some totals buckets.|
|I did need to add a Busy flag to ignore extra clicks while processing a click.|
|After the explanation dialog has been displayed, we make visible a button that will play 1000 games at a time (without animation). The button click code is pretty much much a copy of the MouseDown exit code, except we simulate the clicks and skip the animation parts.|
You'll have to download and run the program to discover the answer and explanation for the original question.
|The game was originally posed to Marilyn Vos Savant in her weekly column in Parade magazine. It prompted 10,000 response letters, most of telling her that her answer was wrong (it wasn't). You can easily spend an hour or so investigating the results from a Google search on "car goats Marilyn".|
Modified: May 15, 2018
[Feedback] [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby All rights reserved.