### Search

 Search WWW Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

Support DFF - Shop

If you shop at Amazon anyway,  consider using this link.

We receive a few cents from each purchase.  Thanks

### Support DFF - Donate

If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

(Hint: If you can wait, current year calendars are usually on sale in January.)

### Contact

 Search DelphiForFun.org only

### Problem Description

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.

Assuming that it is more desirable to take home a car than a goat, what should you do? Does it make any difference?

### Background & Techniques

I ran across this as a math puzzle the other day, and got the answer wrong.    So as "punishment"   I forced  myself to write a program illustrating the  probability  principles involved.

Here are the program implementation highlights:

 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.

### Suggestions for Further Explorations

 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