Two days ago, I had a chat with friends who were playing online card and board games, talked about shuffling algorithms. I used to write some poker games and made a shuffling algorithm in the middle, I wrote an example to test them. The basic idea is very simple, that is, the exchange method. 54 cards are arranged and two cards are randomly selected for exchange. Generally, 54 cards are exchanged as long as the number of cards is sufficient. For example, 54 cards are exchanged for 54 times, the cards have already been badly washed. Oh, you can play cards. If you are not at ease, double, wash 108 times. The program is still relatively simple and should be clear to everyone. It took me about half an hour to write it and I tested it again. Code:
- # Define POKER_MAX 54 // 54 poker cards
- # Define POKER_COLOR_MAX 4 // four main colors
- # Define POKER_POINT_MAX 13 // 13 cards for each suit, J = 11, Q = 12, K = 13
- # Define POKER_COLOR_KING 4 // the color of the king
- # Define POKER_COLOR_0 0 // peach color
- # Define POKER_COLOR_1 1 // peach color
- # Define POKER_COLOR_2 2 // cherry blossom color
- # Define POKER_COLOR_3 3 // color of the square
- # Define POKER_KING_POINT_BIG 1 // Number of dawang points
- # Define POKER_KING_POINT_LITTLE 0 // number of sub-kings
- Typedef struct _ POKER_CARD _
- {
- Short m_sID; // ID (0 ~ 53)
- Char m_cColor; // poker color
- Char m_cPoint; // poker points
- } SCard;
- Const unsigned long SCardSize = sizeof (SCard );
- Class CPoker
- {
- Public:
- CPoker ()
- {
- // 54 cards are arranged in the order of black, red, and Sakura. Each color ranges from 0 ~ 12 Order
- Int I = 0;
- Int j = 0;
- Int nIndex = 0;
- For (I = 0; I <POKER_COLOR_MAX; I ++)
- {
- For (j = 0; j <POKER_POINT_MAX; j ++)
- {
- SetPokerInfo (I, j, nIndex );
- NIndex ++;
- }
- }
- // Put the king in the last two
- SetPokerInfo (POKER_COLOR_KING, POKER_KING_POINT_LITTLE, nIndex); // Mr. Wang
- NIndex ++;
- SetPokerInfo (POKER_COLOR_KING, POKER_KING_POINT_BIG, nIndex); // dawang
- }
- ~ CPoker (){}
- // Generally, the number of shuffling times is determined by the total number of cards, which is already very bad.
- Void Wash (int nTime = POKER_MAX)
- {
- Int I = 0;
- For (I = 0; I <nTime; I ++)
- Random ();
- }
- // The actual game, from which the card data is washed
- Bool Get (unsigned int nIndex, SCard * pCard)
- {
- If (POKER_MAX <= nIndex) return false;
- Memcpy (char *) pCard, (char *) & m_Poker [nIndex], SCardSize );
- Return true;
- }
- Void PrintInfo (void)
- {
- Int I = 0;
- SCard Card;
- For (I = 0; I <POKER_MAX; I ++)
- {
- If (Get (I, & Card ))
- {
- Printf ("% 02d-ID = % 02d, Color = % d, Point = % 02d \ n ",
- I, Card. m_sID, Card. m_cColor, Card. m_cPoint );
- }
- }
- Printf ("=======================\ n ");
- }
- Private:
- // Assign a value to the card ID specified by sID
- Void SetPokerInfo (char cColor, char cPoint, short sID)
- {
- M_Poker [sID]. m_cColor = cColor;
- M_Poker [sID]. m_cPoint = cPoint;
- M_Poker [sID]. m_sID = sID;
- }
- // Exchange two cards
- Void Exchange (int a, int B)
- {
- Char szTemp [SCardSize];
- Memcpy (szTemp, (char *) & m_Poker [a], SCardSize );
- Memcpy (char *) & m_Poker [a], (char *) & m_Poker [B], SCardSize );
- Memcpy (char *) & m_Poker [B], szTemp, SCardSize );
- }
- // Randomly select two cards for shuffling
- // If the random number is equal and a collision occurs, B = a + 1. In short, it will be different.
- Void Random (void)
- {
- Int a = GetRandomBetween (0, POKER_MAX );
- Int B = GetRandomBetween (0, POKER_MAX );
- If (a = B)
- {
- B = a + 1;
- If (POKER_MAX <= B) B = 0;
- }
- Exchange (a, B );
- }
- Private:
- SCard m_Poker [POKER_MAX];
- };
In fact, the main function is Wash. The default function is to Wash 54 times. Of course, if the caller is happy, it is okay to Wash it several times. Use the Get function to use the specified poker player. I tried it for a moment. It's okay. Oh, it's just a little exciting. From my testing code, it's another 0 bug. However, there is a problem with this code, that is, it is not a multi-thread security. At least, one thread is shuffled, and another thread gets a card, it will make an error. Therefore, according to the "resource lock" concept in "0bug-C/C ++ commercial engineering path", I encapsulated a locking thread security edition. Code:
- Class CPokerWithLock
- {
- Public:
- CPokerWithLock (){}
- ~ CPokerWithLock (){}
- Void Wash (int nTime = POKER_MAX)
- {
- M_Lock.Lock ();
- M_Poker.Wash (nTime );
- M_Lock.Unlock ();
- }
- Bool Get (unsigned int nIndex, SCard * pCard)
- {
- Bool bRet = false;
- M_Lock.Lock ();
- BRet = m_Poker.Get (nIndex, pCard );
- M_Lock.Unlock ();
- Return bRet;
- }
- Void PrintInfo (void)
- {
- M_Lock.Lock ();
- M_Poker.PrintInfo ();
- M_Lock.Unlock ();
- }
- Private:
- CPoker m_Poker; // "resource lock" concept, private aggregation, locking of all public methods to ensure security
- CMutexLock m_Lock; // This is the cross-platform security lock in 0bug-C/C ++ commercial engineering. See section 6.2, P226
- };
Well, this locking version is not mandatory and is only required in a multi-threaded environment. CMutexLock is a type that I am too lazy to provide here. If you have books, check the books by yourself. Well, finally, let's test the code. You can see the effect. Code:
- Inline void TestCPoker (void)
- {
- CPokerWithLock Poker;
- Srand (unsigned int) time (NULL ));
- Poker. PrintInfo ();
- Poker. Wash ();
- Poker. PrintInfo ();
- Poker. Wash ();
- Poker. PrintInfo ();
- }
The above code is successfully tested under VS2008. If you are interested, try it on your own. Well, CPokerWithLock Poker, a friend who has no books, can be changed to CPoker Poker. Simply use a non-thread-safe version. Let's see if you have any questions. ========================================================== ====================
Buy 0bug-C/C ++ commercial engineering at the reserve price online
Directly click the link below or copy it to the address bar of the browser)
Http://s.click.taobao.com/t_3? Limit % 3 Fpid % 3Dmm_13866629_0_0 Xiao Miao
This article from the "Xiao blog" blog, please be sure to keep this source http://tonyxiaohome.blog.51cto.com/925273/302220