Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TestStuff.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <stdlib.h> /* srand, rand */
- #include <time.h> /* time */
- #include <string>
- // Blackjack
- // Suits
- // HEART = 0,
- // DIAMOND = 1,
- // SPADE = 2,
- // CLUB = 3
- struct Card
- {
- int value = 0;
- int suit;
- };
- void printCard(Card * card)
- {
- switch (card->value)
- {
- case 1:
- std::cout << "Ace";
- break;
- case 11:
- std::cout << "Jack";
- break;
- case 12:
- std::cout << "Queen";
- break;
- case 13:
- std::cout << "King";
- break;
- default:
- std::cout << card->value;
- }
- std::cout << " of ";
- switch (card->suit)
- {
- case 0:
- std::cout << "Hearts\n";
- break;
- case 1:
- std::cout << "Diamonds\n";
- break;
- case 2:
- std::cout << "Spades\n";
- break;
- case 3:
- std::cout << "Clubs\n";
- break;
- default:
- std::cout << "Magic\n";
- break;
- }
- }
- // A queue type structure
- struct Deck
- {
- Card cards[52];
- };
- void fillDeck(Deck * deck)
- {
- for (int i = 0; i < 4; i++)
- {
- for (int j = 1; j <= 13; j++)
- {
- Card card;
- card.value = j;
- card.suit = i;
- deck->cards[(i * 13) + j - 1] = card;
- }
- }
- }
- void printDeck(Deck * deck)
- {
- for (int i = 0; i < 4; i++)
- {
- for (int j = 1; j <= 13; j++)
- {
- std::cout << deck->cards[(i * 13) + j - 1].value << " ";
- }
- std::cout << std::endl;
- }
- }
- void shuffleDeck(Deck * deck)
- {
- Card temp;
- for (int i = 0; i < 52; i++)
- {
- int cardA = rand() % 52;
- int cardB = rand() % 52;
- if (cardA == cardB)
- continue;
- else
- {
- temp = deck->cards[cardA];
- deck->cards[cardA] = deck->cards[cardB];
- deck->cards[cardB] = temp;
- }
- }
- }
- void printCard(Deck * deck, int cardNum)
- {
- if (cardNum < 52)
- printCard(&(deck->cards[cardNum]));
- else
- std::cout << "Bad cardNum gave, error.\n";
- }
- Card dealCard(Deck * deck, int * deckLoc)
- {
- (*deckLoc)++;
- return deck->cards[((*deckLoc) - 1) % 52];
- }
- struct Hand
- {
- Card * cards = nullptr;
- int size = 0;
- };
- void addCard(Hand * hand, Card * card)
- {
- Card * temp = new Card[hand->size + 1];
- for (int i = 0; i < hand->size; i++)
- {
- temp[i] = hand->cards[i];
- }
- // Add last card then delete and point to new temp array
- temp[hand->size] = *card;
- delete[] hand->cards;
- hand->cards = temp;
- ++(hand->size);
- }
- void printHand(Hand * hand)
- {
- for (int i = 0; i < hand->size; i++)
- {
- printCard(&(hand->cards[i]));
- }
- }
- void printHandDebug(Hand * hand)
- {
- for (int i = 0; i < hand->size; i++)
- {
- std::cout << hand->cards[i].value << " ";
- }
- }
- void clearHand(Hand * hand)
- {
- delete[] hand->cards;
- hand->cards = nullptr;
- hand->size = 0;
- }
- /*
- Calculates the highest hand value you can have under 21 if possible,
- if you have multiple aces it will get the highest value you can without
- exceeding 21.
- */
- int handValue(Hand * hand)
- {
- int val = 0;
- int numAces = 0;
- int cardWorth = 0;
- for (int i = 0; i < hand->size; i++)
- {
- cardWorth = hand->cards[i].value;
- if (cardWorth == 11 || cardWorth == 12 || cardWorth == 13)
- val += 10;
- else if (cardWorth == 1)
- {
- ++numAces;
- }
- else
- val += cardWorth;
- }
- if (numAces > 0)
- {
- if ((cardWorth == 20 || cardWorth == 10) && numAces == 1)
- return 21;
- else
- {
- // Catch if you have 10 and one ace
- if (val + (numAces * 11) == 21)
- return 21;
- // Catch if aces == 1
- else if (val + numAces == 21)
- return 21;
- // Catch if you have multiple aces and exceed 21
- else if (val + numAces > 21)
- return val + numAces;
- // Do some calculating to see if converting one ace to 11 helps
- else if (val + numAces <= 21)
- {
- if ((val + 11) + (numAces - 1) <= 21)
- return (val + 11) + (numAces - 1);
- else
- return val + numAces;
- }
- // You are already over 21 sorry
- else
- {
- return val + numAces;
- }
- }
- }
- return val;
- }
- void dealHands(Hand * player, Hand * dealer, Deck * deck, int * deckLoc)
- {
- std::cout << "\n\nDealing Cards\n\n";
- addCard(dealer, &(dealCard(deck, deckLoc)));
- addCard(player, &(dealCard(deck, deckLoc)));
- addCard(dealer, &(dealCard(deck, deckLoc)));
- addCard(player, &(dealCard(deck, deckLoc)));
- std::cout << "You have:\n";;
- printHand(player);
- std::cout << "\n\nThe dealer is showing a(n) ";
- printCard(&(dealer->cards[1]));
- }
- void playRound(Deck * deck, int numPlayers, int deckLoc)
- {
- printDeck(deck);
- Hand dealerHand;
- Hand playerHand;
- bool playing = true;
- bool end = false;
- std::string action;
- dealHands(&playerHand, &dealerHand, deck, &deckLoc);
- while (playing)
- {
- /*if (handValue(dealerHand.cards, dealerHand.size) == 21)
- {
- std::cout << "Sorry dealer has 21, you lose.\n\nPlease play again.";
- done = true;
- playing = false;
- break;
- }*/
- if (handValue(&playerHand) == 21 && end == false)
- {
- std::cout << "\n\nYou have 21 nothing to do but see what falls.\n\n";
- }
- std::cout << "\n\nWhat would you like to do?\n";
- std::cin >> action;
- if (end == false)
- {
- if (action == "hit")
- {
- addCard(&playerHand, &(dealCard(deck, &deckLoc)));
- std::cout << "\nYou now have:\n";
- printHand(&playerHand);
- std::cout << "Hand Value: " << handValue(&playerHand) << std::endl;
- if (handValue(&playerHand) > 21)
- {
- std::cout << "\nOuch, looks like you busted! Please try again!";
- }
- }
- else if (action == "stay")
- {
- end = true;
- std::cout << "\nDealer is showing: \n";
- printHand(&dealerHand);
- while (handValue(&dealerHand) < handValue(&playerHand) && handValue(&dealerHand) <= 21)
- {
- std::cout << "\nDealer is drawing a card\n";
- addCard(&dealerHand, &(dealCard(deck, &deckLoc)));
- std::cout << "Dealer is showing: \n";
- printHand(&dealerHand);
- }
- if (handValue(&dealerHand) == 21)
- {
- std::cout << "\nDealer has Blackjack! Sorry! Please play again.";
- }
- else if (handValue(&dealerHand) == handValue(&playerHand))
- {
- std::cout << "\nRats! The Dealer tied meaning he won! Please play again";
- }
- else if (handValue(&dealerHand) > 21)
- {
- std::cout << "\nDealer busts! You won! Wanna try to best us again?";
- }
- else if (handValue(&dealerHand) > handValue(&playerHand) && handValue(&dealerHand) < 21)
- {
- std::cout << "\nSorry dealer has the higher hand, please play again.";
- }
- else
- {
- std::cout << "\n*vuvuzela sounds* Congratulations! You won! *vuvuzela sounds* Wanna try to best us again?";
- }
- }
- }
- if (action == "play")
- {
- std::cout << "\n\n\n---New Round---\n\n";
- clearHand(&playerHand);
- clearHand(&dealerHand);
- dealHands(&playerHand, &dealerHand, deck, &deckLoc);
- end = false;
- }
- else if (action == "reshuffle")
- {
- shuffleDeck(deck);
- std::cout << "\n\n\n---New Round---\n\n";
- clearHand(&playerHand);
- clearHand(&dealerHand);
- dealHands(&playerHand, &dealerHand, deck, &deckLoc);
- }
- else if (action == "quit")
- {
- playing = false;
- }
- else if(action == "help")
- {
- std::cout << "\n\n\nValid Commands are:\nhelp play reshuffle quit stay hit\n\n";
- }
- }
- delete[] playerHand.cards;
- delete[] dealerHand.cards;
- }
- int main()
- {
- srand(time(NULL));
- Deck newDeck;
- fillDeck(&newDeck);
- //printDeck(&newDeck);
- std::cout << "\nShuffling Deck\n";
- shuffleDeck(&newDeck);
- printDeck(&newDeck);
- std::cout << "\n\n\nStarting Game\n\n";
- playRound(&newDeck, 1, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement