/* CSCI261 HW07: Smart Blackjack * * Author: jpaone * Created on: 11 Feb 2016 * * This program plays Blackjack in a naive manner. Only cards * with values 1-10 are available and cards are randomly * generated. There is no enforcement that a card cannot be dealt * twice in a row to the Player or Dealer. */ #include // for srand(), rand() #include // for time() #include // for cout, cin, etc. #include // for string using namespace std; /* * The structure of our Card data type. * Contains the rank of the card as an int * and the suit of the card as a string. */ struct Card { int rank; // rank of card, expected acceptable values 1-10 string suit; // suit of card, expected values "Hearts", "Clubs", "Diamonds", "Spades" }; /* * Converts a number between 0 and 3 to a corresponding * playing card suit. If a value other than [0, 3] is * entered then the suit is set to Spades by default. * * Input: * int suitNam : number of the suit in the range of [0, 3] * * Output: * string suit : name of the suit */ string ConvertIntToSuit( int suitNum ); /* * Generates a card variable with random values. * The rank of the card will be between 1 and 10 (inclusive) * and the suit will be set to one of Hearts, Clubs, Diamonds, * or Spades. * * Input: * * Output: * Card card : with valid data member values */ Card MakeRandomCard(); /* * Prints a Card variable to the terminal in the * format of " of " * * Input: * Card card : the card to display * * Output: * */ void PrintCard( const Card card ); /* * Updates the total by adding the rank of the * card. * * Input: * int total : the total to update * Card card : the card to add to the total * * Output: * */ void UpdateTotal( int &total, const Card card ); /* * The main function of our program. This is the entry * point to our program and will be run first. * * Input: * * Output: * int exitCode : code returned when program exits */ int main() { srand( time(NULL) ); // seed our RNG with the current time bool keepPlaying = true; // keeps track if the user wants to continue playing while( keepPlaying ) { // as long as the user decides to keep playing // Step 0 // set up our variables to be used this hand string userChoice; // keeps track of the entered user's choice int userTotal = 0, dealerTotal = 0; // the card total for the user and dealer this hand // Step 1 // Deal a card to the Dealer Card recentCard = MakeRandomCard(); // generate a random card for the dealer cout << "Dealer shows the "; PrintCard( recentCard ); cout << endl; UpdateTotal( dealerTotal, recentCard ); // add the card's rank to the dealer's total cout << "Dealer total is: " << dealerTotal << endl; // Step 2 // Deal a card to the User recentCard = MakeRandomCard(); // generate a random card for the user cout << "You were dealt the "; PrintCard( recentCard ); cout << endl; UpdateTotal( userTotal, recentCard ); // add the card's rank to the user total do { // Step 3 // Deal another card to the user recentCard = MakeRandomCard(); // generate a random card for the user cout << "You were dealt the "; PrintCard( recentCard ); cout << endl; UpdateTotal( userTotal, recentCard ); // add the card's rank to the user total cout << "Your total is: " << userTotal << endl; if( userTotal > 21 ) { // if the user total is above 21 cout << "You busted! "; // tell the user they busted break; // break out of the loop } cout << "Do you want to \"Hit\", \"Stand\"?\n> "; // ask the user if they want another card cin >> userChoice; } while( userChoice.compare( "Stand" ) != 0 ); // only stop dealing cards to the user if they // type stand. Otherwise, deal them another card // Step 4 // Deal another card to the Dealer if user didn't bust if( userTotal <= 21 ) { // check that user did not bust while( dealerTotal < 17 ) { // while the Dealer has less than 17 points recentCard = MakeRandomCard(); // generate a random card to the Dealer cout << "Dealer was dealt the "; PrintCard( recentCard ); cout << endl; UpdateTotal( dealerTotal, recentCard ); // add the card's rank to the user total cout << "Dealer total is: " << dealerTotal << endl; } } // Step 5 // Print winner if( userTotal > 21 ) { // if user busted cout << "Dealer wins" << endl; // then Dealer wins } else if( dealerTotal > 21 ) { // otherwise if Dealer busted cout << "Dealer busted! You win" << endl; // then user wins } else if( dealerTotal > userTotal ) { // otherwise, nobody busted, Dealer's total is higher cout << "Dealer wins" << endl; // then Dealer wins } else if( userTotal > dealerTotal ) { // otherwise if user's total is higher cout << "You win" << endl; // then user wins } else { // otherwise, nobody's total is higher cout << "Push" << endl; // hand is a push } // Step 6 // Ask user if they want to play again cout << "Do you want to play again? \"Yes\" or \"No\"?\n> "; cin >> userChoice; if( userChoice.compare( "No" ) == 0 ) { // if user says No keepPlaying = false; // then turn on keep playing flag off } } return EXIT_SUCCESS; // report the program exited successfully } /* * Converts a number between 0 and 3 to a corresponding * playing card suit. If a value other than [0, 3] is * entered then the suit is set to Spades by default. */ string ConvertIntToSuit( int suitNum ) { switch( suitNum ) { case 0: return "Hearts"; case 1: return "Clubs"; case 2: return "Diamonds"; case 3: default: return "Spades"; } } /* * Generates a card variable with random values. * The rank of the card will be between 1 and 10 (inclusive) * and the suit will be set to one of Hearts, Clubs, Diamonds, * or Spades. */ Card MakeRandomCard() { Card newCard; newCard.rank = (rand() % 10) + 1; // set the rank equal to a random value between 1 and 10 newCard.suit = ConvertIntToSuit( rand() % 4 ); // set the suit equal to a random suit return newCard; // return the generated card } /* * Prints a Card variable to the terminal in the * format of " of " */ void PrintCard( const Card card ) { cout << card.rank << " of " << card.suit; } /* * Updates the total by adding the rank of the * card. */ void UpdateTotal( int &total, const Card card ) { total += card.rank; // add the rank of the card to the total }