Advertisement
Guest User

Untitled

a guest
Nov 27th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.67 KB | None | 0 0
  1. /*
  2. Programmer: Ben Diegel
  3. Program: Casino Blackjack
  4. */
  5.  
  6.  
  7. #include<iostream>
  8. #include<fstream>
  9. #include<string>
  10. #include<iomanip>
  11. #include<vector>
  12. #include<cstdlib>
  13. #include<time.h>
  14. #include<stdlib.h>
  15. // TODO DELETE UNECCESARY DIRECTIVES TO HELP SPEED UP PROGRAM
  16. using namespace std;
  17.  
  18. // class to hold all the player info
  19. class Player {
  20. public:
  21.     string name = "user", hand;
  22.     double funds = 100, betAmount;
  23.     int score, wins = 0, losses = 0, cardTotal = 0;
  24. };
  25.  
  26.  
  27. //prototypes
  28. string startScreen(fstream &userNameFile, fstream &passwordFile);
  29. void mainMenu(Player &P1, Player &CPU, fstream &userNameFile, fstream &passwordFile, fstream &statsFile);
  30. void lineDraw(int x);
  31. void singlePlayer(Player &P1, Player &CPU);
  32. int deck(int x);
  33. int bet(Player &P1);
  34. bool playerChoice(vector<int>&P1Hand, Player &P1);
  35. void winning(Player &P1, int CPU, vector<int>P1Hand, vector<int>CPUHand);
  36. int dealerAdd(vector<int>&CPUHand, Player CPU);
  37. int handTotal(Player Play1, vector<int>PlayHand);
  38. void singlePlayerUpdate(Player &P1, Player &CPU, vector<int>&P1Hand, vector<int>&CPUHand);
  39. int aceCheck(vector<int>&P1Hand);
  40. void displayStats(Player P1);
  41.  
  42. int main() {
  43.     Player P1, P2, CPU; // declaring all players
  44.  
  45.     fstream userNameFile, passwordFile; // declaring files for user and pass
  46.     fstream statsFile;
  47.  
  48.     userNameFile.open("usernames.txt", ios::app | ios::in); // creates the critical files if not found
  49.     passwordFile.open("passwords.txt", ios::app | ios::in);
  50.  
  51.     P1.name = startScreen(userNameFile, passwordFile); // start up screen and gets the username
  52.  
  53.     string statsFileName = P1.name + "_statsFile.txt"; // creates the stats file for each user
  54.     statsFile.open(statsFileName, ios::app | ios::in);
  55.     if (statsFile) { // if its not empty it enters the stats into the player variables
  56.         statsFile >> P1.wins;
  57.         statsFile >> P1.losses;
  58.         statsFile >> P1.funds;
  59.     }
  60.  
  61.     mainMenu(P1, CPU, userNameFile, passwordFile, statsFile); // if it passes the input val it goes to a second main
  62.  
  63.     userNameFile.close(); // closes files at the end
  64.     passwordFile.close();
  65. }
  66.  
  67. //start screen for login
  68. string startScreen(fstream &userNameFile, fstream &passwordFile) {
  69.     int userInput = 0, i = 0;
  70.     string userName, password, fileUser, filePass, temp;
  71.     vector<string> userNames; // vectors so much easier
  72.     vector<string> passwords;
  73.  
  74.     lineDraw(49); // neat lil box
  75.     cout << "\n|\t\t\t\t\t\t|\n"
  76.         "|\t\t(1)LOGIN\t\t\t|\n"
  77.         "|\t\t(2)NEW PROFILE\t\t\t|\n"
  78.         "|\t\t\t\t\t\t|\n";
  79.     lineDraw(49);
  80.     cout << endl;
  81.  
  82.     // todo add better input val and user prompts
  83.     while (userInput != 1 || userInput != 2) {
  84.         cout << ">>";
  85.         cin >> userInput;
  86.  
  87.         if (userInput == 2) { // creates a new user and enters it into the username/password files
  88.             cout << "Please enter a username.\n>>";
  89.             cin >> userName;
  90.             userNameFile << userName << endl;
  91.  
  92.             cout << "Please enter a password " << userName << ".\n>>";
  93.             cin >> password;
  94.  
  95.             passwordFile << password << endl;
  96.             return userName;
  97.         }
  98.         else if (userInput == 1) {
  99.             userNameFile >> temp;
  100.             while (userNameFile) { // populates a vector with all the known usernames
  101.                 userNames.push_back(temp);
  102.                 userNameFile >> temp;
  103.             }
  104.             passwordFile >> temp;
  105.             while (passwordFile) { // same with passwords
  106.                 passwords.push_back(temp);
  107.                 passwordFile >> temp;
  108.             }
  109.  
  110.             cout << "Please enter your username.\n>>";
  111.             cin >> userName;
  112.             for (i = 0; i<userNames.size(); i++) { // checks to see if the entered user name is within its database
  113.                 if (userName == userNames[i]) {
  114.                     cout << "Please enter your password " << userNames[i] << "\n>>";
  115.                     cin >> password;
  116.  
  117.                     if (password == passwords[i]) { // compares to its corrisponding password
  118.                         return userName;
  119.                     }
  120.                     else {
  121.                         cout << "Incorrect password" << endl;
  122.                         main();
  123.                     }
  124.                 }
  125.                 else {
  126.                     continue;
  127.                 }
  128.             }
  129.             cout << "No user with that name." << endl;
  130.             main();
  131.         }
  132.     }
  133. }
  134.  
  135. // main menu of the game
  136. void mainMenu(Player &P1, Player &CPU, fstream &userNameFile, fstream &passwordFile, fstream &statsFile) {
  137.     while (true) {
  138.         char userInput;
  139.         system("cls");
  140.         cout << "\t\t--Welcome to Blackjack CASINO!!!--\n\n";
  141.         cout << "Player: " << P1.name << endl;
  142.         cout << "1. Player VS CPU\n";
  143.         cout << "2. Multiple Players VS CPU\n";
  144.         cout << "3. Player Stats\n";
  145.         cout << "4. Exit\n\n>>";
  146.  
  147.         // todo add input val
  148.         cin >> userInput;
  149.         if (userInput == '1') {
  150.             singlePlayer(P1, CPU);
  151.         }
  152.         else if (userInput == '2') { // couldnt finish this Dx
  153.                                      //multiPlayer(P1);
  154.         }
  155.         else if (userInput == '3') {
  156.             displayStats(P1);
  157.         }
  158.         else if (userInput == '4') {
  159.             statsFile << P1.wins << endl << P1.losses << endl << P1.funds << endl;
  160.             userNameFile.close(); // closes files at the end
  161.             passwordFile.close();
  162.             statsFile.close();
  163.             exit(0);
  164.         }
  165.     }
  166.  
  167. }
  168. void displayStats(Player P1) { // displays the player stats
  169.     string a;
  170.     cout << "\nWins: " << P1.wins << endl;
  171.     cout << "Losses: " << P1.losses << endl;
  172.     cout << "Funds: " << P1.funds << endl;
  173.     cout << "--Press enter to continue back to menu--\n";
  174.     getline(cin, a);
  175.     cin.ignore();
  176. }
  177.  
  178. // single player logic
  179. void singlePlayer(Player &P1, Player &CPU) {
  180.     int i;
  181.     int card1, card2;
  182.     vector<int>P1Hand; // vectors for hands
  183.     vector<int>CPUHand;
  184.  
  185.     P1.betAmount = bet(P1); // get the bet amount before dealing
  186.  
  187.                             // TODO MAKE THIS A LOOP SOMEHOW, WILL MAKE EVERYTHING EASIER
  188.                             //deal 2 cards each i tried a loop but kept getting perdictrable results
  189.     card1 = deck(15); // these numbers im passong are for a more random result
  190.     card2 = deck(16);
  191.     P1Hand.push_back(card1);
  192.     P1Hand.push_back(card2);
  193.     card1 = deck(12);
  194.     card2 = deck(11);
  195.     CPUHand.push_back(card1);
  196.     CPUHand.push_back(card2);
  197.  
  198.     char aceChoice;
  199.  
  200.     // checks to see if the dealt cards have an ace
  201.     aceCheck(P1Hand);
  202.  
  203.     // gets the total of 2 cards (TODO MAKE THIS INTO A FUNCTION)
  204.     P1.cardTotal = handTotal(P1, P1Hand);
  205.  
  206.     // TODO MAKE THIS INTO A LOOP OR LOOP AND FUNCTION
  207.     singlePlayerUpdate(P1, CPU, P1Hand, CPUHand);
  208.  
  209.  
  210. }
  211.  
  212. // this function is used as the loop within the single player game
  213. void singlePlayerUpdate(Player &P1, Player &CPU, vector<int>&P1Hand, vector<int>&CPUHand) {
  214.     bool flag = false;
  215.     int cpuFinalTotal;
  216.     while (!flag) {
  217.         system("cls");
  218.         cout << "\n\nCPU top card: " << CPUHand[0] << endl;
  219.         cout << "Your bet amount is " << P1.betAmount << endl << endl;
  220.         cout << "--Your cards--" << endl;
  221.         for (int i = 0; i<P1Hand.size(); i++) {
  222.             cout << "Card " << i + 1 << ": " << P1Hand[i] << endl; // lists all your current cards
  223.         }
  224.         cout << "Cards total = " << P1.cardTotal << endl << endl;
  225.         flag = playerChoice(P1Hand, P1);
  226.         P1.cardTotal = handTotal(P1, P1Hand);
  227.         CPU.cardTotal = handTotal(CPU, CPUHand);
  228.     }
  229.     cpuFinalTotal = dealerAdd(CPUHand, CPU);
  230.     winning(P1, cpuFinalTotal, P1Hand, CPUHand);
  231. }
  232.  
  233. // checks to see if there is an ace, and if there is asks the user how it wants to play it
  234. int aceCheck(vector<int>&P1Hand) {
  235.     char aceChoice;
  236.     int i;
  237.     for (i = 0; i < P1Hand.size(); i++) {
  238.         if (P1Hand[i] == 1) {
  239.             cout << "\nYou were dealt and ACE, would you like it to be counted as a (1)one or (2)eleven?\n";
  240.             cout << "--Your current cards are--\n";
  241.             for (int r = 0; r < P1Hand.size(); r++) {
  242.                 cout << "Card " << r + 1 << ": " << P1Hand[r] << endl;
  243.             }
  244.             cout << "\n>>";
  245.             while (true) {
  246.                 cin >> aceChoice;
  247.                 if (aceChoice == '1') {
  248.                     P1Hand[i] = 1;
  249.                     return 0;
  250.                 }
  251.                 else if (aceChoice == '2') {
  252.                     P1Hand[i] = 11;
  253.                     return 0;
  254.                 }
  255.                 else {
  256.                     cout << "Please only enter the number 1 (1) or 2 (11).\n>>";
  257.                     continue;
  258.                 }
  259.             }
  260.         }
  261.     }
  262.  
  263. }
  264.  
  265. // totals up the cards in the players hand
  266. int handTotal(Player Play1, vector<int>PlayHand) {
  267.     int i;
  268.     Play1.cardTotal = 0;
  269.     for (i = 0; i<PlayHand.size(); i++) {
  270.         Play1.cardTotal += PlayHand[i];
  271.     }
  272.     return Play1.cardTotal;
  273. }
  274.  
  275. // this is the deck, it will output a single random card
  276. int deck(int x) {
  277.     const int DECK_SIZE = 10;
  278.     int randomNum;
  279.     int deck[DECK_SIZE] = { 1,2,3,4,5,6,7,8,9,10 }; // TODO MAKE SURE TO ADD FUNCTION FOR 1 (1 can be 1 or 11)
  280.  
  281.     srand(time(NULL)); // set the random seed to users time
  282.  
  283.     for (int i = 0; i <x; i++) { // used to make it more random. ie generateing 10 (if x == 10) random numbers and using the 10th
  284.         randomNum = rand() % 10 + 0;
  285.     }
  286.     return deck[randomNum];
  287.  
  288. }
  289.  
  290. // this is the player choice menu. can hit stand split double down or buy insurance
  291. bool playerChoice(vector<int>&P1Hand, Player &P1) {
  292.     char userInput, aceChoice;
  293.     int card;
  294.     cout << "Would you like to (1)Hit, (2)Stand, (3)2x-Down\n>>";
  295.     cin >> userInput;
  296.     // todo input val
  297.     if (userInput == '1') {
  298.         card = deck(10);
  299.  
  300.         // had to make another ace check custom to the card choice
  301.         if (card == 1) {
  302.             cout << "You were dealt and ACE, would you like it to be counted as a (1)one or (2)eleven?\n";
  303.             cout << "--Your current cards are--\n";
  304.             for (int r = 0; r < P1Hand.size(); r++) {
  305.                 cout << "Card " << r + 1 << ": " << P1Hand[r] << endl;
  306.             }
  307.             cout << ">>";
  308.             while (true) {
  309.                 cin >> aceChoice;
  310.                 if (aceChoice == '1') {
  311.                     card = 1;
  312.                     break;
  313.                 }
  314.                 else if (aceChoice == '2') {
  315.                     card = 11;
  316.                     break;
  317.                 }
  318.                 else {
  319.                     cout << "Please only enter the number 1 (1) or 2 (11).\n>>";
  320.                     continue;
  321.                 }
  322.             }
  323.         }
  324.  
  325.         lineDraw(15);
  326.         cout << "You were dealt a " << card;
  327.         lineDraw(15);
  328.         P1Hand.push_back(card);
  329.         cout << "\n\n--Press enter to continue--\n>>";
  330.         string a;
  331.         getline(cin, a);
  332.         cin.ignore();
  333.         return false;
  334.     }
  335.     else if (userInput == '2') {
  336.         return true;
  337.     }
  338.     else if (userInput == '3') { // this is for doubling down your bet
  339.         P1.betAmount = P1.betAmount * 2;
  340.         cout << "You multiplied your bet to " << P1.betAmount << " and have to stand.";
  341.         cout << "\n\n--Press enter to continue--\n>>";
  342.         string a;
  343.         getline(cin, a);
  344.         cin.ignore();
  345.         return true;
  346.     }
  347. }
  348.  
  349. // calculates the winner. TODO NEED TO ADD WIN LOSS RATIO, BET WINNING, AND REPLAY BUTTON
  350. void winning(Player &P1, int CPU, vector<int>P1Hand, vector<int>CPUHand) {
  351.     int i;
  352.     string a;
  353.     system("cls");
  354.     cout << "player final score " << P1.cardTotal << endl;
  355.     cout << "CPU final score " << CPU << endl;
  356.  
  357.     // winning calculation
  358.     if (P1.cardTotal > 21) {
  359.         cout << "You lose!\n\n";
  360.         P1.losses += 1;
  361.         P1.funds -= P1.betAmount;
  362.     }
  363.     else if (P1.cardTotal == 21) {
  364.         cout << "You win!\n\n";
  365.         P1.wins += 1;
  366.         double earnings;
  367.         earnings = P1.betAmount * 2;
  368.         cout << "You've won $" << earnings << "!\n";
  369.         P1.funds += earnings;
  370.     }
  371.     else if (P1.cardTotal > CPU && P1.cardTotal <= 21) {
  372.         cout << "You win!\n\n";
  373.         P1.wins += 1;
  374.         double earnings;
  375.         earnings = P1.betAmount + (.5 * P1.betAmount);
  376.         cout << "You've won $" << earnings << "!\n";
  377.         P1.funds += earnings;
  378.     }
  379.     else {
  380.         cout << "You lose!\n\n";
  381.         P1.losses += 1;
  382.         P1.funds -= P1.betAmount;
  383.     }
  384.     cout << "--Press enter to continue back to menu--\n";
  385.     getline(cin, a);
  386.     cin.ignore();
  387. }
  388.  
  389. // allows the player to bet and subtracts it from total funds
  390. int bet(Player &P1) {
  391.     int amt;
  392.     while(true){
  393.         cout << "How much would you like to bet? (10, 20, 50, 100, 200?)\n"
  394.             << "You have: " << P1.funds << "\n>>";
  395.         cin >> amt;
  396.         if (amt > P1.funds) {
  397.             cout << "You do not have enough money for that!\n";
  398.             continue;;
  399.         }
  400.         else {
  401.             break;
  402.         }
  403.     }
  404. return amt;
  405.  
  406. }
  407.  
  408. // dealer tries to hit a soft 17. not working just right so i need to fix
  409. int dealerAdd(vector<int>&CPUHand, Player CPU) {
  410.     int card;
  411.     while (CPU.cardTotal < 17) {
  412.         card = deck(10);
  413.         CPUHand.push_back(card);
  414.         CPU.cardTotal = CPU.cardTotal + card;
  415.     }
  416.     return CPU.cardTotal;
  417. }
  418.  
  419. // draws a neat line (used for boxes)
  420. void lineDraw(int x) {
  421.     int i;
  422.     for (i = 0; i < x; i++) {
  423.         cout << "-";
  424.     }
  425. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement