// To save this file, simply click Ctrl-S // Enter the filename as "game.c" // Change the "Save as" type located at the bottom of // the dialog box to "Plain Text (*.txt)" #include #include #include #include #include // Global Variables ////////////// char cardoornum, playerchoice, correct; int ChangeMind[2][2] = {0, 0, 0, 0}, TotalRuns; // Prototyping /////////////////// void InitialiseDoors(void); void AskPlayerForDoor(void); void PlayerChangesMind(void); void Statistics(int runs, int CurrentRun); void Conclusion(int runs, int CurrentRun); // Functions //////////////////// void InitialiseDoors(void) { // Set the door with the car cardoornum = rand() % 10; correct = -1; } void AskPlayerForDoor(void) { // Set the player choice playerchoice = rand() % 10; if (playerchoice == cardoornum) correct = 1; else correct = 0; } void PlayerChangesMind(void) { // Check if player changes his mind char change = rand() % 2; if (change == 0) { if (correct == 1) ChangeMind[0][0]++; else ChangeMind[0][1]++; } else { if (correct == 0) ChangeMind[1][0]++; else ChangeMind[1][1]++; } } void Statistics(int runs, int CurrentRun) { double result = 0; gotoxy(1,1); printf("\nThe total number of runs = %d", runs); printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); printf("\nCurrent run : %d\n", CurrentRun); printf("\nIf the player does not change his mind :"); result = (double)ChangeMind[0][0] / (double)runs * 100.0; printf("\n - Win : %lf %", result); result = (double)ChangeMind[0][1] / (double)runs * 100.0; printf("\n - Lose : %lf %", result); printf("\n\nIf the player changes his mind :"); result = (double)ChangeMind[1][0] / (double)runs * 100.0; printf("\n - Win : %lf %", result); result = (double)ChangeMind[1][1] / (double)runs * 100.0; printf("\n - Lose : %lf %\n", result); } void Conclusion(int runs, int CurrentRun) { if (runs == CurrentRun) { if (ChangeMind[0][0] > ChangeMind[1][0]) printf("\nConclusion is that the player should stick to his original choice."); else if (ChangeMind[0][0] == ChangeMind[1][0]) printf("\nConclusion is that there is no difference."); else printf("\nConclusion is that the player should change to the other door."); } else printf("\nPress spacebar to speed up runs"); } main() { char c = 'p'; clrscr(); srand(200); TotalRuns = 10000; for (int j=1;j<=TotalRuns;j++) { InitialiseDoors(); AskPlayerForDoor(); PlayerChangesMind(); // Traps spacebar pressed if (bioskey(1) != 0) if (c != 32) c = bioskey(0); // Skip Statistics & Conclusion if // spacebar is pressed, i.e. if c == 32 if (c != 32) { Statistics(TotalRuns, j); Conclusion(TotalRuns, j); } } // If earlier spacebar was pressed // then print the final result. if (c == 32) { Statistics(TotalRuns, TotalRuns); Conclusion(TotalRuns, TotalRuns); } // Pause for user to see final result char wait = getch(); }