| | |
How to catergorize cards???
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
//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
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
Check out this, I hope it'll help :-)
It's still work in progress though there are some changes made...
It's still work in progress though there are some changes made...
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
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.
In the above example,
"2 of Clubs" could map to
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.
C++ Syntax (Toggle Plain Text)
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: C++ Syntax (Toggle Plain Text)
int suitIndex = 27 / 13; // = 2 int cardIndex = 27 % 13; // = 1 cout << card[cardIndex] << " of " << suit[suitIndex]; // "2 of Clubs"
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
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.
and you have set up the cards using a union.
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.
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.
c++ Syntax (Toggle Plain Text)
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.
experience is the most expensive way to learn anything
c++ Syntax (Toggle Plain Text)
union BinCard{ uint64 Hand; // uint64 is an unsigned 64 bit int (use a typedef) SuitGrp SG;};
... ![]() |
Other Threads in the C++ Forum
- Previous Thread: door opener
- Next Thread: 32 bit color depth bitmap problem with XP
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






