//=========================================================================== // The MONTY HALL PROBLEM //=========================================================================== #include #pragma hdrstop #include "Unit1.h" #include "stdlib.h" #include "time.h" #include // YOU NEED TO TYPE THIS IN //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //=========================================================================== // VARIABLES //=========================================================================== int winningDoor; int player; int monty; bool proceed = false; int turn = 1; int choice1; int choice2; int i; int wins, losses; float pcWins; int stays, switches; int games; AnsiString name; AnsiString ID; AnsiString strategy; //=========================================================================== // FUNCTIONS //=========================================================================== // FUNCTIONS: //---------------------------------------------------------------- SAVE STUFF void saveStuff (void) { if (Form1->SaveDialog1->Execute()) { ofstream outfile(Form1->SaveDialog1->FileName.c_str()); // assuming you're saving two arrays (cityX[] and cityY[]) of size "size": outfile << size << endl; for (int i = 0; i < size; i++) { // use " " and/or endl as record separators: outfile << cityX[i] << " " << cityY[i] << endl; } outfile.close(); } } void resetAll (void) { Form1->Height = 441; Form1->ButtonPlay->Caption = "Play"; Form1->ButtonPlay->Visible = true; wins = 0; losses = 0; pcWins = 0; games = 0; stays = 0; switches = 0; turn = 1; Form1->EditWins->Text = IntToStr(wins) + " wins"; Form1->EditLosses->Text = IntToStr(losses) + " losses"; Form1->EditPCWins->Text = String(pcWins) + " % wins"; Form1->EditStays->Text = IntToStr(stays) + " stays"; Form1->EditSwitches->Text = IntToStr(switches) + " switches"; Form1->EditGames->Text = IntToStr(games) + " games"; } void setup (void) { Form1->ButtonPlay->Visible = true; Form1->Image0->Picture = Form1->ImageDoorClosed->Picture; Form1->Image1->Picture = Form1->ImageDoorClosed->Picture; Form1->Image2->Picture = Form1->ImageDoorClosed->Picture; Form1->ImagePerson->Left = 212; Form1->ImagePerson->Top = 220; proceed = false; turn = 1; //Sleep(500); } void play (void) { setup(); winningDoor = random(3); Form1->ButtonPlay->Visible = false; Form1->LabelInstructions->Caption = "Pick a door."; Application->ProcessMessages(); // PLAYER MAKES A CHOICE while (proceed == false) { Application->ProcessMessages(); } Form1->ImagePerson->Left = choice1 * 300; Form1->ImagePerson->Top = 120; proceed = false; Application->ProcessMessages(); //Sleep(2000); // MONTY OPENS A DOOR WITH A GOAT for (i = 0; i < 3; i++) { while (monty == choice1 || monty == winningDoor) { monty = random(3); } } Sleep(1000); if (monty == 0) { Form1->Image0->Picture = Form1->ImageDoorOpenGoat->Picture; } if (monty == 1) { Form1->Image1->Picture = Form1->ImageDoorOpenGoat->Picture; } if (monty == 2) { Form1->Image2->Picture = Form1->ImageDoorOpenGoat->Picture; } turn = 2; Form1->LabelInstructions->Caption = "Stay or switch doors."; Application->ProcessMessages(); // PLAYER STAYS OR SWITCHES //Sleep(1000); while (proceed == false) { Application->ProcessMessages(); } Form1->ImagePerson->Left = choice2 * 300; Form1->ImagePerson->Top = 1200; proceed == false; Sleep(1000); turn = 3; if (choice2 == winningDoor) { Beep(1000, 60); wins++; } else { Beep(200, 200); losses++; } if (choice1 == choice2) stays++; else switches++; Form1->EditWins->Text = IntToStr(wins) + " wins"; Form1->EditLosses->Text = IntToStr(losses) + " losses"; games = wins + losses; pcWins = (float(wins) * 100) / games; Form1->EditPCWins->Text = FloatToStrF(pcWins, ffNumber, 10, 0) + " % wins"; Form1->EditStays->Text = IntToStr(stays) + " stays"; Form1->EditSwitches->Text = IntToStr(switches) + " switches"; Form1->EditGames->Text = IntToStr(games) + " games"; if (winningDoor == 0) { Form1->Image0->Picture = Form1->ImageDoorOpenGold->Picture; Form1->Image1->Picture = Form1->ImageDoorOpenGoat->Picture; Form1->Image2->Picture = Form1->ImageDoorOpenGoat->Picture; } if (winningDoor == 1) { Form1->Image0->Picture = Form1->ImageDoorOpenGoat->Picture; Form1->Image1->Picture = Form1->ImageDoorOpenGold->Picture; Form1->Image2->Picture = Form1->ImageDoorOpenGoat->Picture; } if (winningDoor == 2) { Form1->Image0->Picture = Form1->ImageDoorOpenGoat->Picture; Form1->Image1->Picture = Form1->ImageDoorOpenGoat->Picture; Form1->Image2->Picture = Form1->ImageDoorOpenGold->Picture; } Form1->LabelInstructions->Caption = ""; Application->ProcessMessages(); Sleep(2000); setup(); } //=========================================================================== // EVENT HANDLERS //=========================================================================== //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { randomize(); Form1->ImagePerson->Left = 212; Form1->ImagePerson->Top = 220; } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonPlayClick(TObject *Sender) { play(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonResetStatsClick(TObject *Sender) { resetAll(); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormShow(TObject *Sender) { resetAll(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Image0Click(TObject *Sender) { if (turn == 1) choice1 = 0; if (turn == 2) choice2 = 0; proceed = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::Image1Click(TObject *Sender) { if (turn == 1) choice1 = 1; if (turn == 2) choice2 = 1; proceed = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::Image2Click(TObject *Sender) { if (turn == 1) choice1 = 2; if (turn == 2) choice2 = 2; proceed = true; } //---------------------------------------------------------------------------