How to catergorize cards???
//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
Manutebecker
Junior Poster in Training
51 posts since May 2008
Reputation Points: 10
Solved Threads: 3
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"
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
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 ;)...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348