Card representation again...

Programming, for all ages and all languages.
Post Reply
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Card representation again...

Post by Zacariaz »

ive tryed askin before, now i try again.

This seems to be a good start: (need only to hold 7 cards)

Code: Select all

enum {S, H, D, C, A =1, J  10, Q = 11, K = 12};
struct {unsigned Color:2; unsigned Value:4;} Cards[7];
But still it seems a better way would be some sort of wraping array to sort out the buisness with the ace, but how?

any comments?
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

well i'd represent a card as an object.

Code: Select all

class Card {
    enum Type { Diamond, Spades, Hearts, Clubs };
    enum Face { One=1, Two=2, Three=3 ... Jack = 10, Queen = 11, King = 12, Ace = 11; 
    Type    _type;
    Face    _face;

    int getValue(void) { return((int)_face); }
}

class Hand {
    Card[7]    _cards; //- better to use template btw instead of 7.
}

class Deck {
     Card[13*4]    _deck;
}

class Game {
    //- behavioural changes for each hand.
}
the behavour of the ace is different between games and should therefor be handled outside the card object. for instance if you want a total of your hand you could sum all the values of the cards and count the number of aces, then the possible totals are the sum and minus 10 for each chosen ace.
Author of COBOS
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

ok, looks reasonable.

thanks
Post Reply