Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <cmath>
- #include <iomanip>
- #include <climits>
- //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.
- int cpu(int door);
- int door(int round);
- void doorCount(int door);
- bool doorCheck(int doorChoice, int cpuDoor);
- void cpuStillAlive(bool cpu, int cpuDoor, int cpuNum);
- int main() {
- srand(time(0));
- int round = 1;
- int doors = 0;
- int doorChoice = 0;
- int cpu1door = 0;
- int cpu2door = 0;
- int cpu3door = 0;
- bool cpu1 = true;
- bool cpu2 = true;
- bool cpu3 = true;
- std::cout << "**Welcome to Choose a Door!**\n\n";
- std::cout << "Rules: Choose a door and eliminate all the CPUs before time runs out!\n";
- do{
- doors = door(round);
- std::cout << '\n';
- if(doors == 1) {
- break;
- }
- if(doors > 2){
- std::cout << "Round " << round << "!\n";
- }
- else if(doors == 2){
- std::cout << "Final Round!\n";
- }
- doorCount(doors);
- do {
- std::cin >> doorChoice;
- std::cin.clear();
- std::cin.ignore(INT_MAX,'\n');
- }while (doorChoice < 1 || doorChoice > doors);
- if(cpu1 == true) {
- cpu1door = cpu(doors);
- cpu1 = doorCheck(doorChoice,cpu1door);
- cpuStillAlive(cpu1, cpu1door, 1);
- }
- if(cpu2 == true) {
- cpu2door = cpu(doors);
- cpu2 = doorCheck(doorChoice,cpu2door);
- cpuStillAlive(cpu2, cpu2door, 2);
- }
- if(cpu3 == true) {
- cpu3door = cpu(doors);
- cpu3 = doorCheck(doorChoice,cpu3door);
- cpuStillAlive(cpu3, cpu3door, 3);
- }
- if(cpu1 == true || cpu2 == true || cpu3 == true){
- round++;
- }
- }while(cpu1 == true || cpu2 == true || cpu3 == true);
- if(cpu1 == true || cpu2 == true || cpu3 == true) {
- std::cout << "\nCPUs win!";
- }
- else {
- std::cout << "\nPlayer wins!";
- }
- return 0;
- }
- int cpu(int door) {
- int doorChoice;
- doorChoice = rand() % door + 1;
- return doorChoice;
- }
- int door(int round) {
- int door;
- //Start with 10 Doors
- //Subtract a door each round.
- for(int i = 1; i <= (11 - round); i++){
- door = i;
- }
- return door;
- }
- void doorCount(int door) {
- for(int i = 1; i <= door; i++){
- if(i % 3 != 0) {
- std::cout << i << ": Door " << i << '\t';
- }
- else{
- std::cout << i << ": Door " << i << '\n';
- }
- }
- }
- bool doorCheck(int doorChoice, int cpuDoor) {
- bool cpuSafe = true;
- if(doorChoice == cpuDoor) {
- cpuSafe = false;
- }
- return cpuSafe;
- }
- void cpuStillAlive(bool cpu, int cpuDoor, int cpuNum){
- std::cout << "CPU " << cpuNum << " chose " << cpuDoor << "! ";
- if(cpu == true) {
- std::cout << "CPU " << cpuNum << " is still in!\n";
- }
- else{
- std::cout << "CPU " << cpuNum << " is out!\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement