Cocos2dx standalone Mahjong (1)
Today, I plan to explain how to create national standard mahjong in cocos2dx.
The first half explains the logic of mahjong, because it is Code and may be boring. After this part, you can also use other game engines to create mahjong.
In the second half, we will explain the remaining cocos2dx part, because we will leave this part for later, mainly considering whether to use 3d or 2d.
Finally, the robot and server modules of AI can be expanded.
Cocos2dx standalone Mahjong (1)
Mahjong logic 1. Disrupt Mahjong order
National Standard Mahjong has a total of 144 cards
# Define max_reper1_144
Store all the cards in a constant array.
Each card is a total of 4 from 1 to 9, the wind brand is the Southeast and northwest, the Wrigley is the release of white, the flower card is the spring, summer, autumn and winter plum blossom Chrysanthemum
Const BYTE m_cbCardDataArray [MAX_REPERTORY] =
{
0 x, //
0 x, //
0 x, //
0 x, //
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, // same as the child
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, // same as the child
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, // same as the child
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, // same as the child
0x24, 0x25, 0x26, 0x28, 0x29, // suo Zi
0x24, 0x25, 0x26, 0x28, 0x29, // suo Zi
0x24, 0x25, 0x26, 0x28, 0x29, // suo Zi
0x24, 0x25, 0x26, 0x28, 0x29, // suo Zi
0x31,0x32, 0x33,0x34, // wind brand
0x31,0x32, 0x33,0x34, // wind brand
0x31,0x32, 0x33,0x34, // wind brand
0x31,0x32, 0x33,0x34, // wind brand
0x41,0x42,0x43, // Wrigley
0x41,0x42,0x43, // Wrigley
0x41,0x42,0x43, // Wrigley
0x41,0x42,0x43, // Wrigley
0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58, // card
};
With the ordered array, You can disrupt
To reduce coupling, the function that breaks the card is defined as static
// Chaotic poker
# Define CountArray (Array) (sizeof (Array)/sizeof (Array [0])
Static void RandCardData (BYTE cbCardData [], BYTE cbMaxCount)
{
// Chaotic preparation
BYTE cbCardDataTemp [CountArray (m_cbCardDataArray)]; // Why directly use MAX_REPERTORY? This is because there is no coupling.
Memcpy (cbCardDataTemp, m_cbCardDataArray, sizeof (m_cbCardDataArray); // copy a copy to the temporary card Array
// Chaotic poker (key core disrupting code)
BYTE cbRandCount = 0, cbPosition = 0;
Do
{
CbPosition = rand () % (cbMaxCount-cbRandCount );
CbCardData [cbRandCount ++] = cbCardDataTemp [cbPosition];
CbCardDataTemp [cbPosition] = cbCardDataTemp [cbMaxCount-cbRandCount];
} While (cbRandCount
Return;
}
This call
BYTE _ cardData [MAX_REPERTORY];
RandCardData (_ cardData, MAX_REPERTORY );
The following is the complete console code.
//// Main. cpp // MajiangLogicTest /// Created by TinyUlt on 14-8-16. // Copyright (c) 2014 TinyUlt. all rights reserved. // # include
Using namespace std; # define MAX_REPERTORY 144 typedef unsigned char BYTE; // Array dimension # ifndef CountArray # define CountArray (Array) (sizeof (Array)/sizeof (Array [0]) # endifconst BYTE m_cbCardDataArray [MAX_REPERTORY] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, // Wanzi 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, // Wanzi 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, // Wanzi 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, // Ten thousand sub 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, // same as 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, // same as 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, // same as 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, // same as 0x24, 0x25, 0x26, 0x27,0 x, 0x29, // suo Zi 0x24, 0x25, 0x26, 0x27,0x28, 0x29, // suo Zi 0x24, 0x25, 0x26, 0x27,0x28, 0x29, // suo Zi 0x24, 0x25, 0x26, 0x27,0x28, 0x29, // Suo Zi 0x31,0x32, 0x33,0x34, // Feng Pai 0x31,0x32, 0x33,0x34, // Feng Pai 0x31,0x32, 0x33,0x0x34, // 0x34, // 0x43, // Wrigley 0x43, // Wrigley 0x41,0x42,0x43, // Wrigley 0x41,0x42,0x43, // Wrigley 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58, // card}; // chaotic poker static void RandCardData (BYTE cbCardData [], BYTE cbMaxCount) {// confused preparation BYTE cbCardDataTemp [CountArray (m_cbCardDataArray)]; // Why do I directly use MAX_REPERTORY? Because of this non-coupling memcpy (cbCardDataTemp, m_cbCardDataArray, sizeof (m_cbCardDataArray); // copy a copy to the temporary card array // chaotic poker (key core disrupting code) BYTE cbRandCount = 0, cbPosition = 0; do {cbPosition = rand () % (cbMaxCount-cbRandCount); cbCardData [cbRandCount ++] = cbCardDataTemp [cbPosition]; cbCardDataTemp [cbPosition] = cbCardDataTemp [cbMaxCount-cbRandCount];} while (cbRandCount
Output:
25 13 1 3 21 43 54 14 9 12 13 8 31 24 13 31 6 4 28 31 34 18 7 27 15 11 42 12 28 2 57 25 16 4 33 15 18 21 42 33 29 41 25 3 23 14 41 27 22 34 21 2 9 29 19 43 23 22 22 19 34 16 15 32 58 6 28 17 21 18 8 43 28 33 32 6 33 2 25 14 11 29 19 26 13 4 24 53 16 16 15 27 3 31 9 1 26 22 3 32 17 26 7 12 42 41 32 17 8 7 9 34 8 7 16 17 41 19 5 29 2 23 6 4 24 42 24 1 56 11 1 12 5 23 11 14 43 5Program ended with exit code: 0