Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- //Randomizes the player's cards, the computer's cards, and the amount of cards the computer drew.
- void GenNumbers(int player[], int comp[], int psize, int &csize)
- {
- srand(time(0));
- csize = 1 + rand()%10;
- for(int i = 0; i < psize; i++)
- {
- player[i] = 1 + rand()%10;
- }
- for(int x = 0; x < csize; x++)
- {
- comp[x] = 1 + rand()%10;
- }
- }
- //Displays the game's results.
- void DisplayResults(int pwin, int cwin, int draw, int rounds, string player)
- {
- cout << "Here are the stats for this game: \n\n";
- cout << " Number of rounds played: " << rounds << "\n";
- cout << " Your wins, " << player << ": " << pwin << "\n";
- cout << " Computer Wins: " << cwin << "\n";
- cout << " Draws: " << draw << "\n\n";
- }
- int main()
- {
- int pcards, ccards, pwin, cwin, draw, psum, csum, round;
- int player[10], comp[10];
- char r, waffle;
- string name;
- ofstream gamesaves ("saves.txt", ios::app);
- pwin = 0;
- cwin = 0;
- draw = 0;
- round = 1;
- //Allows the player to enter their name.
- cout << "Welcome to Twenty One! What's your name? ";
- getline(cin, name);
- do
- {
- //Resets the sum of the cards each round.
- psum = 0;
- csum = 0;
- system("cls");
- cout << "Round " << round;
- //Asks the user how many cards they would like.
- cout << "\n\nHow many cards would you like " << name << "? ";
- cin >> pcards;
- //While the amount of cards are greater than 10, the user must choose a different amount of cards.
- while(pcards > 10)
- {
- cout << name << ", you cannot have more than 10 cards. Please re-enter your amount of cards: ";
- cin >> pcards;
- }
- //Calls the GenNumbers function in order to randomize numbers.
- GenNumbers(player, comp, pcards, ccards);
- cout << "\nYour cards: ";
- //Displays the player's cards.
- for(int u = 0; u < pcards; u++)
- {
- cout << player[u] << " ";
- }
- cout << "\n\nComputer's Cards: ";
- //Displays the computer's cards.
- for(int y = 0; y < ccards; y++)
- {
- cout << comp[y] << " ";
- }
- //Adds the player's cards.
- for(int a = 0; a < pcards; a++)
- {
- psum += player[a];
- }
- //Adds the computer's cards.
- for(int uter = 0; uter < ccards; uter++)
- {
- csum += comp[uter];
- }
- cout << "\n\n\n";
- //If both sums are greater than or equal to 21, then it's a draw.
- if(psum >= 21 && csum >= 21)
- {
- cout << "DRAW!";
- draw += 1;
- }
- //If the player's sum is greater than or equal to 21 and the computer's sum is less than 21, the computer wins.
- else if(psum >= 21 && csum < 21)
- {
- cout << "The computer has won this time.";
- cwin += 1;
- }
- //If the computer's sum is greater than or equal to 21 and the player's sum is less than 21, the player wins.
- else if(csum >= 21 && psum < 21)
- {
- cout << "You win this round, " << name << "!";
- pwin += 1;
- }
- //If the two sums are both equal, it's a draw.
- else if(psum == csum)
- {
- cout << "DRAW!";
- draw += 1;
- }
- //If the player's sum is larger, the player wins.
- else if(psum > csum)
- {
- cout << "You win this round " << name << "!";
- pwin += 1;
- }
- //If the computer's sum is larger, the computer wins.
- else if(psum < csum)
- {
- cout << "The computer has won this time.";
- cwin += 1;
- }
- //Asks the player if they would like to play another round.
- cout << "\n\nWould you like to play another round(y/n): ";
- cin >> r;
- //If the player responds "yes" (or y in this case), the round number increases by one.
- if(r == 'y' || r == 'Y')
- {
- round += 1;
- }
- }while(r == 'y' || r == 'Y');
- system("cls");
- //Calls the function DisplayResults to show the stats of the game.
- DisplayResults(pwin, cwin, draw, round, name);
- //Asks the player if they would like to save the stats of this game.
- cout << "Would you like to save this game(y/n): ";
- cin >> waffle;
- //If the player responds, "yes," the game will be saved to saves.txt.
- if(waffle == 'y' || waffle == 'Y')
- {
- gamesaves << name << "'s Game:\n\n";
- gamesaves << " " << name << "'s wins: " << pwin << "\n";
- gamesaves << " Computer wins: " << cwin << "\n";
- gamesaves << " Draws: " << draw << "\n";
- gamesaves << " Number of rounds: " << round << "\n\n----------------------------\n\nj";
- system("cls");
- cout << "Game saved.\n";
- }
- //End of the program.
- cout << "Game over.\n\n";
- system("pause");
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement