Advertisement
M3IY0U

Untitled

Nov 4th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. string d1; //String to store the door contents in
  9. string d2;
  10. string d3;
  11. int choice; //int to store player's choice
  12. int r; //used for the random generator
  13. string switchChoice; //used to store if the user wants to switch
  14. int safeGoat; //used to store behind which door is a goat
  15. int switchDoor; //used to store to which door the player will switch if he decides to
  16. bool revealed = false; //used to prevent revealing 2 doors
  17. bool repeat; //used to either repeat or end the game
  18. string contchoice; //stores if the user wants to continue
  19. double wincount = 0;
  20. double gamecount = 0;
  21.  
  22.  
  23.  
  24.                    
  25. //converts the numbers 1,2,3 to whatever's behind the respective door                  
  26. string doorConvert(int i) {
  27.     if (i == 1) {
  28.         return d1;
  29.     }
  30.     else if (i == 2) {
  31.         return d2;
  32.     }
  33.     else if (i == 3) {
  34.         return d3;
  35.     }
  36. }
  37.  
  38.  
  39. //Mainly for text and getting the player's choice
  40. void intro() {
  41.     cout << "Welcome to Let's Make a Deal!" << endl;
  42.     cout << "Here are three doors:\nBehind 1 of these doors the Car! \nBehind the other 2 is a Goat" << endl;
  43.     cout << "Door1\nDoor2\nDoor3\n";
  44.     cout << "Pick a Door!";
  45.     cin >> choice;
  46. }
  47.  
  48.  
  49. //function to randomize what's behind the door
  50. void randDoor() {
  51.     srand(time(NULL));
  52.     r = rand() % 3;
  53.     if (r == 0) {
  54.         d1 = "Car";
  55.         d2 = "Goat";
  56.         d3 = "Goat";
  57.     }
  58.     else if (r == 1) {
  59.         d1 = "Goat";
  60.         d2 = "Car";
  61.         d3 = "Goat";
  62.     }
  63.     else if (r == 2) {
  64.         d1 = "Goat";
  65.         d2 = "Goat";
  66.         d3 = "Car";
  67.     }
  68. }
  69.  
  70. //reveals a door with a goat behind it to the player
  71. void doorReveal() {
  72.     if (choice == 1) {
  73.         cout << "Let me reveal another Door for you:" << endl;
  74.         cout << "Door1 <- Your pick" << endl; //userpick
  75.         if (d2 == "Goat" && revealed == false) {
  76.             cout << "Goat\n";
  77.             safeGoat = 2;
  78.             switchDoor = 3;
  79.             revealed = true;
  80.         }
  81.         else {
  82.             cout << "Door2\n";//reveal door2
  83.         }
  84.         if (d3 == "Goat" && revealed == false) {
  85.             cout << "Goat\n";
  86.             safeGoat = 3;
  87.             switchDoor = 2;
  88.             revealed = true;
  89.         }
  90.         else {
  91.             cout << "Door3\n";//reveal door3
  92.         }
  93.     }
  94.     else if (choice == 2) {
  95.         cout << "Let me reveal another Door for you:" << endl;
  96.         if (d1 == "Goat" && revealed == false) {
  97.             cout << "Goat\n";
  98.             safeGoat = 1;
  99.             switchDoor = 3;
  100.             revealed = true;
  101.         }
  102.         else {
  103.             cout << "Door1\n"; //reveal door1
  104.         }
  105.         cout << "Door2 <- Your pick" << endl; //userpick
  106.         if (d3 == "Goat" && revealed == false) {
  107.             cout << "Goat\n";
  108.             safeGoat = 3;
  109.             switchDoor = 1;
  110.             revealed = true;
  111.         }
  112.         else {
  113.             cout << "Door3\n"; //reveal door3
  114.         }
  115.     }
  116.     else if (choice == 3) {
  117.         cout << "Let me reveal another Door for you:" << endl;
  118.         if (d1 == "Goat" && revealed == false) {
  119.             cout << "Goat\n";
  120.             safeGoat = 1;
  121.             switchDoor = 2;
  122.             revealed = true;
  123.         }
  124.         else {
  125.             cout << "Door1\n"; //reveal door1
  126.         }
  127.         if (d2 == "Goat" && revealed == false) {
  128.             cout << "Goat\n";
  129.             safeGoat = 2;
  130.             switchDoor = 1;
  131.             revealed = true;
  132.         }
  133.         else {
  134.             cout << "Door2\n"; //reveal door2
  135.         }
  136.         cout << "Door3 <- Your pick" << endl;
  137.     }
  138.  
  139. }
  140.  
  141.  
  142. //asks the user if he wants to switch doors and ends the game by revealing all doors
  143. void switcheroo() {
  144.     cout << "Do you want to switch your Door? " << endl;
  145.     cin >> switchChoice;
  146.     if (switchChoice == "yes" || switchChoice == "Y" || switchChoice == "Yes" || switchChoice == "YES" || switchChoice == "y") {
  147.         cout << "You switched and have selected Door" << switchDoor << endl << endl;
  148.         cout << d1 << endl;
  149.         cout << d2 << endl;
  150.         cout << d3 << endl;
  151.         if (doorConvert(switchDoor) == "Car") {
  152.             cout << endl << "Congratulations, you won the car!" << endl;
  153.             wincount++;
  154.         }
  155.         else if (doorConvert(switchDoor) == "Goat") {
  156.             cout << endl << "Better luck next time!" << endl;
  157.         }
  158.     }
  159.     else {
  160.         cout << "You decided to stick with your choice!" << endl;
  161.         cout << d1 << endl;
  162.         cout << d2 << endl;
  163.         cout << d3 << endl;
  164.         if (doorConvert(choice) == "Car") {
  165.             cout << "Congratulations, you won the car!" << endl;
  166.             wincount++;
  167.         }
  168.         else if (doorConvert(choice) == "Goat") {
  169.             cout << endl << "Better luck next time!" << endl;
  170.         }
  171.  
  172.     }
  173. }
  174.  
  175.  
  176. //to store if the user wants to play another game
  177. void cont(string decision) {
  178.     if (decision == "y" || decision == "Y") {
  179.         repeat = true;
  180.     }
  181.     else {
  182.         repeat = false;
  183.     }
  184. }
  185.  
  186.  
  187. double calculateWinPercentage() {
  188.     double p = (wincount / gamecount) * 100;
  189.     return p;
  190. }
  191.  
  192. //the actual game progress line
  193. void game() {
  194.     randDoor();
  195.     intro();
  196.     doorReveal();
  197.     switcheroo();
  198.     gamecount++;
  199.     cout << endl << endl << "Games played: " << gamecount << endl;
  200.     cout << "Games won: " << wincount << endl;
  201.     cout << endl << "-------Win Percentage: " << calculateWinPercentage() << "%-------" << endl << endl;
  202.     cout << "Do you want to play the game again with another random set of doors?" << endl;
  203.     cin >> contchoice;
  204.     cont(contchoice);
  205. }
  206.  
  207.  
  208.  
  209.  
  210.  
  211. //lol main lol
  212. int main() {
  213.     game();
  214.     while (repeat == true) {
  215.         game();
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement