The C++ Program: nowin2.cpp

  1 #include <iostream>
  2 #include <cstdlib>
  3 #include <string>
  4 #include <vector>
  5 #include <fstream>
  6 using namespace std;
  7 
  8 /* Arrays mapping characters to the card values they represent. */
  9 string keys = "23456789TJQKA";
 10 
 11 //              2  3  4  5  6  7  8  9  T   J   Q   K   A 
 12 int values[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};
 13 
 14 //Deal one card, to either the player or the dealer.
 15 void dealCard (int& hand, bool& hasEleven, int card) {
 16    if (card == 11) {
 17       //Dealing an ace
 18 
 19       //Already using an ace as 11, so the new ace must be 1 (since 11 + 11 = 22)
 20       if (hasEleven) {
 21          hand++;
 22       } else {
 23          //Otherwise count the ace as 11
 24          hand += 11;
 25          hasEleven = true;
 26       }
 27    } else {
 28       //Dealing any card other than an ace
 29       hand += card;
 30    }
 31         
 32    //If the hand is over 21 but we have an ace counted as 11, change the ace to 1.
 33    if (hand > 21 && hasEleven) {
 34       hand -= 10;
 35       hasEleven = false;
 36    }
 37 }
 38 
 39 int main() {
 40 
 41     while (true) {
 42        vector<int> deck;
 43        string str;
 44        cin >> str;
 45 
 46        size_t comp = str.compare("JOKER");
 47        if (comp==0) return 0;
 48        
 49        //Form the deck out of the input string
 50        for (int i = 0; i < str.length(); i++) {
 51            int index = keys.find(str[i]);
 52            if (index != string::npos) {
 53               deck.push_back(values[index]);
 54            }
 55        }
 56        
 57        //The value of the player/dealer's hand
 58        int playerHand = 0;
 59        int dealerHand = 0;
 60        
 61        //True if the player/dealer has an ace counted as 11
 62        bool playerEleven = false;
 63        bool dealerEleven = false;
 64        
 65        //Deal the first 4 cards.
 66        dealCard (playerHand, playerEleven, deck[0]);
 67        dealCard (dealerHand, dealerEleven, deck[1]);
 68        dealCard (playerHand, playerEleven, deck[2]);
 69        dealCard (dealerHand, dealerEleven, deck[3]);
 70               
 71        int oldDealerHand = dealerHand;
 72        bool oldDealerEleven = dealerEleven;
 73        
 74        bool canWin = false; //Is it possible for the player to win?
 75 
 76        //If the player starts out with 21, automatically wins.
 77        if (playerHand == 21) canWin = true;
 78           
 79        int dealerStart = 4; //the index in the deck where the dealer will start drawing
 80        
 81        //In this loop, try each possible number of cards the player could draw.
 82        while (!canWin && playerHand <= 21) {             
 83            //Dealer's turn is at beginning of loop, to allow for player drawing 0 cards
 84            int curCard = dealerStart;
 85            
 86            //Reset the dealer's hand to what it was after the first 4 cards.
 87            dealerHand = oldDealerHand;
 88            dealerEleven = oldDealerEleven;
 89            
 90            /*
 91              "The dealer must draw cards until reaching 17 or more points
 92              (with aces counting as 11 when possible)."
 93            */
 94            while (dealerHand < 17) {
 95               dealCard (dealerHand, dealerEleven, deck[curCard]);
 96               curCard++;
 97            }
 98            if (dealerHand <= playerHand || dealerHand > 21) canWin = true;
 99                          
100            //Try adding another card to the player's hand
101            dealCard (playerHand, playerEleven, deck[dealerStart]);
102            dealerStart++;
103        }
104        
105        cout << (canWin?"Yes":"No") << endl;
106     }
107     return 0;
108 }