Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a Deck of Cards

Tags:

c++

I'm trying to make a simple blackjack program. Sadly, I'm having problems right off the bat with generating a deck of cards.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<char> deck;
    char suit[] = {'h','d','c','s'};
    char card[] = {'2','3','4','5','6','7','8','9','10','J','Q','K','A'};
    for (int j=0; j<13; j++) {
        for (int i=0; i<4; i++) {
            deck.push_back(card[j] suit[i]);
        }       
    }

    return 0;
}

I know my problem begins with me trying to assign the value '10' to a char. Obviously I couldn't get this to compile but I'm sure when I try to assign the card values to the vector deck I'll also get an error since I used variable type 'char'. Knowing what kind of variable type to use seems to be killing me. Also, would 'deck.push_back(card[j] suit[i]);' be the correct code to combine the card and suit, or do you have to put something between card[j] and suit[i]? I'd appreciate it if any of you could lead me in the right direction. Also as a little side note, this is part of a homework assignment so please don't just give me entire blocks of code. Thanks for your help.

like image 708
Ian Burris Avatar asked Oct 24 '08 17:10

Ian Burris


3 Answers

I think what you are looking to use is an enumeration. It will make your code clearer and resolve your problem.

enum SUIT { HEART, CLUB, DIAMOND, SPADE }; 
enum VALUE { ONE, TWO, THREE, ..., TEN, JACK, QUEEN, KING};
like image 144
Kevin Avatar answered Sep 19 '22 15:09

Kevin


Try to create class of Card with suit and card as a member and set it as a type of vector. Like

public class Card {
 public:
  Card(char suit, char card);
  char suit, card;
};

int main() {
    vector<Card> deck;
    char suit[] = {'h','d','c','s'};
    char card[] = {'2','3','4','5','6','7','8','9','T','J','Q','K','A'};
    for (int j=0; j<13; j++) {
        for (int i=0; i<4; i++) {
                deck.push_back(new Card(card[j],suit[i]));
        }               
    }
    return 0;
}

also using enums instead of chars in suit and card would make it clearer.

like image 25
JtR Avatar answered Sep 20 '22 15:09

JtR


The way you model it really depends on what you're trying to do.

Are you creating an actual game, and the data structures just need to support the gameplay?

If so, I'd create a card class, with an enum field for the suit and a numeric type (with values 1 - 13) for the face value.

On the other hand, if you're building an analysis application or an AI player, then the model might be a little bit different.

A few years ago, I wrote a simulator to calculate probabilities in various Texas Holdem scenarios, and I wanted it to crunch numbers REALLY quickly. I started out with a very straightforward model (card class, suit enum, etc) but after a lot of profiling and optimization, I ended up with a bitwise representation.

Each card was a sixteen-bit value, with the thirteen high-order bits representing the face value, the two low order bits representing the suit, and with bit[2] as a special flag indicating an ace (used only in cases where the ace might appear in an A2345 straight).

Here are a few examples:

0000000000001001  <----  Two of hearts
0100000000000011  <----  King of spades
1000000000000110  <----  Ace of diamonds

^^^^^^^^^^^^^            ("face-value" bits)
             ^           ("low-ace" flag)
              ^^         ("suit" bits)

You can imagine how, with a design like this, it's lighting fast to look for pairs, threes-of-a-kind, and straights (flushes are slightly more tricky).

I won't go into all the particular operations, but suffice it to say that this kind of model supports millions of operations per second...

Of course, keep in mind, I'm not actually advocating that you use a design like this in a straightforward game implementation. The only reason I ended up with this design is because I needed to conduct massive statistical simulations.

So think carefully about how you want to model:

  • Each card
  • A player's hand
  • The entire deck
  • The state of the table... including all player hands (including players who have split their initial hand), maybe a six-deck shoe, the discard pile, etc

The overall application model, and the goals of the application in general, will determine to a large extent the types of data structures that'll be most appropriate.

Have fun!!!

like image 33
benjismith Avatar answered Sep 18 '22 15:09

benjismith