Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int amountOfGames() {
- int gameNumber;
- cout << "How many games would you like to play? ";
- cin >> gameNumber;
- return gameNumber;
- }
- char returnCard() {
- char deck[] = { 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K' };
- int randomNum = rand() % 13;
- cout << randomNum << endl;
- return deck[randomNum];
- }
- bool dealerBust(int total) {
- if (total > 21) {
- return true;
- }
- else {
- return false;
- }
- }
- int cardValue(char cardChar) {
- switch (cardChar) {
- case 'A': return 1;
- break;
- case '2': return 2;
- break;
- case '3': return 3;
- break;
- case '4': return 4;
- break;
- case '5': return 5;
- break;
- case '6': return 6;
- break;
- case '7': return 7;
- break;
- case '8': return 8;
- break;
- case '9': return 9;
- break;
- case '10': return 10;
- break;
- case 'J': return 10;
- break;
- case 'Q': return 10;
- break;
- case 'K': return 10;
- break;
- default: return 0;
- break;
- }
- }
- int main() {
- int gameNumber = amountOfGames();
- int playerTotal, dealerTotal;
- int playedGames = 0;
- bool didDealerBust = false;
- bool dealerHasAce = false;
- bool playerHasAce = false;
- bool didPlayerBust = false;
- bool keepPulling = true;
- char pulledCard, userInput;
- srand(time(NULL));
- do {
- pulledCard = returnCard();
- if (pulledCard == 'A')
- dealerHasAce = true;
- cout << "Dealer's first card: " << pulledCard << endl;
- dealerTotal = cardValue(pulledCard);
- pulledCard = returnCard();
- if (pulledCard == 'A')
- playerHasAce = true;
- cout << "Your first card: " << pulledCard << endl;
- playerTotal = cardValue(pulledCard);
- pulledCard = returnCard();
- if (pulledCard == 'A')
- playerHasAce = true;
- cout << "Your second card: " << pulledCard << endl;
- playerTotal = cardValue(pulledCard) + playerTotal;
- cout << "Current dealer total: " << dealerTotal << endl;
- cout << "Current player total: " << playerTotal << endl;
- do {
- cout << "Would you like another card? (Y/N): ";
- cin >> userInput;
- if (userInput == 'y' || userInput == 'Y') {
- pulledCard = returnCard();
- if (pulledCard == 'A')
- playerHasAce = true;
- cout << "Your next card: " << pulledCard << endl;
- playerTotal = cardValue(pulledCard) + playerTotal;
- cout << "New player total: " << playerTotal << endl;
- if (playerTotal > 21) {
- keepPulling = false;
- didPlayerBust = true;
- cout << "You busted! Next game start." << endl;
- }
- }
- else if (userInput == 'n' || userInput == 'N') {
- keepPulling = false;
- if (playerHasAce && playerTotal <=11){
- playerTotal = playerTotal + 10;
- }
- cout << "User stays at " << playerTotal << endl;
- }
- } while (keepPulling == true);
- if (didPlayerBust == false) {
- do {
- if (pulledCard == 'A')
- dealerHasAce = true;
- cout << "Dealer's next card: " << pulledCard << endl;
- dealerTotal = cardValue(pulledCard) + dealerTotal;
- cout << "New dealer total: " << dealerTotal << endl;
- if (dealerHasAce && (dealerTotal > 6 && dealerTotal < 12)) {
- dealerTotal = dealerTotal + 10;
- cout << "New dealer total: " << dealerTotal << endl;
- }
- didDealerBust = dealerBust(dealerTotal);
- } while (dealerTotal < 17 || didDealerBust == false);
- }
- playedGames++;
- } while (playedGames < gameNumber);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement