A shuffling algorithm I used before

Source: Internet
Author: User

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:

  1. # Define POKER_MAX 54 // 54 poker cards
  2. # Define POKER_COLOR_MAX 4 // four main colors
  3. # Define POKER_POINT_MAX 13 // 13 cards for each suit, J = 11, Q = 12, K = 13
  4. # Define POKER_COLOR_KING 4 // the color of the king
  5. # Define POKER_COLOR_0 0 // peach color
  6. # Define POKER_COLOR_1 1 // peach color
  7. # Define POKER_COLOR_2 2 // cherry blossom color
  8. # Define POKER_COLOR_3 3 // color of the square
  9. # Define POKER_KING_POINT_BIG 1 // Number of dawang points
  10. # Define POKER_KING_POINT_LITTLE 0 // number of sub-kings
  11. Typedef struct _ POKER_CARD _
  12. {
  13. Short m_sID; // ID (0 ~ 53)
  14. Char m_cColor; // poker color
  15. Char m_cPoint; // poker points
  16. } SCard;
  17. Const unsigned long SCardSize = sizeof (SCard );
  18. Class CPoker
  19. {
  20. Public:
  21. CPoker ()
  22. {
  23. // 54 cards are arranged in the order of black, red, and Sakura. Each color ranges from 0 ~ 12 Order
  24. Int I = 0;
  25. Int j = 0;
  26. Int nIndex = 0;
  27. For (I = 0; I <POKER_COLOR_MAX; I ++)
  28. {
  29. For (j = 0; j <POKER_POINT_MAX; j ++)
  30. {
  31. SetPokerInfo (I, j, nIndex );
  32. NIndex ++;
  33. }
  34. }
  35. // Put the king in the last two
  36. SetPokerInfo (POKER_COLOR_KING, POKER_KING_POINT_LITTLE, nIndex); // Mr. Wang
  37. NIndex ++;
  38. SetPokerInfo (POKER_COLOR_KING, POKER_KING_POINT_BIG, nIndex); // dawang
  39. }
  40. ~ CPoker (){}
  41. // Generally, the number of shuffling times is determined by the total number of cards, which is already very bad.
  42. Void Wash (int nTime = POKER_MAX)
  43. {
  44. Int I = 0;
  45. For (I = 0; I <nTime; I ++)
  46. Random ();
  47. }
  48. // The actual game, from which the card data is washed
  49. Bool Get (unsigned int nIndex, SCard * pCard)
  50. {
  51. If (POKER_MAX <= nIndex) return false;
  52. Memcpy (char *) pCard, (char *) & m_Poker [nIndex], SCardSize );
  53. Return true;
  54. }
  55. Void PrintInfo (void)
  56. {
  57. Int I = 0;
  58. SCard Card;
  59. For (I = 0; I <POKER_MAX; I ++)
  60. {
  61. If (Get (I, & Card ))
  62. {
  63. Printf ("% 02d-ID = % 02d, Color = % d, Point = % 02d \ n ",
  64. I, Card. m_sID, Card. m_cColor, Card. m_cPoint );
  65. }
  66. }
  67. Printf ("=======================\ n ");
  68. }
  69. Private:
  70. // Assign a value to the card ID specified by sID
  71. Void SetPokerInfo (char cColor, char cPoint, short sID)
  72. {
  73. M_Poker [sID]. m_cColor = cColor;
  74. M_Poker [sID]. m_cPoint = cPoint;
  75. M_Poker [sID]. m_sID = sID;
  76. }
  77. // Exchange two cards
  78. Void Exchange (int a, int B)
  79. {
  80. Char szTemp [SCardSize];
  81. Memcpy (szTemp, (char *) & m_Poker [a], SCardSize );
  82. Memcpy (char *) & m_Poker [a], (char *) & m_Poker [B], SCardSize );
  83. Memcpy (char *) & m_Poker [B], szTemp, SCardSize );
  84. }
  85. // Randomly select two cards for shuffling
  86. // If the random number is equal and a collision occurs, B = a + 1. In short, it will be different.
  87. Void Random (void)
  88. {
  89. Int a = GetRandomBetween (0, POKER_MAX );
  90. Int B = GetRandomBetween (0, POKER_MAX );
  91. If (a = B)
  92. {
  93. B = a + 1;
  94. If (POKER_MAX <= B) B = 0;
  95. }
  96. Exchange (a, B );
  97. }
  98. Private:
  99. SCard m_Poker [POKER_MAX];
  100. };
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:
  1. Class CPokerWithLock
  2. {
  3. Public:
  4. CPokerWithLock (){}
  5. ~ CPokerWithLock (){}
  6. Void Wash (int nTime = POKER_MAX)
  7. {
  8. M_Lock.Lock ();
  9. M_Poker.Wash (nTime );
  10. M_Lock.Unlock ();
  11. }
  12. Bool Get (unsigned int nIndex, SCard * pCard)
  13. {
  14. Bool bRet = false;
  15. M_Lock.Lock ();
  16. BRet = m_Poker.Get (nIndex, pCard );
  17. M_Lock.Unlock ();
  18. Return bRet;
  19. }
  20. Void PrintInfo (void)
  21. {
  22. M_Lock.Lock ();
  23. M_Poker.PrintInfo ();
  24. M_Lock.Unlock ();
  25. }
  26. Private:
  27. CPoker m_Poker; // "resource lock" concept, private aggregation, locking of all public methods to ensure security
  28. CMutexLock m_Lock; // This is the cross-platform security lock in 0bug-C/C ++ commercial engineering. See section 6.2, P226
  29. };
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:
  1. Inline void TestCPoker (void)
  2. {
  3. CPokerWithLock Poker;
  4. Srand (unsigned int) time (NULL ));
  5. Poker. PrintInfo ();
  6. Poker. Wash ();
  7. Poker. PrintInfo ();
  8. Poker. Wash ();
  9. Poker. PrintInfo ();
  10. }
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.