Advertisement
shadowlucario50

C++ Choose a Door

Apr 29th, 2025 (edited)
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cmath>
  4. #include <iomanip>
  5. #include <climits>
  6.  
  7. //Make a code with 3 CPUs. Choose a door and try to get a door none of the CPUs choose. Add a new door each round until the player wins.
  8.  
  9. int cpu(int door);
  10. int door(int round);
  11. void doorCount(int door);
  12. bool doorCheck(int doorChoice, int cpuDoor);
  13. void cpuStillAlive(bool cpu, int cpuDoor, int cpuNum);
  14.  
  15. int main() {
  16.  
  17.     srand(time(0));
  18.  
  19.     int round = 1;
  20.     int doors = 0;
  21.     int doorChoice = 0;
  22.     int cpu1door = 0;
  23.     int cpu2door = 0;
  24.     int cpu3door = 0;
  25.     bool cpu1 = true;
  26.     bool cpu2 = true;
  27.     bool cpu3 = true;
  28.  
  29.     std::cout << "**Welcome to Choose a Door!**\n\n";
  30.     std::cout << "Rules: Choose a door and eliminate all the CPUs before time runs out!\n";
  31.  
  32.     do{
  33.         doors = door(round);
  34.        
  35.         std::cout << '\n';
  36.  
  37.         if(doors == 1) {
  38.             break;
  39.         }
  40.  
  41.         if(doors > 2){
  42.             std::cout << "Round " << round << "!\n";
  43.         }
  44.         else if(doors == 2){
  45.             std::cout << "Final Round!\n";
  46.         }
  47.  
  48.        
  49.  
  50.         doorCount(doors);
  51.         do {
  52.             std::cin >> doorChoice;
  53.             std::cin.clear();
  54.             std::cin.ignore(INT_MAX,'\n');
  55.         }while (doorChoice < 1 || doorChoice > doors);
  56.        
  57.        
  58.        
  59.         if(cpu1 == true) {
  60.             cpu1door = cpu(doors);
  61.             cpu1 = doorCheck(doorChoice,cpu1door);
  62.             cpuStillAlive(cpu1, cpu1door, 1);
  63.         }
  64.        
  65.         if(cpu2 == true) {
  66.             cpu2door = cpu(doors);
  67.             cpu2 = doorCheck(doorChoice,cpu2door);
  68.             cpuStillAlive(cpu2, cpu2door, 2);
  69.         }
  70.        
  71.         if(cpu3 == true) {
  72.             cpu3door = cpu(doors);
  73.             cpu3 = doorCheck(doorChoice,cpu3door);
  74.             cpuStillAlive(cpu3, cpu3door, 3);
  75.         }
  76.  
  77.         if(cpu1 == true || cpu2 == true || cpu3 == true){
  78.             round++;
  79.         }
  80.  
  81.     }while(cpu1 == true || cpu2 == true || cpu3 == true);
  82.    
  83.     if(cpu1 == true || cpu2 == true || cpu3 == true) {
  84.         std::cout << "\nCPUs win!";
  85.     }
  86.     else {
  87.         std::cout << "\nPlayer wins!";
  88.     }
  89.    
  90.     return 0;
  91. }
  92.  
  93. int cpu(int door) {
  94.     int doorChoice;
  95.  
  96.     doorChoice = rand() % door + 1;
  97.    
  98.     return doorChoice;
  99. }
  100.  
  101. int door(int round) {
  102.     int door;
  103.  
  104.     //Start with 10 Doors
  105.     //Subtract a door each round.
  106.     for(int i = 1; i <= (11 - round); i++){
  107.         door = i;
  108.     }
  109.    
  110.     return door;
  111. }
  112.  
  113. void doorCount(int door) {
  114.     for(int i = 1; i <= door; i++){
  115.         if(i % 3 != 0) {
  116.             std::cout << i << ": Door " << i << '\t';
  117.         }
  118.         else{
  119.             std::cout << i << ": Door " << i << '\n';
  120.         }
  121.     }
  122. }
  123.  
  124. bool doorCheck(int doorChoice, int cpuDoor) {
  125.     bool cpuSafe = true;
  126.    
  127.     if(doorChoice == cpuDoor) {
  128.         cpuSafe = false;
  129.     }
  130.    
  131.     return cpuSafe;
  132. }
  133.  
  134. void cpuStillAlive(bool cpu, int cpuDoor, int cpuNum){
  135.     std::cout << "CPU " << cpuNum << " chose " << cpuDoor << "! ";
  136.    
  137.     if(cpu == true) {
  138.         std::cout << "CPU " << cpuNum << " is still in!\n";
  139.     }
  140.     else{
  141.         std::cout << "CPU " << cpuNum << " is out!\n";
  142.     }
  143. }
  144.  
  145.  
Tags: C++ coding
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement