Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- void PrintDoors(unsigned int car, unsigned int chosenDoor, bool*opened);
- int main()
- {
- srand((unsigned)time(NULL));
- unsigned int wins=0, winsStick=0, winsSwitch=0;
- for(unsigned int game=1;; ++game) {
- unsigned int car = rand()%3;
- bool opened[3] = {0,0,0};
- unsigned int chosenDoor=10, switchDoor, chooseSwitch=10;
- cout << "Game " << game << endl << "---------" << endl;
- //Contestant chooses initial door
- PrintDoors(car, chosenDoor, opened);
- while(chosenDoor<1 || chosenDoor>3) {
- cout << "Choose a door (1,2,3): ";
- cin >> chosenDoor ;
- }
- --chosenDoor;
- //Monty opens door
- if(chosenDoor==car)
- opened[ (car + rand()%2 + 1) % 3 ] = 1;
- else if((chosenDoor+1)%3 != car)
- opened[ (chosenDoor+1)%3 ] = 1;
- else if((car+1)%3 != chosenDoor)
- opened[ (car+1)%3 ] = 1;
- cout << endl;
- PrintDoors(car, chosenDoor, opened);
- //Record result for switching
- switchDoor = (chosenDoor+1)%3;
- if(opened[switchDoor])
- switchDoor = (switchDoor+1)%3;
- if(switchDoor==car)
- ++winsSwitch;
- //Record result for sticking
- if(chosenDoor==car)
- ++winsStick;
- //Contestant chooses to switch or stick
- while(chooseSwitch<0 || chooseSwitch>1) {
- cout << "Stick (0) or switch (1)?: ";
- cin >> chooseSwitch;
- }
- if(chooseSwitch==1)
- chosenDoor = switchDoor;
- //Find game result
- opened[0] = opened[1] = opened[2] = 1;
- cout << endl;
- PrintDoors(car, chosenDoor, opened);
- cout << endl;
- if(chosenDoor==car) {
- cout << "*** You win the car! ***" << endl << endl;
- ++wins;
- } else
- cout << "*** You win a goat. ***" << endl << endl;
- cout << "Won " << wins << " cars in " << game << " games, p=" << static_cast<float>(wins)/game << "" << endl;
- cout << "By sticking every time, you would have won " << winsStick<< " cars in " << game << " games, p=" << static_cast<float>(winsStick)/game << endl;
- cout << "By switching every time, you would have won " << winsSwitch << " cars in " << game << " games, p=" << static_cast<float>(winsSwitch)/game << endl << endl;
- }
- return 0;
- }
- void PrintDoors(unsigned int car, unsigned int chosenDoor, bool*opened)
- {
- for(int door=0; door<3; ++door) {
- cout << door+1;
- if(door == chosenDoor)
- cout << "*";
- else
- cout << " ";
- if(!opened[door])
- cout << "[x]";
- else if(car!=door)
- cout << "[Goat]";
- else
- cout << "[Car]";
- cout << " ";
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement