Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- void displayMenu();
- void startSimulation();
- void startGame();
- // function prototypes
- //********************
- // main fucntion
- // displays menu
- // get choice from the user and calls upon the function they want
- //*****************
- int main ()
- {
- // varible for the choice option
- int choice;
- displayMenu(); // calling upon the display menu function
- cin >> choice;
- // choice vaules are eqaul to the number the user puts in
- if (choice ==1)
- {
- startGame(); // gives the function for the game when enter 1
- }
- else if (choice ==2)
- {
- startSimulation(); // gives the function for the simulation when enter 2
- }
- else if (choice > 2)
- {
- cout << " Please enter a valid choice here: " ;
- cin >> choice;
- }
- else if (choice < 1) // incase of invalid choice
- {
- cout << " Please enter a valid choice here: ";
- cin >> choice;
- }
- return 0;
- }
- //*************
- // Simulation function
- // takes 1000 turns and gives the results
- //*************
- void startSimulation ()
- {
- srand( time(0));
- int counter = 1000;
- int winCounter = 0;
- int winFirstCounter = 0;
- // varibles for the for loop
- // for loop for
- for (int i = 0; i < counter; ++i)
- {
- int prizeDoor = (1 + rand() % 3);
- int firstChoice = (1 + rand() % 3);
- bool switchDoor = (rand() % 2)==0;
- // randomizing the prize door and the first choice
- //varible for switching the door with a boolean function
- // loop for
- if (!switchDoor && (firstChoice == prizeDoor))
- {
- ++winFirstCounter;
- ++winCounter;
- }
- else if (switchDoor && (firstChoice != prizeDoor))
- {
- ++winCounter;
- }
- }
- // cout statements of the results of the simulation
- cout << "Out of 1000 games" << endl;
- cout << (counter - winCounter) << " is the ammount of games lost " << endl;
- cout << winCounter << " is the ammount of games won total " << endl;
- cout << winFirstCounter << " is the ammount games won without change " << endl;
- int winWithChange = (winCounter - winFirstCounter);
- cout << winWithChange << " is the ammount of games won with change " << endl;
- cout << "Percent won after change " << winWithChange* 100 / winCounter << "%" << endl;
- cout << "Percent won without change " << winFirstCounter * 100 / winCounter << "%" << endl;
- }
- //********************
- //Game mode function
- // Plays game normaly
- // gives the user the data
- // Then ask the user if they want to play again
- //*****************
- void startGame ()
- {
- bool PlayAgain = true; // play again loop
- while(PlayAgain)
- {
- int choice, prizeDoor, openDoor, secondChoice = 0; // integer varibles for the game
- char repick; // char varible for the repick
- int doors = 3; // door varible se to 3
- srand ( time(NULL) );
- prizeDoor = rand() % doors + 1; // making the prize door random
- cout << " Lets make a deal, There are three doors. One of these doors have your dream car." << endl;
- cout << " Two of these doors have zonks and each game is random. " << endl;
- cout << " What door do you want to choose? ";
- cin >> choice; // intro and getting the users choice
- while ( choice > doors || choice < 1) {
- cout << "Please retry: ";
- cin >> choice;
- } // while loop that doesnt let the user pick a door less than 1 and more than 3
- do
- {
- srand ( time(NULL) );
- openDoor = rand() % doors + 1;
- }
- while (openDoor == prizeDoor || openDoor == choice);
- cout << "Monty opens door " << openDoor << " and door " << openDoor << " has a zonk " << endl;
- // loop for picking on of the wrong doors
- cout << "Would you like to pick a new door? type y for yes and n for no ";
- cin >> repick; // ask user to repick the door
- if (repick == 'y') // loop for if they agree to repick they get to pick the other door
- {
- cout << "What door do you want to choose? ";
- cin >> secondChoice;
- while (secondChoice == openDoor) // while statement if they pick the same door just incase
- {
- cout << "That door is open, retry: ";
- cin >> secondChoice;
- }
- }
- // part for finding the percent of wining by changing and winning by staying
- int winCounter = 0;
- int winFirstCounter = 0;
- // loop for stayWin = finding if the user won on there turn and how did they win
- for(int i = 0; i < doors; i++)
- {
- if (prizeDoor == choice)
- {
- ++winFirstCounter;
- ++winCounter;
- }
- else if (prizeDoor == secondChoice)
- {
- ++winCounter;
- }
- }
- // cout statments for telling the user if they won or not.
- if (choice == prizeDoor)
- {
- cout << "YOU WIN YOUR DREAM CAR!" << endl;
- }
- else if (secondChoice == prizeDoor)
- {
- cout << "YOU WIN YOUR DREAM CAR!" << endl;
- }
- else
- cout << "YOU LOSE AND GET A ZONK!" << endl; // loop for wining or losing
- // cout statments for the percentage of winning
- int winWithChange = (winCounter - winFirstCounter);
- cout << "Percent won after change " << winWithChange* 100 / winCounter << "%" << endl;
- cout << "Percent won without change " << winFirstCounter * 100 / winCounter << "%" << endl;
- // play again function
- char again;
- cout << "Would you like to play again? Enter y or n" << endl;
- cin >> again;
- if (again != 'y')
- {
- PlayAgain = false;
- }
- } // play again loop ends here
- } // function for the game mode ends here
- //******************************************************************************
- // Display menu function
- // Gives neat cout statements that the user sees and chooses based on the numbers
- //**********************************************************************************
- void displayMenu()
- {
- cout << "Please choose the mode you want to run" << endl;
- cout << " 1. Game " << endl;
- cout << " 2. Simulation " << endl;
- cout << " Enter the number here " << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement