[Kde-games-devel] enums for unified CardCache

Andreas Pakulat apaku at gmx.de
Wed Feb 6 22:06:52 CET 2008


On 06.02.08 00:22:45, Burkhard Lehner wrote:
> 2008/2/5, Andreas Pakulat <apaku at gmx.de>:
> >
> > Currently this is a QFlags thingie, i.e. an enum with values 2, 4, 8,
> > ...  However this is limiting us to 32 elements, and 18 are already
> > taken by a 52 card deck.
> 
> 
> I don't think that using QFlags here is a good idea, because it wastes so
> many bits for invalid combinations. E.g., it doesn't make sense to specify a
> card like "Spades | Diamonds | Queen", or "Hearts | Queen | Jack".
> The suit of a card can be specified in 2 bits (instead of 4 with QFlags),
> that prevents invalid combinations for free, and the value of a 52 cards
> deck with wildcards can be specified in 4 bits.
> One could use the old technique of just combining bits using "|", and use
> enums like
> 
> enum Suit {Diamonds = 0x0, Hearts = 0x1, Spades = 0x2, Clubs = 0x3};
> enum Value {Joker = 0x0, Ace = 0x4, Two = 0x8, Three = 0xC, Four = 0x10,
> Five = 0x14, Six = 0x18, Seven = 0x1C, Eight = 0x20, Nine = 0x24, Ten =
> 0x28, Jack = 0x2C, Queen = 0x30, King = 0x34};
> 
> Then you can specify a card like "Nine | Diamond" (or "Nine + Diamond"). The
> advantage is that the cards are sorted automatically (of course not
> addressing the problem that the ace is at the same time lower than 2 and
> higher than king).
> 
> If you want to prevent expressions like "Ace | Jack" or "Three | Diamonds |
> Spades", and at the same time get the type safety similar to that of QFlags,
> a class for creating a card could be used; something like

Generally I like this way, however there's one downside to any such
flags-based implementation: We need a table to convert all possible
combinations of flags into a string to be used for the svg id.

> And this is just the beginning. Thinking of other cards like, e.g., tarrot
> cards, one could think of additional enums and additional constructors of
> "Card". Or maybe a subclass of "Card" that provides support for other card
> types. You just have to keep the value of the internal "card" attribute
> unique. This reminds me of unicode: Every type of card deck has a specific
> range. The "traditional" card deck of 52 cards plus wildcards occupy the
> numbers "0" (Diamonds | Joker) to "55" (Clubs | King).

But thats a rather nice aspect, as it reduces the number of static data.
With this loading a whole theme becomes as easy as:

for(int i = 0; i < MAX_NUM; i++ )
  QString id = getId(i);
  if( !id.isEmpty() )
    renderFront(id);

> Maybe I haven't seen one or the other drawback or pitfall in the approach I
> described, but I leave the "work" to you. :-)

:) No problem, except the extra lookup table there's only one I see
myself: having to do all the "ground work" myself instead of using Qt
code for it.

Andreas

-- 
You are magnetic in your bearing.


More information about the kde-games-devel mailing list