Advertisement
JustACodingStudent

Twenty One

Jun 10th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <ctime>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. //Randomizes the player's cards, the computer's cards, and the amount of cards the computer drew.
  9. void GenNumbers(int player[], int comp[], int psize, int &csize)
  10. {
  11.     srand(time(0));
  12.     csize = 1 + rand()%10;
  13.  
  14.     for(int i = 0; i < psize; i++)
  15.     {
  16.         player[i] = 1 + rand()%10;
  17.     }
  18.     for(int x = 0; x < csize; x++)
  19.     {
  20.         comp[x] = 1 + rand()%10;
  21.     }
  22. }
  23.  
  24. //Displays the game's results.
  25. void DisplayResults(int pwin, int cwin, int draw, int rounds, string player)
  26. {
  27.     cout << "Here are the stats for this game: \n\n";
  28.     cout << "     Number of rounds played: " << rounds << "\n";
  29.     cout << "     Your wins, " << player << ": " << pwin << "\n";
  30.     cout << "     Computer Wins: " << cwin << "\n";
  31.     cout << "     Draws: " << draw << "\n\n";
  32. }
  33.  
  34. int main()
  35. {
  36.     int pcards, ccards, pwin, cwin, draw, psum, csum, round;
  37.     int player[10], comp[10];
  38.     char r, waffle;
  39.     string name;
  40.     ofstream gamesaves ("saves.txt", ios::app);
  41.     pwin = 0;
  42.     cwin = 0;
  43.     draw = 0;
  44.     round = 1;
  45.     //Allows the player to enter their name.
  46.     cout << "Welcome to Twenty One! What's your name? ";
  47.     getline(cin, name);
  48.     do
  49.     {
  50.         //Resets the sum of the cards each round.
  51.         psum = 0;
  52.         csum = 0;
  53.         system("cls");
  54.         cout << "Round " << round;
  55.         //Asks the user how many cards they would like.
  56.         cout << "\n\nHow many cards would you like " << name << "? ";
  57.         cin >> pcards;
  58.        
  59.         //While the amount of cards are greater than 10, the user must choose a different amount of cards.
  60.         while(pcards > 10)
  61.         {
  62.             cout << name << ", you cannot have more than 10 cards. Please re-enter your amount of cards: ";
  63.             cin >> pcards;
  64.         }
  65.  
  66.         //Calls the GenNumbers function in order to randomize numbers.
  67.         GenNumbers(player, comp, pcards, ccards);
  68.         cout << "\nYour cards: ";
  69.  
  70.         //Displays the player's cards.
  71.         for(int u = 0; u < pcards; u++)
  72.         {
  73.             cout << player[u] << " ";
  74.         }
  75.  
  76.         cout << "\n\nComputer's Cards: ";
  77.  
  78.         //Displays the computer's cards.
  79.         for(int y = 0; y < ccards; y++)
  80.         {
  81.             cout << comp[y] << " ";
  82.         }
  83.  
  84.         //Adds the player's cards.
  85.         for(int a = 0; a < pcards; a++)
  86.         {
  87.             psum += player[a];
  88.         }
  89.  
  90.         //Adds the computer's cards.
  91.         for(int uter = 0; uter < ccards; uter++)
  92.         {
  93.             csum += comp[uter];
  94.         }
  95.  
  96.         cout << "\n\n\n";
  97.  
  98.         //If both sums are greater than or equal to 21, then it's a draw.
  99.         if(psum >= 21 && csum >= 21)
  100.         {
  101.             cout << "DRAW!";
  102.             draw += 1;
  103.         }
  104.         //If the player's sum is greater than or equal to 21 and the computer's sum is less than 21, the computer wins.
  105.         else if(psum >= 21 && csum < 21)
  106.         {
  107.             cout << "The computer has won this time.";
  108.             cwin += 1;
  109.         }
  110.         //If the computer's sum is greater than or equal to 21 and the player's sum is less than 21, the player wins.
  111.         else if(csum >= 21 && psum < 21)
  112.         {
  113.             cout << "You win this round, " << name << "!";
  114.             pwin += 1;
  115.         }
  116.         //If the two sums are both equal, it's a draw.
  117.         else if(psum == csum)
  118.         {
  119.             cout << "DRAW!";
  120.             draw += 1;
  121.         }
  122.         //If the player's sum is larger, the player wins.
  123.         else if(psum > csum)
  124.         {
  125.             cout << "You win this round " << name << "!";
  126.             pwin += 1;
  127.         }
  128.         //If the computer's sum is larger, the computer wins.
  129.         else if(psum < csum)
  130.         {
  131.             cout << "The computer has won this time.";
  132.             cwin += 1;
  133.         }
  134.  
  135.         //Asks the player if they would like to play another round.
  136.         cout << "\n\nWould you like to play another round(y/n): ";
  137.         cin >> r;
  138.  
  139.         //If the player responds "yes" (or y in this case), the round number increases by one.
  140.         if(r == 'y' || r == 'Y')
  141.         {
  142.             round += 1;
  143.         }
  144.     }while(r == 'y' || r == 'Y');
  145.     system("cls");
  146.     //Calls the function DisplayResults to show the stats of the game.
  147.     DisplayResults(pwin, cwin, draw, round, name);
  148.     //Asks the player if they would like to save the stats of this game.
  149.     cout << "Would you like to save this game(y/n): ";
  150.     cin >> waffle;
  151.  
  152.     //If the player responds, "yes," the game will be saved to saves.txt.
  153.     if(waffle == 'y' || waffle == 'Y')
  154.     {
  155.         gamesaves << name << "'s Game:\n\n";
  156.         gamesaves << "        " << name << "'s wins: " << pwin << "\n";
  157.         gamesaves << "        Computer wins: " << cwin << "\n";
  158.         gamesaves << "        Draws: " << draw << "\n";
  159.         gamesaves << "        Number of rounds: " << round << "\n\n----------------------------\n\nj";
  160.         system("cls");
  161.         cout << "Game saved.\n";
  162.     }
  163.     //End of the program.
  164.     cout << "Game over.\n\n";
  165.     system("pause");
  166.     return(0);
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement