Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- string d1; //String to store the door contents in
- string d2;
- string d3;
- int choice; //int to store player's choice
- int r; //used for the random generator
- string switchChoice; //used to store if the user wants to switch
- int safeGoat; //used to store behind which door is a goat
- int switchDoor; //used to store to which door the player will switch if he decides to
- bool revealed = false; //used to prevent revealing 2 doors
- bool repeat; //used to either repeat or end the game
- string contchoice; //stores if the user wants to continue
- double wincount = 0;
- double gamecount = 0;
- //converts the numbers 1,2,3 to whatever's behind the respective door
- string doorConvert(int i) {
- if (i == 1) {
- return d1;
- }
- else if (i == 2) {
- return d2;
- }
- else if (i == 3) {
- return d3;
- }
- }
- //Mainly for text and getting the player's choice
- void intro() {
- cout << "Welcome to Let's Make a Deal!" << endl;
- cout << "Here are three doors:\nBehind 1 of these doors the Car! \nBehind the other 2 is a Goat" << endl;
- cout << "Door1\nDoor2\nDoor3\n";
- cout << "Pick a Door!";
- cin >> choice;
- }
- //function to randomize what's behind the door
- void randDoor() {
- srand(time(NULL));
- r = rand() % 3;
- if (r == 0) {
- d1 = "Car";
- d2 = "Goat";
- d3 = "Goat";
- }
- else if (r == 1) {
- d1 = "Goat";
- d2 = "Car";
- d3 = "Goat";
- }
- else if (r == 2) {
- d1 = "Goat";
- d2 = "Goat";
- d3 = "Car";
- }
- }
- //reveals a door with a goat behind it to the player
- void doorReveal() {
- if (choice == 1) {
- cout << "Let me reveal another Door for you:" << endl;
- cout << "Door1 <- Your pick" << endl; //userpick
- if (d2 == "Goat" && revealed == false) {
- cout << "Goat\n";
- safeGoat = 2;
- switchDoor = 3;
- revealed = true;
- }
- else {
- cout << "Door2\n";//reveal door2
- }
- if (d3 == "Goat" && revealed == false) {
- cout << "Goat\n";
- safeGoat = 3;
- switchDoor = 2;
- revealed = true;
- }
- else {
- cout << "Door3\n";//reveal door3
- }
- }
- else if (choice == 2) {
- cout << "Let me reveal another Door for you:" << endl;
- if (d1 == "Goat" && revealed == false) {
- cout << "Goat\n";
- safeGoat = 1;
- switchDoor = 3;
- revealed = true;
- }
- else {
- cout << "Door1\n"; //reveal door1
- }
- cout << "Door2 <- Your pick" << endl; //userpick
- if (d3 == "Goat" && revealed == false) {
- cout << "Goat\n";
- safeGoat = 3;
- switchDoor = 1;
- revealed = true;
- }
- else {
- cout << "Door3\n"; //reveal door3
- }
- }
- else if (choice == 3) {
- cout << "Let me reveal another Door for you:" << endl;
- if (d1 == "Goat" && revealed == false) {
- cout << "Goat\n";
- safeGoat = 1;
- switchDoor = 2;
- revealed = true;
- }
- else {
- cout << "Door1\n"; //reveal door1
- }
- if (d2 == "Goat" && revealed == false) {
- cout << "Goat\n";
- safeGoat = 2;
- switchDoor = 1;
- revealed = true;
- }
- else {
- cout << "Door2\n"; //reveal door2
- }
- cout << "Door3 <- Your pick" << endl;
- }
- }
- //asks the user if he wants to switch doors and ends the game by revealing all doors
- void switcheroo() {
- cout << "Do you want to switch your Door? " << endl;
- cin >> switchChoice;
- if (switchChoice == "yes" || switchChoice == "Y" || switchChoice == "Yes" || switchChoice == "YES" || switchChoice == "y") {
- cout << "You switched and have selected Door" << switchDoor << endl << endl;
- cout << d1 << endl;
- cout << d2 << endl;
- cout << d3 << endl;
- if (doorConvert(switchDoor) == "Car") {
- cout << endl << "Congratulations, you won the car!" << endl;
- wincount++;
- }
- else if (doorConvert(switchDoor) == "Goat") {
- cout << endl << "Better luck next time!" << endl;
- }
- }
- else {
- cout << "You decided to stick with your choice!" << endl;
- cout << d1 << endl;
- cout << d2 << endl;
- cout << d3 << endl;
- if (doorConvert(choice) == "Car") {
- cout << "Congratulations, you won the car!" << endl;
- wincount++;
- }
- else if (doorConvert(choice) == "Goat") {
- cout << endl << "Better luck next time!" << endl;
- }
- }
- }
- //to store if the user wants to play another game
- void cont(string decision) {
- if (decision == "y" || decision == "Y") {
- repeat = true;
- }
- else {
- repeat = false;
- }
- }
- double calculateWinPercentage() {
- double p = (wincount / gamecount) * 100;
- return p;
- }
- //the actual game progress line
- void game() {
- randDoor();
- intro();
- doorReveal();
- switcheroo();
- gamecount++;
- cout << endl << endl << "Games played: " << gamecount << endl;
- cout << "Games won: " << wincount << endl;
- cout << endl << "-------Win Percentage: " << calculateWinPercentage() << "%-------" << endl << endl;
- cout << "Do you want to play the game again with another random set of doors?" << endl;
- cin >> contchoice;
- cont(contchoice);
- }
- //lol main lol
- int main() {
- game();
- while (repeat == true) {
- game();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement