Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- using namespace std;
- void initializeDeck(int deck[]); //
- void dumpDeck(const int deck[]);
- void shuffle(int deck[]); //
- int getRandomNumber(int lowerBound, int upperBound); //
- void showCard(int card); //
- void showCards(const int cards[], int numCards); //
- int cardValue(int card); //
- int getTopCard(int deck[]); //
- void addToHand(int hand[], int cardToAdd); //
- int getHandValue(const int hand[]); //
- void playOneHand(int deck[]); //
- const int NUM_CARDS = 52;
- const int CARDS_IN_HAND = 10;
- void main()
- {
- int deck[NUM_CARDS];
- int choice;
- initializeDeck(deck);
- shuffle(deck);
- while((sizeof(deck)/sizeof(int)) >= 15)
- {
- cout << "Do you wanto play a round of 21 (1:yes 0:no)?" << endl;
- cin >> choice;
- if(choice)
- {
- playOneHand(deck);
- }
- }
- if((sizeof(deck)/sizeof(int)) < 15)
- {
- cout << "Sorry, but there are not enough cards left in the deck for another round!";
- }
- cin.ignore();
- cin.get();
- }
- void playOneHand(int deck[])
- {
- int playerHand[CARDS_IN_HAND] = {0,0,0,0,0,0,0,0,0,0};
- int dealerHand[CARDS_IN_HAND] = {0,0,0,0,0,0,0,0,0,0};
- bool pBust = false;
- bool dBust = false;
- bool error = false;
- int hand = 0;
- while(!pBust && !dBust && !error)
- {
- int choice;
- if(hand > 0)
- {
- cout << "Hand number: " << hand << endl;
- cout << "Your Current hand: ";
- showCards(playerHand, CARDS_IN_HAND);
- cout << endl;
- cout << "Dealers Current Hand: ";
- showCards(dealerHand, CARDS_IN_HAND);
- cout << endl;
- }
- cout << "Do you wanna draw (1:yes 0:no)?" << endl;
- cin >> choice;
- if(choice)
- {
- addToHand(playerHand, getTopCard(deck));
- addToHand(dealerHand, getTopCard(deck));
- if(getHandValue(playerHand) >= 21)
- pBust = true;
- if(getHandValue(dealerHand) >= 21)
- dBust = true;
- }else
- {
- error = true;
- }
- hand++;
- }
- if(pBust)
- cout << "You busted!" << endl;
- else if(dBust && !pBust)
- cout << "The Dealer Busted!" << endl;
- else if(!dBust && !pBust)
- {
- if(getHandValue(playerHand) > getHandValue(dealerHand))
- cout << "Your hand is more valuable, you win!" << endl;
- else if(getHandValue(playerHand) == getHandValue(dealerHand))
- cout << "The dealers hand is more valuable, you loose!" << endl;
- else
- cout << "It's a tie!" << endl;
- }
- }
- void initializeDeck(int deck[])
- {
- int index = 0;
- for(int suite = 100; suite <= 400; suite+=100)
- for(int card = 1; card <= 13; card++)
- deck[index++] = suite+card;
- }
- void dumpDeck(const int deck[])
- {
- for(int i = 0; i < NUM_CARDS; i++)
- cout << deck[i] << "\n";
- }
- void shuffle(int deck[])
- {
- for(int i = 0; i <= 500; i++)
- {
- int firstIndex = getRandomNumber(0,NUM_CARDS);
- int secondIndex = getRandomNumber(0,NUM_CARDS);
- int tempCard = deck[firstIndex];
- deck[firstIndex] = deck[secondIndex];
- deck[secondIndex] = tempCard;
- }
- }
- int getRandomNumber(int lowerBound, int upperBound)
- {
- static bool firstRun = true;
- int tempRand;
- if(firstRun) //only seed the random gen once
- {
- srand(time(NULL));
- firstRun = false;
- }
- if(upperBound > lowerBound)//swap bounds if needed
- tempRand = rand() % (upperBound - lowerBound) + lowerBound;
- else
- tempRand = rand() % (lowerBound - upperBound) + upperBound;
- return tempRand;
- }
- void showCard(int card)
- {
- int suite = card / 100;
- int cardType = card % 100;
- string symbols[] = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
- char outSuite = static_cast<char>(suite+2);
- cout << outSuite << symbols[cardType] << " ";
- }
- void showCards(const int cards[], int numCards)
- {
- for(int i = 0; i < numCards; i++)
- if(cards[i] > 0)
- showCard(cards[i]);
- }
- int cardValue(int card)
- {
- int cardType = card % 100;
- int value;
- switch(cardType)
- {
- case 1:
- value = 11;
- break;
- case 11:
- case 12:
- case 13:
- value = 10;
- break;
- default:
- value = cardType;
- break;
- }
- return value;
- }
- int getTopCard(int deck[])
- {
- int tempCard;
- for(int i = 0; i <= NUM_CARDS; i++)
- {
- if(deck[i] > 0)
- {
- tempCard = deck[i];
- deck[i] = 0;
- break;
- }
- if(i == NUM_CARDS)
- {
- tempCard = 0;
- }
- }
- return tempCard;
- }
- //might wanna check if busted or not.
- void addToHand(int hand[], int cardToAdd)
- {
- bool error = false;
- int index = 0;
- while(index < CARDS_IN_HAND && !error)
- {
- if(hand[index] == 0)
- {
- hand[index] = cardToAdd;
- error = true;
- }
- index++;
- }
- }
- int getHandValue(const int hand[])
- {
- int totalValue = 0;
- for(int i = 0; i < CARDS_IN_HAND; i++)
- totalValue += cardValue(hand[i]);
- return totalValue;
- }
Add Comment
Please, Sign In to add comment