/* Programmer: Dylan McDonald Class & Section: CS 54, Section Date: 25 September, 2007 Purpose: Programatically play Blackjack using structs */ #include using namespace std; struct card { int rank; /* Let Ace = 1, Jack = 11, Queen = 12, and King = 13 */ char suit; /* Let spades = 's', clubs = 'c', hearts = 'h', and diamonds = 'd' */ }; struct blackjack { struct card first; /* first up card */ struct card second; /* second up card */ }; int main() { int randRank, score = 0; char randSuit; bool hasAce = false; unsigned short whichCard; struct blackjack deal; struct card hitCard; /* seed the random numbers */ srand(time(NULL)); /* this for loop will do the heavy lifting in randomly choosing the rank & suit as well as scoring the hand without incessantly copying & pasting code */ for (whichCard = 0; whichCard < 3 && score != -1; whichCard++) { /* randomly decide the rank & suit */ randRank = rand() % 13 + 1; randSuit = rand() % 4; /* do some bookkeeping with the score of the first two cards */ if (whichCard < 2) { switch (randRank) { case 1: /* ace */ hasAce = true; score += 11; break; case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: /* numbered cards */ score += randRank; break; case 11: case 12: case 13: /* face cards */ score += 10; break; default: /* invalid cards */ score = -1; break; } } /* map the random suit range to an actual suit */ switch (randSuit) { case 0: randSuit = 's'; break; case 1: randSuit = 'c'; break; case 2: randSuit = 'h'; break; case 3: randSuit = 'd'; break; default: randSuit = 'X'; break; } /* change the first card */ if (whichCard == 0) { deal.first.rank = randRank; deal.first.suit = randSuit; } /* change the second card */ else if (whichCard == 1) { deal.second.rank = randRank; deal.second.suit = randSuit; } /* change the hit card */ else if (whichCard == 2) { hitCard.rank = randRank; hitCard.suit = randSuit; } } cout << "You have a " << deal.first.rank << deal.first.suit << " " << deal.second.rank << deal.second.suit << endl; /* they had a pair of aces */ if (score == 22) score = 12; cout << "You scored a " << score << endl; /* display hit card & finish calculating the score */ cout << "Your hit card is a " << hitCard.rank << hitCard.suit << endl; switch (hitCard.rank) { case 1: /* ace */ score += 11; break; case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: /* numbered cards */ score += randRank; break; case 11: case 12: case 13: /* face cards */ score += 10; break; default: /* invalid cards */ score = -1; break; } /* reward hands */ if (deal.first.rank == 1 && deal.first.suit != 'd') { /* Ace King */ if (deal.first.suit == 'd' && deal.second.rank == 13 && deal.second.suit == 'd') cout << "Congratulations! You have a free meal at Clayton's cafteria!" << endl; /* Ace Queen */ if (deal.first.suit == 'c' && deal.second.rank == 12 && deal.second.suit == 'd') cout << "The evil Ace Queen has struck again! Your luck has ran out. " << "Goodbye!" << endl; /* Ace Jack */ if (deal.first.suit == 's' && deal.second.rank == 11 && deal.second.suit == 's') cout << "The one and only \"Blackjack\"; give this guy more money!" << endl; } /* if they hit a soft ace and went broke, we can deduct 10 OR if they hit and went broke, but received an ace, we can deduct 10 */ if ((score > 21 && hasAce) || (score > 21 && !hasAce && hitCard.rank == 1)) score -= 10; cout << "After hitting, You scored a " << score << endl; /* maybe they shouldn't have hit? */ if (score > 21) cout << "So who here didn't take a hit when they realised their Blackjack " << "score exceeded their IQ? You are the weakest link. Goodbye." << endl; return 0; }