Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include "Crash.h"
  2.  
  3. Crash::Crash()
  4.     :balance(100)
  5. {}
  6.  
  7. void Crash::wrongBA()
  8. {
  9.     std::cout << "Wrong bet amount. Hit enter to try again.";
  10.     std::cin.ignore();
  11.     std::cin.get();
  12.     menu();
  13. }
  14.  
  15. void Crash::pickRandom()
  16. {
  17.     srand(time(NULL));
  18.     random_var = rand() % 200 + 1;
  19. }
  20.  
  21. void Crash::menu()
  22. {
  23.     std::cout << "\033[2J\033[1;1H";
  24.     std::cout << "Balance: $" << balance << std::endl;
  25.     std::cout << "Bet amount: ";
  26.     std::cin >> bet_amount;
  27.  
  28.     if(std::cin.good())
  29.     {
  30.         if(bet_amount > balance || bet_amount <= 0)
  31.         {
  32.             wrongBA();
  33.         }
  34.         else
  35.         {
  36.             std::cout << "Where will it crash(1-200): ";
  37.             std::cin >> crash_guess;
  38.             if(std::cin.good())
  39.             {
  40.                 game();
  41.             }
  42.             else
  43.             {
  44.                 std::cin.clear();
  45.                 std::cin.ignore();
  46.                 menu();
  47.             }
  48.         }
  49.     }
  50.     else
  51.     {
  52.         std::cin.clear();
  53.         std::cin.ignore();
  54.         menu();
  55.     }
  56. }
  57.  
  58. void Crash::win()
  59. {
  60.     std::cout << "\033[2J\033[1;1H";
  61.     win_amount = bet_amount / 100 * counter;;
  62.     balance += win_amount;
  63.     std::cout << "You won $" << win_amount << std::endl;
  64.     std::cout << "Hit enter to play again";
  65.     std::cin.ignore();
  66.     std::cin.get();
  67.     menu();
  68. }
  69.  
  70. void Crash::lose()
  71. {
  72.     std::cout << "\033[2J\033[1;1H";
  73.     balance -= bet_amount;
  74.     std::cout << "You lost $" << bet_amount << std::endl;
  75.     std::cout << "Hit enter to play again";
  76.     std::cin.ignore();
  77.     std::cin.get();
  78.     menu();
  79. }
  80.  
  81. void Crash::game()
  82. {
  83.     pickRandom();
  84.     counter = 0;
  85.     for(;;)
  86.     {
  87.         std::cout << counter << std::endl;
  88.         counter += 1;
  89.         usleep(100000);
  90.         if(counter == random_var)
  91.         {
  92.             lose();
  93.         }
  94.         else if(counter == crash_guess)
  95.         {
  96.             win();
  97.         }
  98.     }
  99. }
  100.  
  101. void Crash::play()
  102. {
  103.     menu();
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement