//=========================================================================== // MONTY HALL PROBLEM //=========================================================================== //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include "Graphics.hpp" #include "mmsystem.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //=========================================================================== // VARIABLES //=========================================================================== int monitorWidth, monitorHeight; int spacingW, spacingH; //bool next = false; bool stop = false; bool skipDoors = false; int door; bool nextDoor = false; int montysDoor; int winningDoor; int wins = 0; int games = 0; float percentWins = 0; //=========================================================================== // FUNCTIONS //=========================================================================== void doorsVisible (bool show) { if (show) { Form1->ImageLeftDoor->Visible = true; Form1->ImageCenterDoor->Visible = true; Form1->ImageRightDoor->Visible = true; Form1->ImageIdleBanker->Visible = true; Form1->ImageLeftBanker->Visible = true; Form1->ImageCenterBanker->Visible = true; Form1->ImageRightBanker->Visible = true; Form1->LabelDoors->Visible = true; } else { Form1->ImageLeftDoor->Visible = false; Form1->ImageCenterDoor->Visible = false; Form1->ImageRightDoor->Visible = false; Form1->ImageIdleBanker->Visible = false; Form1->ImageLeftBanker->Visible = false; Form1->ImageCenterBanker->Visible = false; Form1->ImageRightBanker->Visible = false; Form1->LabelDoors->Visible = false; Form1->LabelPercentWins->Visible = false; } } //---------------------------------------------------------------- Monty Hall void doorsBegin (void) { games = 0; wins = 0; Form1->ImageIdleBanker->Picture = Form1->ImageBankerPick->Picture; PlaySound(MAKEINTRESOURCE(400), HInstance, SND_RESOURCE | SND_ASYNC); // next = false; // close Application->ProcessMessages(); again: // re-entry point winningDoor = random(3) + 1; Form1->ImageLeftDoor->Picture = Form1->ImageVaultClosed->Picture; Form1->ImageCenterDoor->Picture = Form1->ImageVaultClosed->Picture; Form1->ImageRightDoor->Picture = Form1->ImageVaultClosed->Picture; Form1->ImageIdleBanker->Picture = Form1->ImageBankerPick->Picture; Form1->ImageLeftBanker->Picture = NULL; Form1->ImageCenterBanker->Picture = NULL; Form1->ImageRightBanker->Picture = NULL; Form1->LabelDoors->Caption = "Pick a vault door to stand alongside..."; while (!nextDoor) { // pick first door if (stop) goto end; // if (next) goto end; Application->ProcessMessages(); } nextDoor = false; // next = false; Application->ProcessMessages(); Form1->ImageIdleBanker->Picture = NULL; if (door == 1) Form1->ImageLeftBanker->Picture = Form1->ImageBankerPick->Picture; if (door == 2) Form1->ImageCenterBanker->Picture = Form1->ImageBankerPick->Picture; if (door == 3) Form1->ImageRightBanker->Picture = Form1->ImageBankerPick->Picture; Form1->LabelDoors->Caption = "I'll show you where the gold is not..."; PlaySound(MAKEINTRESOURCE(300), HInstance, SND_RESOURCE | SND_ASYNC); // open Application->ProcessMessages(); Sleep(2000); // wait before Monty opens a door montysDoor = random(3) + 1; while (montysDoor == winningDoor || montysDoor == door) { montysDoor = random(3) + 1; } if (montysDoor == 1) Form1->ImageLeftDoor->Picture = Form1->ImageVaultOpenEmpty->Picture; if (montysDoor == 2) Form1->ImageCenterDoor->Picture = Form1->ImageVaultOpenEmpty->Picture; if (montysDoor == 3) Form1->ImageRightDoor->Picture = Form1->ImageVaultOpenEmpty->Picture; Form1->LabelDoors->Caption = "Pick a door to open. Either stay or switch..."; while (!nextDoor) { // pick second door if (stop) goto end; // if (next) goto end; Application->ProcessMessages(); } nextDoor = false; // next = false; Application->ProcessMessages(); if (door == winningDoor) { PlaySound(MAKEINTRESOURCE(300), HInstance, SND_RESOURCE | SND_ASYNC); // open Application->ProcessMessages(); Sleep(2000); // wait before the final door opens wins++; Form1->ImageLeftDoor->Picture = Form1->ImageVaultOpenEmpty->Picture; Form1->ImageCenterDoor->Picture = Form1->ImageVaultOpenEmpty->Picture; Form1->ImageRightDoor->Picture = Form1->ImageVaultOpenEmpty->Picture; Form1->ImageLeftBanker->Picture = NULL; Form1->ImageCenterBanker->Picture = NULL; Form1->ImageRightBanker->Picture = NULL; // wait before reveal if (door == 1) { Form1->ImageLeftDoor->Picture = Form1->ImageVaultOpenGold->Picture; Form1->ImageLeftBanker->Picture = Form1->ImageBankerWin->Picture; } if (door == 2) { Form1->ImageCenterDoor->Picture = Form1->ImageVaultOpenGold->Picture; Form1->ImageCenterBanker->Picture = Form1->ImageBankerWin->Picture; } if (door == 3) { Form1->ImageRightDoor->Picture = Form1->ImageVaultOpenGold->Picture; Form1->ImageRightBanker->Picture = Form1->ImageBankerWin->Picture; } Form1->LabelDoors->Caption = "Congratulations, you picked wisely!"; } else { PlaySound(MAKEINTRESOURCE(300), HInstance, SND_RESOURCE | SND_ASYNC); // open Application->ProcessMessages(); // new Sleep(2000); // wait before the final door opens // new Form1->ImageLeftDoor->Picture = Form1->ImageVaultOpenEmpty->Picture; Form1->ImageCenterDoor->Picture = Form1->ImageVaultOpenEmpty->Picture; Form1->ImageRightDoor->Picture = Form1->ImageVaultOpenEmpty->Picture; Form1->ImageLeftBanker->Picture = NULL; Form1->ImageCenterBanker->Picture = NULL; Form1->ImageRightBanker->Picture = NULL; // wait before reeal if (winningDoor == 1) Form1->ImageLeftDoor->Picture = Form1->ImageVaultOpenGold->Picture; if (winningDoor == 2) Form1->ImageCenterDoor->Picture = Form1->ImageVaultOpenGold->Picture; if (winningDoor == 3) Form1->ImageRightDoor->Picture = Form1->ImageVaultOpenGold->Picture; if (door == 1) Form1->ImageLeftBanker->Picture = Form1->ImageBankerLoose->Picture; if (door == 2) Form1->ImageCenterBanker->Picture = Form1->ImageBankerLoose->Picture; if (door == 3) Form1->ImageRightBanker->Picture = Form1->ImageBankerLoose->Picture; Form1->LabelDoors->Caption = "Sorry, you lost...!"; } Application->ProcessMessages(); games++; percentWins = (float(wins) / float(games)) * 100; Form1->LabelPercentWins->Visible = true; if (wins == 1) { Form1->LabelPercentWins->Caption = IntToStr(wins) + " win, out of " + IntToStr(games) + " = " + FloatToStrF(percentWins, ffNumber, 10, 0) + "% wins."; } else { Form1->LabelPercentWins->Caption = IntToStr(wins) + " wins, out of " + IntToStr(games) + " = " + FloatToStrF(percentWins, ffNumber, 10, 0) + "% wins."; } nextDoor = false; Application->ProcessMessages(); Sleep(2000); PlaySound(MAKEINTRESOURCE(400), HInstance, SND_RESOURCE | SND_ASYNC); // close Application->ProcessMessages(); if ( !(games >= 10 && percentWins >= 60) ) { // keep trying goto again; } // you win Application->ProcessMessages(); end: nextDoor = false; } //======================================================================= Run //======================================================================= Run //======================================================================= Run void run (void) { //----------------------------------------------------- Monty Hall Sleep(1000); doorsVisible(true); Form1->LabelDoors->Left = Form1->ImageIdleBanker->Left; Form1->LabelDoors->Caption = "What are the chances? Wait a moment..."; Form1->LabelDoors->Visible = true; Application->ProcessMessages(); Sleep(3000); Form1->LabelDoors->Caption = "Gather your gold..." + AnsiString(char(13)) + AnsiString(char(13)) + " You must score over 60% in 10 tries."; Application->ProcessMessages(); Sleep(6000); Form1->LabelDoors->Caption = ""; Application->ProcessMessages(); Form1->LabelDoors->Left = Form1->ImageIdleBanker->Left + 250; Application->ProcessMessages(); Sleep(3000); doorsBegin(); // executes Monty Hall doorsVisible(false); } //=========================================================================== // EVENT HANDLERS //=========================================================================== //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Randomize(); randomize(); monitorWidth = Monitor->Width; monitorHeight = Monitor->Height; Form1->Width = monitorWidth; Form1->Height = monitorHeight; Form1->Top = 0; Form1->Left = 0; Form1->LabelPlayTheOdds->Width = 1200; Form1->LabelPlayTheOdds->Height = 600; Form1->LabelPlayTheOdds->Left = 350; Form1->LabelPlayTheOdds->Top = 400; spacingW = (Form1->ClientWidth - 1500) / 4; Form1->ImageLeftDoor->Left = spacingW; Form1->ImageCenterDoor->Left = 2 * spacingW + 500; Form1->ImageRightDoor->Left = 3 * spacingW + 1000; Form1->ImageIdleBanker->Left = Form1->ImageCenterDoor->Left; Form1->ImageIdleBanker->Top = Form1->ImageLeftDoor->Top + 400; Form1->ImageLeftBanker->Left = Form1->ImageLeftDoor->Left + 20; Form1->ImageCenterBanker->Left = Form1->ImageCenterDoor->Left + 20; Form1->ImageRightBanker->Left = Form1->ImageRightDoor->Left + 20; Form1->ImageLeftDoor->AutoSize = true; Form1->ImageCenterDoor->AutoSize = true; Form1->ImageRightDoor->AutoSize = true; Form1->ImageLeftBanker->AutoSize = true; Form1->ImageCenterBanker->AutoSize = true; Form1->ImageRightBanker->AutoSize = true; Form1->ImageIdleBanker->AutoSize = true; Form1->LabelDoors->Top = Form1->ImageIdleBanker->Top + 100; Form1->LabelDoors->Left = Form1->ImageIdleBanker->Left + 250; Form1->LabelPercentWins->Top = Form1->ImageIdleBanker->Top + 150; Form1->LabelPercentWins->Left = Form1->ImageIdleBanker->Left + 250; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormActivate(TObject *Sender) { run(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ImageLeftDoorClick(TObject *Sender) { door = 1; nextDoor = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::ImageCenterDoorClick(TObject *Sender) { door = 2; nextDoor = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::ImageRightDoorClick(TObject *Sender) { door = 3; nextDoor = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::ImageLeftBankerClick(TObject *Sender) { door = 1; nextDoor = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::ImageCenterBankerClick(TObject *Sender) { door = 2; nextDoor = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::ImageRightBankerClick(TObject *Sender) { door = 3; nextDoor = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { stop = true; } //---------------------------------------------------------------------------