943,522 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 470
  • C++ RSS
Dec 21st, 2008
0

How to catergorize cards???

Expand Post »
//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
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
Manutebecker is offline Offline
51 posts
since May 2008
Dec 21st, 2008
0

Re: How to catergorize cards???

Check out this, I hope it'll help :-)

It's still work in progress though there are some changes made...
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 21st, 2008
0

Re: How to catergorize cards???

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.

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  1. int suitIndex = 27 / 13; // = 2
  2. int cardIndex = 27 % 13; // = 1
  3. cout << card[cardIndex] << " of " << suit[suitIndex]; // "2 of Clubs"
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Dec 22nd, 2008
0

Re: How to catergorize cards???

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.

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Dec 22nd, 2008
0

Re: How to catergorize cards???

c++ Syntax (Toggle Plain Text)
  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 ...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: door opener
Next Thread in C++ Forum Timeline: 32 bit color depth bitmap problem with XP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC