#include #include #include using namespace std; ////////////////////////////////////////////////////////////////////////////// // Function Prototypes ////////////////////////////////////////////////////////////////////////////// // Function to get a random card value from 0 to 51 (that's 52 different values) int getRandomCard(); // Function to "extract" the suit (represented as 0, 1, 2, or 3) from a card // value. int getCardSuit(int card); // Function to "extract" the face (represeted as 1-13, 1 for Ace, 2-10 for // non-face cards, 11 for Jack, 12 for Queen, 13 for King) from a card value. int getCardFace(int card); // Function to return the blackjack value from a given card. // NOTE: This function assumes Aces ALWAYS have a value of 1, which is WRONG! // You will have to do something more advanced in your HW4 submission. int getBlackJackValue(int card); // Function to pretty-print a given card. void printCard(int card); ////////////////////////////////////////////////////////////////////////////// // Main Function ////////////////////////////////////////////////////////////////////////////// int main() { char response; int card; // Step 1: Seed random number generator and report seed int seed = (int) time(NULL); cout << "seed: " << seed << endl; srand(seed); // Step 2: Start drawing cards do { // Step 2.1 Draw the card card = getRandomCard(); // Step 2.2 Display what card was drawn cout << "You drew: "; printCard(card); cout << endl; // Step 2.3 Display the value of the card in blackjack cout << "\tBlackJack value: " << getBlackJackValue(card) << endl; // Step 2.4 Would the user like another card? cout << "Would you like to draw another random card? [y/n] "; cin >> response; } while (response == 'y' || response == 'Y'); // Step 3: Run various tests /* // run a few million tests to make sure i never generate an invalid face, // suit, or blackjack value cout << endl << "Running some tests..." << endl; for (int i = 0; i < 10000000; i++) { // draw the card card = getRandomCard(); // check the face int face = getCardFace(card); if (face < 1 || face > 13) cout << "face error! " << face << endl; // check the suit int suit = getCardSuit(card); if (suit < 0 || suit > 3) cout << "suit error!" << suit << endl; // check the blackjack value int value = getBlackJackValue(card); if (value < 1 || value > 10) cout << "value error!" << value << endl; } */ /* // do 10 million draws and report stats to make sure distribution is OK int deck[52] = {0}; int testSize = 1000000; for (int i = 0; i < testSize; i++) { deck[ getRandomCard() ]++; } cout << fixed << setprecision(3); for (int i = 0; i < 52; i++) { cout << setw(5) << i << ":" << setw(10) << deck[i] << setw(10) << (double)deck[i] / (double)testSize << "%" << endl; } */ return 0; } // Function to "draw" (return) a random card value. There are 52 cards, and // this function returns a random value from 0 to 51 (which is 52 different // values) int getRandomCard() { return rand() % 52; } // Function to extract and return a card suit (as a number) from a card value // as returned from the getRandomCard() function above. // Note that 0 = hearts, 1 = diamonds, 2 = clubs, 3 = spades int getCardSuit(int card) { return card / 13; } // Function to extract and return a card face (as a number) from a card value // as returned from the getRandomCard() function above. // Note that 1 = Ace, // 2-10 = that particular card // 11 = Jack // 12 = Queen // 13 = King int getCardFace(int card) { return (card % 13) + 1; } // Function to compute and return the value of a card in BlackJack. The card // is passed as a card-value (as returned from the getRandomCard() function // above). // !!! NOTE !!! This function assumes aces are always have a value of 1... // WHICH IS WRONG! // They can be either 1 or 11. In your HW 4 submissions, you must account // for this. int getBlackJackValue(int card) { int face = getCardFace(card); // face cards are always 10 if (face <= 10) return face; else return 10; } // Function to print (to "cout") a given card. The card is provided as a // card-value (as returned from the getRandomCard() function above). The // face and suit are extracted and printed seperately. void printCard(int card) { int face = getCardFace(card); int suit = getCardSuit(card); // first, print the card face if (face == 1) cout << "Ace"; else if (face <= 10) cout << face; else { switch (face) { case 11: cout << "Jack"; break; case 12: cout << "Queen"; break; case 13: cout << "King"; break; default: cout << "Invalid face value: " << face << endl; break; } } cout << " of "; // second, print the card suit switch(suit) { case 0: cout << "Hearts"; break; case 1: cout << "Diamonds"; break; case 2: cout << "Clubs"; break; case 3: cout << "Spades"; break; default: cout << "Invalid suit: " << suit << endl; break; } }