How to catergorize cards???

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 51
Reputation: Manutebecker is an unknown quantity at this point 
Solved Threads: 2
Manutebecker's Avatar
Manutebecker Manutebecker is offline Offline
Junior Poster in Training

How to catergorize cards???

 
0
  #1
Dec 21st, 2008
//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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: How to catergorize cards???

 
0
  #2
Dec 21st, 2008
Check out this, I hope it'll help :-)

It's still work in progress though there are some changes made...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: How to catergorize cards???

 
0
  #3
Dec 21st, 2008
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.

  1. string card[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
  2. "Jack", "Queen", "King"};
  3. 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:

  1. int suitIndex = 27 / 13; // = 2
  2. int cardIndex = 27 % 13; // = 1
  3. cout << card[cardIndex] << " of " << suit[suitIndex]; // "2 of Clubs"
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: How to catergorize cards???

 
0
  #4
Dec 22nd, 2008
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.

  1. struct SuitGrp
  2. {
  3. unsigned int clubs : 13;
  4. unsigned int : 3;
  5. unsigned int diamonds : 13;
  6. unsigned int : 3;
  7. unsigned int hearts : 13;
  8. unsigned int : 3;
  9. unsigned int spades : 13;
  10. unsigned int : 3;
  11. };
  12.  
  13. union BinCard
  14. {
  15. uint64 Hand; // uint64 is an unsigned 64 bit int (use a typedef)
  16. SuitGrp SG;
  17. };

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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: How to catergorize cards???

 
0
  #5
Dec 22nd, 2008
  1. union BinCard{
  2. uint64 Hand; // uint64 is an unsigned 64 bit int (use a typedef)
  3. SuitGrp SG;};
How about this solution portability? There are lots of compilers without 64-bit integer types ...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC