SHARE
TWEET

Blackjack

a guest Apr 1st, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>     // std::cout
  2. #include <algorithm>    // std::shuffle
  3. #include <array>        // std::array
  4. #include <random>       // std::default_random_engine
  5. #include <chrono>
  6. #include <string>
  7. #include <time.h>
  8. #include <stdio.h>  
  9. #include <fstream>
  10. #include <iostream>
  11. #include <cstdio>
  12.  
  13. using namespace std;
  14.  
  15. //int begin;
  16. //int info;
  17.  
  18. string login();
  19. struct User{
  20.     string username;
  21.     string password;
  22.     double cash = 1000;
  23.     int gain = 0;
  24.     int loss = 0;
  25.     int logins = 0;
  26.    
  27. };
  28.  
  29.  
  30. void createAccount(){
  31.     User userInfo;
  32.    
  33.     //for(int i = 0; i < 999; i++){
  34.        
  35.        
  36.         //Create account
  37.        
  38.         ofstream userFile;
  39.        
  40.         cout << "What would you like your username to be?\n";
  41.         cin >> userInfo.username;
  42.  
  43.         cout << "What would you like your password to be?\n";
  44.         cin >> userInfo.password;
  45.         userFile.open(userInfo.username + ".txt");
  46.         userFile << userInfo.username << endl;
  47.         userFile << userInfo.password << endl;
  48.         userFile << userInfo.cash << endl;
  49.         userFile << userInfo.gain << endl;
  50.         userFile << userInfo.loss << endl;
  51.         userFile << userInfo.logins << endl;
  52.         userFile.close();
  53.         /*if(userFile.is_open()){
  54.             cout << "exists" << endl;
  55.             exit(0);
  56.         } else if(!userFile.is_open()){
  57.             cout << "File doesn't exist yet, Creating new File" << endl;
  58.             userFile.open(userInfo.username + ".txt");
  59.             userFile.close();
  60.             DOES NOT WORK
  61.         }*/
  62.        
  63.        
  64.  
  65.        
  66.  
  67.  
  68. }
  69.  
  70. string login(){
  71.     User userInfo;
  72.     string username;
  73.     string name;
  74.     string pass;
  75.     string password;
  76.     fstream userFile;
  77.     cout << "Enter your username: ";
  78.     cin >> name;
  79.     userFile.open(name + ".txt");
  80.     cout << endl;
  81.     userFile >> username;
  82.    
  83.  
  84.     cout << "Enter your password: ";
  85.     cin >> pass;
  86.     cout << endl;
  87.     userFile >> password;
  88.    
  89.     if(name == username && pass == password){
  90.         cout << "You are successfully logged in!\n\n";
  91.     } else{
  92.         cout << "Login failed\n";
  93.         exit(0);
  94.     }
  95.     userFile.close();
  96.     return name;
  97. }
  98.  
  99.  
  100. int main (){
  101.     int choice = 1;
  102.    
  103.     fstream file;
  104.     User userInfo;
  105.     while(choice == 1 || choice == 2){
  106.         cout <<"Welcome! Please login (press 1) or create an account (press 2)\n";
  107.         cin >> choice;
  108.         if(choice == 2){
  109.             createAccount();
  110.             break;
  111.         } else if (choice == 1){
  112.             break;
  113.             login();
  114.         } else{
  115.             choice = 1;  
  116.         }
  117.     }
  118.     string user = login();
  119.     cout << "Hello " << user << "Would you like to view your account stats?(y/n)\n";
  120.     string inp;
  121.     cin >> inp;
  122.     if (inp == "y")
  123.         file.open(user + ".txt");
  124.         file >> userInfo.username;
  125.         file >> userInfo.password;
  126.         file >> userInfo.cash;
  127.         file >> userInfo.gain;
  128.         file >> userInfo.loss;
  129.         file >> userInfo.logins;
  130.        
  131.         cout << "Username: " << userInfo.username << endl;
  132.         cout << "Password: " << userInfo.password << endl;
  133.         cout << "Net Earnings: " << userInfo.cash << endl;
  134.         cout << "Gains: " << userInfo.gain << endl;
  135.         cout << "Losses: " << userInfo.loss << endl;
  136.         cout << "Logins " << userInfo.logins << endl;
  137.    
  138.  
  139.  
  140.     cout << "Welcome to a good ol' game of BLACKJACK! " << user << endl;
  141.     cout << "Would you like to see some information to help you understand the code? { 1 - yes , 2 - no} " << endl;
  142.     cout << "Here is some information:" << endl;
  143.     // This is the information for the user to read in the beginning so they can understand how to play.
  144.         cout << "1. Blackjack will be played with one deck of 52-card decks" << endl;
  145.         cout << "2. Aces may be counted as 1 or 11 points, 2 to 9 according to pip value, and tens and face cards count as ten points." << endl;
  146.         cout << "3. The value of a hand is the sum of the point values of the individual cards." << endl;
  147.         cout << "4. DO NOT PASS A HAND VALUE OF 21, or you will LOSE!" << endl;
  148.         cout << "5. The objective is to get a hand total of closer to 21 than the dealer without going over 21 (busting)." << endl;
  149.         cout << "6. At the start of a Blackjack game, the players and the dealer receive two cards each. The players' cards are normally dealt face up, while the dealer has one face down (called the hole card) and one face up." << endl;
  150.         cout << "7. PLEASE HAVE FUN and don't be afraid to bet more " << endl;
  151.         cout << "\n==================================================\n\n";
  152.                
  153.    
  154.    
  155.    
  156.    
  157.    
  158.     srand(time(0));
  159.  
  160.     int Ace = 1;
  161.     int Jack = 10;
  162.     int Queen = 10;
  163.     int King = 10;
  164.    
  165.    
  166.     //Array of cards
  167.     string suit[4] = {"Hearts","Diamonds","Spades","Clubs"};
  168.     int number[13] = {Ace,2,3,4,5,6,7,8,9,10,Jack,Queen,King};
  169.      
  170.  
  171.    
  172.     random_shuffle(begin(suit), end(suit));
  173.     random_shuffle(begin(number), end(number));
  174.    
  175.     int start = 0;
  176.     int card1 = number[0];
  177.     int card2 = number[1];
  178.    
  179.    
  180.    
  181.     cout << "You got " << card1 << " of " << suit[0] << " and " << card2 << " of " << suit[1] << endl;
  182.     int total = card1 + card2;
  183.    
  184.    
  185.     int n = 0;
  186.     for(int i = 2; i < 52; i++){
  187.         string response;
  188.         cout << "Would you like a hit? (Y/N)";
  189.         cin >> response;
  190.        
  191.         //int n = rand() % 3 + 0;
  192.        
  193.         if(response == "Y" || response == "y"){
  194.             cout << "You draw a " << number[i++] << " of " << suit[n++] << endl;
  195.             total += number[i++];
  196.             cout << "Your total is " << total << endl;
  197.            
  198.             if(total > 21){
  199.                 cout << "You went over 21. You lose";
  200.                 return 0;
  201.             }
  202.            
  203.             if(n > 3){
  204.                 n = 0;
  205.             } else{
  206.                 continue;
  207.             }
  208.            
  209.         }
  210.         else if(response == "N" || response == "n"){
  211.             cout << "Your cards add up to " << total << endl;
  212.             break;
  213.         }
  214.        
  215.        
  216.     }
  217.    
  218.     random_shuffle(begin(suit), end(suit));
  219.     random_shuffle(begin(number), end(number));
  220.    
  221.     int Dcard1 = number[0];
  222.     int Dcard2 = number[1];
  223.    
  224.    
  225.    
  226.    
  227.     cout << "The dealer draws " << Dcard1 << " of " << suit[0] << " and " << Dcard2 << " of " << suit[1] << endl;
  228.     int Dtotal = Dcard1 + Dcard2;
  229.     if(Dtotal > total){
  230.         cout << "Dealer drew " << Dtotal << " .You lose!";
  231.         file.open(user + ".txt");
  232.         file >> userInfo.username;
  233.         file >> userInfo.password;
  234.         file >> userInfo.cash;
  235.         file >> userInfo.gain;
  236.         file >> userInfo.loss;
  237.         file >> userInfo.logins;
  238.        
  239.         userInfo.cash -= 10;
  240.         userInfo.loss += 10;
  241.         userInfo.logins += 1;
  242.         file.close();
  243.        
  244.        
  245.         file.open(user + ".txt", fstream::out | fstream::trunc);
  246.         file << userInfo.username << endl;
  247.         file << userInfo.password << endl;
  248.         file << userInfo.cash << endl;
  249.         file << userInfo.gain << endl;
  250.         file << userInfo.loss << endl;
  251.         file << userInfo.logins << endl;
  252.         file.close();
  253.        
  254.     } else if (Dtotal < total){
  255.         cout << "Dealer drew " << Dtotal << " .You win!\n";
  256.        
  257.         file.open(user + ".txt");
  258.        
  259.         file >> userInfo.username;
  260.         file >> userInfo.password;
  261.         file >> userInfo.cash;
  262.         file >> userInfo.gain;
  263.         file >> userInfo.loss;
  264.         file >> userInfo.logins;
  265.        
  266.         userInfo.cash += 20;
  267.         userInfo.gain += 20;
  268.         userInfo.logins += 1;
  269.        
  270.  
  271.         file.close();
  272.        
  273.        
  274.         file.open(user + ".txt", fstream::out | fstream::trunc);
  275.         file << userInfo.username << endl;
  276.         file << userInfo.password << endl;
  277.         file << userInfo.cash << endl;
  278.         file << userInfo.gain << endl;
  279.         file << userInfo.loss << endl;
  280.         file << userInfo.logins << endl;
  281.         file.close();
  282.        
  283.     }
  284.    
  285.    
  286.    
  287.     return 0;  
  288.  
  289.    
  290. }
  291.  
  292.  
  293. /*
  294.  
  295. void IAP (){
  296.     string answer;
  297.     \
  298.     cout << "Before starting, would you like to buy anything to help your luck? (Y/N)\n";
  299.     cin >> answer;
  300.     if(response == "Y" || response == "y"){
  301.         cout << "You have chosen to spend money. You can buy tokens to spend in game. Each day has a special discount.\n";
  302.         cout << "You have 3 options to buy from. You can spend 10$ to buy 1000 tokens, 20$ to buy 2500 tokens, or 50$ to buy 7500 tokens"
  303.     }
  304. }
  305. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top