Need help with black jack

This is a discussion on Need help with black jack within the C++ Programming forums, part of the General Programming Boards category; Hello, so I've been working on a little project of mine, i'm trying to imitate a black jack game without ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    70

    Need help with black jack

    Hello, so I've been working on a little project of mine, i'm trying to imitate a black jack game without the fancy rules though, just the basics. Here's the code:

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <cstdlib>
    #include <ctime>
    
    #define NUM_CARDS 52
    using namespace std;
    typedef vector<int> CardVec;
    
    void InitDeck(CardVec& deck);
    void ShuffleDeck(CardVec& deck);
    int SumCardValues(const CardVec& cards);
    void PrintCards(const CardVec& cards, bool dealer);
    void PlayGame();
    
    const string CardNames[52] = // Creating array for name for cards
          {
             "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "HJ", "HQ", "HK", "HA",
             "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "DJ", "DQ", "DK", "DA",
             "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "SJ", "SQ", "SK", "SA",
             "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "CJ", "CQ", "CK", "CA"
          };
    
          const int CardValues[52] =
          {
              2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
              2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
              2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
              2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11
          };
    int main()
    {
          srand((unsigned)time(0));
          bool quit = false;
    
          while(!quit)
          {
              string choice = "";
    
              cout <<  "1) Play Game 2) Quit" << endl;
              cin >> choice;
    
              if(choice == "1")
              {
                  PlayGame();
              }
    
              else if(choice == "2")
              {
                  quit = true;
              }
              else
              {
                  cout << "Invalid Input" << endl;
              }
          }
    }
    
    void InitDeck(CardVec& deck)
    {
        for(int i=0;i<52;++i)
        {
            deck[i] = i;
        }
    }
    
    void ShuffleDeck(CardVec& deck)
    {
        int temp = 0;
        int swapnum = 0;
    
        for(int i=0;i<52;++i)
        {
            swapnum = rand() % 52;
            temp = deck[i];
            deck[i] = deck[swapnum];
            deck[swapnum] = temp;
        }
    }
    
    int SumCardValues(const CardVec& cards)
    {
        int sum = 0;
        for(int i=0;i<2;++i)
        {
            sum += CardValues[cards[i]];
        }
    
        return sum;
    }
    
    void PrintCards(const CardVec& cards, bool dealer)
    {
          int cardsize = 0;
          cardsize = cards.size();
    
          if(dealer == true)
          {
              cout << "Dealers has: ";
              for(int i=0;i<cardsize;++i)
              {
                  cout << CardNames[cards[i]] << ", ";
              }
    
          }
    
          else if(dealer == false)
          {
              cout << "You have: ";
              for(int i=0;i<2;++i)
              {
                  cout << CardNames[cards[i]] << ", ";
              }
          }
    }
    
    void PlayGame()
    {
        //Create, initialize, shuffle deck
        CardVec deck(52);
        InitDeck(deck);
        ShuffleDeck(deck);
    
        //Create hands for dealer and player as vectors
        CardVec PlayerHand;
        CardVec DealerHand;
    
        //Draw cards
    
        //////Player Hand/////////
        PlayerHand.push_back(deck.back());
        deck.pop_back();
    
        PlayerHand.push_back(deck.back());
        deck.pop_back();
    
        /////DealerHand///////////
        DealerHand.push_back(deck.back());
        deck.pop_back();
    
        DealerHand.push_back(deck.back());
        deck.pop_back();
    
        PrintCards(PlayerHand,false); // Display players hand
        cout << endl;
    
        PrintCards(DealerHand,true); // Display dealers hand
        cout << endl;
        while(1) // Players turn
        {
            int choice = 0;
            int sumvalue = 0;
    
            cout << "1) Hit 2) Stand" << endl;
            cin >> choice;
    
            if(choice == 1)
            {
                PlayerHand.push_back(deck.back());
                sumvalue = SumCardValues(PlayerHand);
                if(sumvalue > 21)
                {
                    cout << "You busted!" << endl;
                    break;
                }
                else if(sumvalue< 21)
                {
                    continue;
                }
            }
            else
            {
                cout << "You stand." << endl;
            }
        }
    
        while(1) // Dealers turn
        {
            if(SumCardValues(DealerHand) < 18)
            {
                cout << "Dealer hits, ";
                PrintCards(DealerHand,true);
            }
            else
            {
                cout << "Dealer stands." << endl;
            }
        }
    
    }
    The problem is that when i run it, it gets to "1) Hit 2) Stand" and i press 1 but, then it keeps repeating itself, it'll keep saying "1) Hit 2) Stand" even though i'm pressing 1

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    That's what it does (sometimes) in the code. What do you want it to do?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help -- Red Black Tree
    By Fatima Rizwan in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2011, 02:38 AM
  2. can u fix this black jack code
    By onlinegeek in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2006, 09:15 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. I need some ideas please>>
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-23-2002, 02:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21