[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdegames/kpat
From:       Parker Coates <parker.coates () kdemail ! net>
Date:       2012-02-15 4:44:54
Message-ID: 20120215044454.DB318AC896 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1280163 by coates:

Make KCardDeck card IDs unique, even for cards with same suit and rank.

Previously, two KCards having the same suit and rank would have
identical IDs, leaving no real way (other than their addresses) to tell
them apart at run time. This makes implementing some changes I have
planned a real hassle.

Since KCardDeck was only using the lowest 6 bits of the 32 available in
the ID, we have plenty of room to store the card's position within the
deck. The new layout is 8 bits for the rank, 8 for the suit, and the
remaining 16 for the number. So that's support for 256 different ranks,
256 different suits and 65536 different cards.

This change is also made to KPat's slightly custom ID generation code.

 M  +4 -3      dealer.cpp  
 M  +9 -8      libkcardgame/kcarddeck.cpp  
 M  +1 -1      libkcardgame/kcarddeck.h  


--- trunk/KDE/kdegames/kpat/dealer.cpp #1280162:1280163
@@ -290,7 +290,7 @@
 
     QMultiHash<quint32,KCard*> cards;
     foreach ( KCard * c, deck()->cards() )
-        cards.insert( c->id(), c );
+        cards.insert( (c->id() & 0xffff), c );
 
     foreach (PatPile *p, patPiles())
     {
@@ -307,7 +307,7 @@
                     int s = card.attribute("suit").toInt();
                     int r = card.attribute("value").toInt();
                     
-                    KCard * c = cards.take( ( s << 4 ) + r );
+                    KCard * c = cards.take( ( s << 8 ) + r );
                     if (!c)
                         continue;
 
@@ -1601,10 +1601,11 @@
     // generates card by rank and then by suit, rather than the more common
     // suit then rank ordering.
     QList<quint32> ids;
+    unsigned int number = 0;
     for ( int i = 0; i < copies; ++i )
         foreach ( const KCardDeck::Rank & r, KCardDeck::standardRanks() )
             foreach ( const KCardDeck::Suit & s, suits )
-                ids << KCardDeck::getId( s, r );
+                ids << KCardDeck::getId( s, r, number++ );
 
     deck()->setDeckContents( ids );
 }
--- trunk/KDE/kdegames/kpat/libkcardgame/kcarddeck.cpp #1280162:1280163
@@ -53,9 +53,9 @@
 }
 
 
-quint32 KCardDeck::getId( Suit suit, Rank rank )
+quint32 KCardDeck::getId( KCardDeck::Suit suit, KCardDeck::Rank rank, int number )
 {
-    return ((suit & 0x3) << 4) | (rank & 0xf);
+    return (number << 16) | ((suit & 0xff) << 8) | (rank & 0xff);
 }
 
 
@@ -71,10 +71,11 @@
     // avoided if at all possible. Doing so may effect the game logic of
     // games relying on LibKCardGame.
     QList<quint32> ids;
-    for ( int i = 0; i < copies; ++i )
+    unsigned int number = 0;
+    for ( int c = 0; c < copies; ++c )
         foreach ( const Suit & s, suits )
             foreach ( const Rank & r, ranks )
-                ids << getId( s, r );
+                ids << getId( s, r, number++ );
 
     return ids;
 }
@@ -94,7 +95,7 @@
 
 int KCardDeck::rankFromId( quint32 id ) const
 {
-    int rank = id & 0xf;
+    int rank = id & 0xff;
     Q_ASSERT( Ace <= rank && rank <= King );
     return rank;
 }
@@ -102,7 +103,7 @@
 
 int KCardDeck::suitFromId( quint32 id ) const
 {
-    int suit = (id >> 4) & 0x3;
+    int suit = (id >> 8) & 0xff;
     Q_ASSERT( Clubs <= suit && suit <= Spades );
     return suit;
 }
@@ -122,7 +123,7 @@
 
     QString element;
 
-    int rank = id & 0xf;
+    int rank = rankFromId( id );
     switch( rank )
     {
     case King:
@@ -139,7 +140,7 @@
         break;
     }
 
-    switch( id >> 4 )
+    switch( suitFromId( id ) )
     {
     case Clubs:
         element += "_club";
--- trunk/KDE/kdegames/kpat/libkcardgame/kcarddeck.h #1280162:1280163
@@ -59,7 +59,7 @@
 
     static QList<Suit> standardSuits();
     static QList<Rank> standardRanks();
-    static quint32 getId( Suit suit, Rank rank );
+    static quint32 getId( Suit suit, Rank rank, int number );
     static QList<quint32> generateIdList( int copies = 1,
                                           const QList<Suit> & suits = standardSuits(),
                                           const QList<Rank> & ranks = standardRanks() );
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic