Advertisement
Porr011

Lesson 11 activity 6

May 8th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. void displayMenu();
  8. void startSimulation();
  9. void startGame();
  10. // function prototypes
  11.  
  12. //********************
  13. // main fucntion
  14. // displays menu
  15. // get choice from the user and calls upon the function they want
  16. //*****************
  17. int main ()
  18. {
  19.     // varible for the choice option
  20.     int choice;
  21.     displayMenu(); // calling upon the display menu function
  22.     cin >> choice;
  23.    
  24.     // choice vaules are eqaul to the number the user puts in
  25.     if (choice ==1)
  26.     {
  27.     startGame(); // gives the function for the game when enter 1
  28.     }
  29.     else if (choice ==2)
  30.     {
  31.     startSimulation(); // gives the function for the simulation when enter 2
  32.     }
  33.     else if (choice > 2)
  34.     {
  35.         cout << " Please enter a valid choice here: " ;
  36.         cin >> choice;
  37.     }
  38.     else if (choice < 1) // incase of invalid choice
  39.     {
  40.         cout << " Please enter a valid choice here: ";
  41.         cin >> choice;
  42.     }
  43.     return 0;
  44. }
  45.  
  46. //*************
  47. // Simulation function
  48. // takes 1000 turns and gives the results
  49. //*************
  50. void startSimulation ()
  51. {
  52.     srand( time(0));
  53.     int counter = 1000;
  54.     int winCounter = 0;
  55.     int winFirstCounter = 0;
  56.     // varibles for the for loop
  57.    
  58.     // for loop for
  59.     for (int i = 0; i < counter; ++i)
  60.     {
  61.         int prizeDoor = (1 + rand() % 3);
  62.         int firstChoice = (1 + rand() % 3);
  63.         bool switchDoor = (rand() % 2)==0;
  64.         // randomizing the prize door and the first choice
  65.         //varible for switching the door with a boolean function
  66.        
  67.         // loop for
  68.         if (!switchDoor && (firstChoice == prizeDoor))
  69.         {
  70.             ++winFirstCounter;
  71.             ++winCounter;
  72.         }
  73.         else if (switchDoor && (firstChoice != prizeDoor))
  74.         {
  75.             ++winCounter;
  76.         }
  77.     }
  78.     // cout statements of the results of the simulation
  79.     cout << "Out of 1000 games" << endl;
  80.     cout << (counter - winCounter) << " is the ammount of games lost " << endl;
  81.     cout << winCounter << " is the ammount of games won total " << endl;
  82.     cout << winFirstCounter << " is the ammount games won without change " << endl;
  83.    
  84.     int winWithChange = (winCounter - winFirstCounter);
  85.    
  86.     cout << winWithChange << " is the ammount of games won with change " << endl;
  87.     cout << "Percent won after change " << winWithChange* 100 / winCounter << "%" << endl;
  88.     cout << "Percent won without change " << winFirstCounter * 100 / winCounter << "%" << endl;
  89. }
  90.  
  91. //********************
  92. //Game mode function
  93. // Plays game normaly
  94. // gives the user the data
  95. // Then ask the user if they want to play again
  96. //*****************
  97. void startGame ()
  98. {
  99.     bool PlayAgain = true; // play again loop
  100. while(PlayAgain)
  101. {
  102.    
  103.     int choice, prizeDoor, openDoor, secondChoice = 0; // integer varibles for the game
  104.     char repick; // char varible for the repick
  105.     int doors = 3; // door varible se to 3
  106.    
  107.     srand ( time(NULL) );
  108.     prizeDoor = rand() % doors + 1; // making the prize door random
  109.    
  110.     cout << " Lets make a deal, There are three doors. One of these doors have your dream car." << endl;
  111.     cout << " Two of these doors have zonks and each game is random. " << endl;
  112.     cout << " What door do you want to choose? ";
  113.     cin >> choice; // intro and getting the users choice
  114.    
  115.     while ( choice > doors || choice < 1) {
  116.         cout << "Please retry: ";
  117.         cin >> choice;
  118.     } // while loop that doesnt let the user pick a door less than 1 and more than 3
  119.    
  120.     do
  121.     {
  122.         srand ( time(NULL) );
  123.         openDoor = rand() % doors + 1;
  124.     }
  125.     while (openDoor == prizeDoor || openDoor == choice);
  126.     cout << "Monty opens door " << openDoor << " and door " << openDoor << " has a zonk " << endl;
  127.     // loop for picking on of the wrong doors
  128.    
  129.     cout << "Would you like to pick a new door? type y for yes and n for no ";    
  130.     cin >> repick; // ask user to repick the door
  131.    
  132.     if (repick == 'y') // loop for if they agree to repick they get to pick the other door
  133.     {
  134.         cout << "What door do you want to choose? ";
  135.         cin >> secondChoice;
  136.         while (secondChoice == openDoor) // while statement if they pick the same door just incase
  137.         {
  138.             cout << "That door is open, retry: ";
  139.             cin >> secondChoice;
  140.         }
  141.     }
  142.  
  143. // part for finding the percent of wining by changing and winning by staying
  144. int winCounter = 0;
  145. int winFirstCounter = 0;
  146. // loop for stayWin = finding if the user won on there turn and how did they win
  147.     for(int i = 0; i < doors; i++)
  148.     {
  149.         if (prizeDoor == choice)
  150.         {
  151.             ++winFirstCounter;
  152.             ++winCounter;
  153.         }
  154.         else if (prizeDoor == secondChoice)
  155.         {
  156.           ++winCounter;  
  157.         }
  158.     }
  159.    
  160.     // cout statments for telling the user if they won or not.
  161.     if (choice == prizeDoor)
  162.     {
  163.     cout << "YOU WIN YOUR DREAM CAR!" << endl;
  164.     }
  165.     else if (secondChoice == prizeDoor)
  166.     {
  167.         cout << "YOU WIN YOUR DREAM CAR!" << endl;
  168.     }
  169.     else
  170.     cout << "YOU LOSE AND GET A ZONK!" << endl; // loop for wining or losing
  171.    
  172.     // cout statments for the percentage of winning
  173.     int winWithChange = (winCounter - winFirstCounter);
  174.     cout << "Percent won after change " << winWithChange* 100 / winCounter << "%" << endl;
  175.     cout << "Percent won without change " << winFirstCounter * 100 / winCounter << "%" << endl;
  176.  
  177.  // play again function    
  178.     char again;
  179. cout << "Would you like to play again? Enter y or n" << endl;
  180. cin >> again;
  181.     if (again != 'y')
  182.     {
  183.     PlayAgain = false;
  184.     }
  185. } // play again loop ends here
  186.    
  187. } // function for the game mode ends here
  188.  
  189. //******************************************************************************
  190. // Display menu function
  191. // Gives neat cout statements that the user sees and chooses based on the numbers
  192. //**********************************************************************************
  193. void displayMenu()
  194. {
  195.     cout << "Please choose the mode you want to run" << endl;
  196.     cout << " 1. Game " << endl;
  197.     cout << " 2. Simulation " << endl;
  198.     cout << " Enter the number here " << endl;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement