//Deck Class
#include<iostream>
#include<stdlib.h>

using namespace std;


class Deck{
    int Cards[51];
public:
    Deck();
    void Display();
    void Shuffle();
};


Deck::Deck(){
    for(int n = 0; n < 52; n++){
        Cards[n]=n;
    }
}

void Deck::Display(){
    for(int n = 0; n < 52; n++){
        cout << n+1 << ". " << Cards[rand()%51] <<endl;
    }}

void Deck::Shuffle(){
    for(int n = 0;n < 52; n++){
        int r=n+(rand()%(52-n));
        int temp=Cards[n];
        Cards[n]=Cards[r];Cards[r]=temp;
    }}

This is what I have so far for card class, I want to categorize them into the suits then to the number they are. So like a two of clubs would be assigned to number 22 or something. How would I get it so it can detect what is a club and what isn't, same with numbers/face cards... I was told by a friend to use Enums but I wanted to know if there was any more efficient way

enum would work or you can use the / and % operators to get the value and suit.

22 is a bad choice for 2 of clubs in my view. 2 of clubs should be 1, 14, 27, or 40 depending on whether clubs has a value of 0, 1, 2, or 3.

Presumably 2 has a value of 1 since it is the second lowest card value (after ace) and we generally make the lowest index 0, which would work well if you are numbering the cards from 0 to 51.

string card[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                 "Jack", "Queen", "King"};
string suit[] = {"Spades", "Hearts", "Clubs", "Diamonds"};

In the above example, card[1] is "2" and suit[2] is "Clubs", so:

"2 of Clubs" could map to (13 * 2) + 1 = 27 . To get the indexes from 27, you could do this:

int suitIndex = 27 / 13; // = 2
int cardIndex = 27 % 13;  // = 1
cout << card[cardIndex] << " of " << suit[suitIndex]; // "2 of Clubs"

Hi,

If you are doing cards for any general card game, which makes use of suit, then you will benefit greatly from using a 64 bit card system. Then each bit refers to one card (with three left over).

That way in instances where suit doesn't matter it is easy to combine (e.g.

int rankedCards = (Cards.clubs | Cards.diamonds |
	      Cards.hearts | Cards.spades );

and you have set up the cards using a union.

struct SuitGrp
{
  unsigned int clubs : 13;
  unsigned int       : 3;
  unsigned int diamonds : 13;
  unsigned int       : 3;
  unsigned int hearts : 13;
  unsigned int       : 3;
  unsigned int spades : 13;
  unsigned int       : 3;
};

union BinCard
{
  uint64 Hand;    // uint64 is an unsigned 64 bit int (use a typedef)
  SuitGrp SG;
};

With this structure you can combine/sort/compare with much more ease than with a plan 52 count system.

Note the use of the : construct to define the length. This only works if the base type is longer than the number given. (in my case int is 2 bytes (16 bits) so that is ok.).

Hope this helps.

union BinCard{  
uint64 Hand;    // uint64 is an unsigned 64 bit int (use a typedef)  
SuitGrp SG;};

How about this solution portability? There are lots of compilers without 64-bit integer types ;)...

This article has been dead for over six months. Start a new discussion instead.