Advertisement
shadowlucario50

C++ Avoid the Hole

Apr 24th, 2025 (edited)
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.30 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. int main(){
  7.     //Challenge: Use both break and continue functions in one loop.
  8.  
  9.     //Jump function? Sure.
  10.  
  11.     srand(time(NULL));
  12.     int hole;
  13.     int walk = 0;
  14.     int hopsleft = 5;
  15.     char playerInput;
  16.     bool winner = true;
  17.     bool cheat = false;
  18.     std::string predict;
  19.  
  20.     std::cout << "**AVOID THE HOLES**";
  21.     std::cout << "Holes are in the way! Get through without falling!\n";
  22.     std::cout << "**Controls**\nPredict: 'p' | Walk: 'w' | Hop: 'h' \n";
  23.     std::cout << "Hops Left: " << hopsleft << '\n';
  24.  
  25.     for(int i = 1; i <= 50; i++) {
  26.         //20% chance of a hole. Fail if 0 - 9.
  27.         hole = rand() % 50;
  28.        
  29.         std::cout << "Hole " << i << ": ";
  30.         if(hole < 10 && cheat == true) {
  31.             std::cout << "Hop here! ";
  32.         }
  33.         std::cin >> playerInput;
  34.  
  35.         if (playerInput == 'P') playerInput='p';
  36.         if (playerInput == 'W') playerInput='w';
  37.         if (playerInput == 'H') playerInput='h';
  38.         if (playerInput == 'C') playerInput='c';
  39.  
  40.         while(playerInput == 'p') {
  41.             std::cout << "Prediction: ";
  42.            
  43.             if(i <= 10) {
  44.                 if(hole < 10) {
  45.                     std::cout << "There is a hole here!\n";
  46.                 }
  47.                 else {
  48.                     std::cout << "Should be safe!\n";
  49.                 }
  50.             }
  51.             else if(i > 10 && i < 30){
  52.                 if(hole < 5) {
  53.                     std::cout << "There is a hole here!\n";
  54.                 }
  55.                 else if(hole < 20) {
  56.                     std::cout << "There's a chance of there being a hole here...\n";
  57.                 }
  58.                 else {
  59.                     std::cout << "Should be safe!\n";
  60.                 }
  61.             }
  62.             else {
  63.                 if(hole < 25) {
  64.                     std::cout << "There's a chance of there being a hole here...\n";
  65.                 }
  66.                 else if(hole >= 25 && hole < 35) {
  67.                     std::cout << "There could be a hole here!\n";
  68.                 }
  69.                 else {
  70.                     std::cout << "Should be safe...\n";
  71.                 }
  72.             }
  73.             std::cout << "Hole " << i << ": ";
  74.             std::cin >> playerInput;
  75.         }
  76.  
  77.         if(playerInput == 'h') {
  78.             if(hopsleft > 0) {
  79.                 hopsleft--;
  80.                 std::cout << "Hops Left: " << hopsleft << '\n';
  81.                 continue;
  82.             }
  83.             else {
  84.                 std::cout << "You have no hops left! You tired yourself out and can no longer go any further...\n**YOU LOSE!**";
  85.                 winner = false;
  86.                 break;
  87.             }
  88.         }
  89.  
  90.         else if(playerInput == 'w') {
  91.             if(hole < 10) {
  92.                 if(cheat==false) {
  93.                     std::cout << "You fell in a hole!\n**YOU LOSE!**\n";
  94.                 }
  95.                 else {
  96.                     std::cout << "You literally had cheats on. How did you lose?\n**YOU SUCK!**\n";
  97.                 }
  98.                 winner = false;
  99.                 break;
  100.             }
  101.             else {
  102.                 std::cout << "Safe!\n";
  103.             }
  104.         }
  105.         else if(playerInput == 'c') {
  106.             if(cheat==false)
  107.             {
  108.                 std::cout << "Cheats Activated! You will now be told when to hop!\n";
  109.                 cheat=true;
  110.                 continue;
  111.             }
  112.             else {
  113.                 std::cout << "Nice try. You're only get to skip one hole.\n**YOU LOSE!**\n";
  114.                 winner = false;
  115.                 break;
  116.             }
  117.         }
  118.         else {
  119.             std::cout << "Wrong key! You fall into a hole immediately!\n**YOU LOSE!**\n";
  120.             winner = false;
  121.             break;
  122.         }
  123.  
  124.         switch (i) {
  125.             case 5:
  126.                 hopsleft+=2;
  127.                 std::cout << "Two hops added!\n";
  128.                 break;
  129.             case 10:
  130.                 hopsleft+=2;
  131.                 std::cout << "Two hops added!\n";
  132.                 break;
  133.             case 15:
  134.                 hopsleft+=2;
  135.                 std::cout << "Two hops added!\n";
  136.                 break;
  137.             case 20:
  138.                 hopsleft+=2;
  139.                 std::cout << "Two hops added!\n";
  140.                 break;
  141.             case 25:
  142.                 hopsleft+=2;
  143.                 std::cout << "Two hops added!\n";
  144.                 break;
  145.             case 30:
  146.                 hopsleft+=2;
  147.                 std::cout << "Two hops added!\n";
  148.                 break;
  149.             case 35:
  150.                 hopsleft+=2;
  151.                 std::cout << "Two hops added!\n";
  152.                 break;
  153.             case 40:
  154.                 hopsleft++;
  155.                 std::cout << "One hop added!\n";
  156.                 break;
  157.             case 45:
  158.                 hopsleft++;
  159.                 std::cout << "One hop added!\n";
  160.                 break;
  161.             default:
  162.                 break;
  163.         }
  164.     }
  165.     if(winner == false) {
  166.         std::cout << "Try again sometime!";
  167.     }
  168.     else if(winner == true && cheat == true) {
  169.         std::cout << "**CONGRATS! YOU CHEATED THE GAME!**";
  170.     }
  171.     else {
  172.         std::cout << "**CONGRATS! YOU WIN!**";
  173.     }
  174.  
  175.     return 0;
  176. }
Tags: C++ game Code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement