Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Case Study: Black Jack
- Jarod Beaumariage, Brandi Sumey, Desmond Hull 4-10-2017*/
- #include <iostream>
- #include <string>
- #include <windows.h>
- #include <iomanip>
- #include <ctime>
- #include "MyConsole.h"
- using namespace std;
- void DrawCard(int &card)
- {
- /* Generates a random card between an A and a K
- A random card value from 2 to 4 returned as card*/
- srand (time(NULL));
- card = rand() % 12 + 2;
- }
- //==================================================================================================================
- void NameCard(int value, string &name)
- {
- /* Names each card based on value given
- value >= 2 and value <= 14
- Name of card returned as name*/
- if (value == 2)
- {
- name = "2";
- }
- else if (value == 3)
- {
- name = "3";
- }
- else if (value == 4)
- {
- name = "4";
- }
- else if (value == 5)
- {
- name = "5";
- }
- else if (value == 6)
- {
- name = "6";
- }
- else if (value == 7)
- {
- name = "7";
- }
- else if (value == 8)
- {
- name = "8";
- }
- else if (value == 9)
- {
- name = "9";
- }
- else if (value == 10)
- {
- name = "10";
- }
- else if (value == 11)
- {
- name = "A";
- }
- else if (value == 12)
- {
- name = "J";
- }
- else if (value == 13)
- {
- name = "K";
- }
- else
- {
- name = "Q";
- }
- }
- //==================================================================================================================
- void PlayAgain(char &again)
- {
- /*Gives the player the option to play again
- Again returned as either y or n*/
- //Deal again
- cout << "\nDo you want another hand dealt? Enter either 'y' or 'n': ";
- cin >> again;
- ClearScreen();
- //Error in again
- if(again != 'y' && again != 'n')
- {
- do
- {
- ClearScreen();
- cout << "Error! Please enter either 'y' or 'n': ";
- cin >> again;
- ClearScreen();
- }while(again != 'y' && again != 'n');
- }
- }
- //==================================================================================================================
- void DealAmount(int &number)
- {
- /* Asks the user how many cards they would like dealt
- The number of cards the user enters is returned as number*/
- cout << "How many cards do you want dealt? ";
- cin >> number;
- ClearScreen();
- }
- //==================================================================================================================
- void CardSum(int val1, int val2, int val3, int val4, int &total)
- {
- /*Takes the values of three cards as val1, val2, and val3, changes the value to 10 if a value is greater than 10, sums them, and outputs their totals
- val1 >= 0 && val1 <= 14, val2 >= 0 && val2 <= 14, val3 >= 0 && val3 <= 14, val4 >= 0 && val4 <= 14
- Returns the sum of the values as total*/
- if (val1 == 12 || val1 == 13 || val1 == 14)
- {
- val1 = 10;
- }
- if (val2 == 12 || val2 == 13 || val2 == 14)
- {
- val2 = 10;
- }
- if (val3 == 12 || val3 == 13 || val3 == 14)
- {
- val3 = 10;
- }
- if (val4 == 12 || val4 == 13 || val4 == 14)
- {
- val4 = 10;
- }
- total = 0;
- total = val1 + val2 + val3 + val4;
- }
- //==================================================================================================================
- void dealuser(int &total)
- {
- /*Deals user desired amount of cards and returns the value of their total sum
- Total returned as an integer with the value of the sum of the user's cards*/
- string name1, name2, name3, name4;
- int value1, value2, value3, value4, cardamount;
- do
- {
- //Call deal amount
- DealAmount(cardamount);
- ClearScreen();
- //Deal number of cards requested and sums them
- if(cardamount == 1)
- {
- DrawCard(value1);
- value2 = 0;
- value3 = 0;
- value4 = 0;
- NameCard(value1, name1);
- CardSum(value1, value2, value3, value4, total);
- cout << "Your first card is a " << name1 << "." << endl;
- }
- else if(cardamount == 2)
- {
- DrawCard(value1);
- Sleep(1000);
- DrawCard(value2);
- value3 = 0;
- value4 = 0;
- NameCard(value1, name1);
- NameCard(value2, name2);
- CardSum(value1, value2, value3, value4, total);
- cout << "Your first card is a " << name1 << "." << endl;
- cout << "Your second card is a " << name2 << "." << endl;
- }
- else if(cardamount == 3)
- {
- DrawCard(value1);
- Sleep(1000);
- DrawCard(value2);
- Sleep(1000);
- DrawCard(value3);
- value4 = 0;
- NameCard(value1, name1);
- NameCard(value2, name2);
- NameCard(value3, name3);
- CardSum(value1, value2, value3, value4, total);
- cout << "Your first card is a " << name1 << "." << endl;
- cout << "Your second card is a " << name2 << "." << endl;
- cout << "Your third card is a " << name3 << "." << endl;
- }
- else if(cardamount == 4)
- {
- DrawCard(value1);
- Sleep(1000);
- DrawCard(value2);
- Sleep(1000);
- DrawCard(value3);
- Sleep(1000);
- DrawCard(value4);
- NameCard(value1, name1);
- NameCard(value2, name2);
- NameCard(value3, name3);
- NameCard(value4, name4);
- CardSum(value1, value2, value3, value4, total);
- cout << "Your first card is a " << name1 << "." << endl;
- cout << "Your second card is a " << name2 << "." << endl;
- cout << "Your third card is a " << name3 << "." << endl;
- cout << "Your fourth card is a " << name4 << "." << endl;
- }
- else
- {
- cout << "Error! Please choose a number of cards between 1 and 4." << endl << endl;
- }
- }while(cardamount < 1 || cardamount > 4);
- //Inform user of their card total value
- SetColor(9);
- cout << "Your card total is " << total << "." << endl;
- SetColor(7);
- }
- //==================================================================================================================
- void dealcomputer(int &total)
- {
- /*Generate three cards for the computer, total their value, and inform user of the cards and total
- Generates three cards, calculates their total, and informs the user of the cardes and their value*/
- int card1, card2, card3, card4;
- string name1, name2, name3;
- card4 = 0;
- DrawCard(card1);
- NameCard(card1, name1);
- Sleep(1000);
- DrawCard(card2);
- NameCard(card2, name2);
- Sleep(1000);
- DrawCard(card3);
- NameCard(card3, name3);
- CardSum(card1, card2, card3, card4, total);
- cout << endl << endl << "The computer's first card is a " << name1 << "." << endl;
- cout << "The computer's second card is a " << name2 << "." << endl;
- cout << "The computer's third card is a " << name3 << "." << endl;
- SetColor(14);
- cout << "The computer's total is " << total << "." << endl;
- SetColor(7);
- }
- //==================================================================================================================
- void FindWinner(int computer_in, int user_in, int &winner)
- {
- /*Determines winner of the round from computer_in and user_in
- computer_in > 0 and user_in > 0
- Winner returned as 1 if computer won, 2 if user won, or 0 if it was a draw*/
- if(computer_in > 21 && user_in > 21)
- {
- winner = 0;
- }
- else if(computer_in > user_in && computer_in <= 21)
- {
- winner = 1;
- }
- else if(user_in > computer_in && user_in <=21)
- {
- winner = 2;
- }
- else if(user_in < computer_in && computer_in > 21)
- {
- winner = 2;
- }
- else if(computer_in < user_in && user_in > 21)
- {
- winner = 1;
- }
- else if(user_in == computer_in)
- {
- winner = 0;
- }
- else
- {
- winner = 0;
- }
- }
- //==================================================================================================================
- void updatecount(int winner, int &computerwins, int &userwins)
- {
- /* Updates the count of how many wins each player has
- winner == 0 || winner == 1 || winner == 2
- Updated computer wins returned as compwins and updated user wins returned as userwins*/
- if (winner == 1)
- {
- computerwins += 1;
- }
- else if (winner == 2)
- {
- userwins += 1;
- }
- }
- //==================================================================================================================
- void ReportWinner(int winvalue)
- {
- /*Informs the user who won the round based on winvalue
- winvalue >= 0 && winvalue <= 2
- The winner of the round is reported*/
- if(winvalue == 1)
- {
- SetColor(4);
- cout << endl << "Sorry, the computer won this hand." << endl;
- SetColor(7);
- }
- else if (winvalue == 2)
- {
- SetColor(2);
- cout << endl << "Congratulations, you won this hand!" << endl;
- SetColor(7);
- }
- else
- {
- SetColor(5);
- cout << endl << "This hand resulted in a draw." << endl;
- SetColor(7);
- }
- }
- //==================================================================================================================
- void ReportResult(int compwins, int userwins)
- {
- cout << "The computer has won " << compwins << " times." << endl << endl;
- cout << "The user has won " << userwins << " times." << endl << endl;
- if(userwins > compwins)
- {
- cout << "Congratulations, you have won this playing session!" << endl;
- }
- else if(compwins > userwins )
- {
- cout << "Sorry, but the computer has won this playing session." << endl;
- }
- else
- {
- cout << "You and the computer have tied this playing session." << endl;
- }
- }
- //==================================================================================================================
- int Outro ()
- {
- int choice;
- cout << endl;
- cout << "Thank you for playing the game! Have a great day." << endl;
- return 0;
- }
- //==================================================================================================================
- int Intro()
- {
- cout << "Welcome to The Game of 21!" << endl;
- cout << "You will be asked to pick your amount of cards in a few seconds. You must pick a value between 1 and 4 cards." << endl;
- cout << "Now I am going to explain a few rules to you so you can understand the game a little better." << endl << endl;
- Sleep(10000);
- cout << "This is a game of cards, maybe you have heard of it maybe you have not." << endl;
- cout << "Each card is worth its respective number, and special cards, such as Kings, Queens, or Jacks, are worth 10." << endl;
- cout << "Ace's, however, are worth 11. The dealer will start with 3 cards." << endl;
- cout << "If both scores are over 21, or if both are equal but less than or equal to 21, the game is declared a draw." << endl;
- cout << "Otherwise the winner is the one with the highest score equal to or less than 21." << endl;
- cout << "If one score is over 21, but the other is 21 or less, the player with 21 or less is declared the winner." << endl;
- cout << "Now that you know the rules, lets play the game!" << endl << endl;
- Sleep(10000);
- return 0;
- }
- //==================================================================================================================
- void PlayGame(int &computerwins, int &userwins)
- /* Calls majority of functions to run the game adn keeps track of how many times computer wins and use wins
- Number of times the computer wins returned as computerwins and number of times user wins returned as user wins*/
- {
- int usertotal, computertotal, winner;
- dealuser(usertotal);
- dealcomputer(computertotal);
- FindWinner(computertotal, usertotal, winner);
- ReportWinner(winner);
- updatecount(winner, computerwins, userwins);
- }
- //==================================================================================================================
- int main()
- {
- int computerwins = 0, userwins = 0;
- char play;
- Intro();
- do
- {
- //Run the game
- PlayGame(computerwins, userwins);
- //Determine if game is played again
- PlayAgain(play);
- }while(play != 'n');
- ReportResult(computerwins, userwins);
- cout << endl;
- Outro();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement