Cocos2dx standalone Mahjong (1)

Source: Internet
Author: User

Today, I plan to explain how to make national standard Mahjong under cocos2dx.

The first half explains the logic of mahjong. It may be boring because it is code. After this part, you can use other game engines to make mahjong.

In the second half, I will explain the remaining cocos2dx part. Because I want to leave this part for later, I am still considering using 3D or 2D.

Finally, robot and server modules that can expand AI


Cocos2dx standalone Mahjong (1)


Mahjong logic 1. Disrupt the mahjong sequence (initialize the card heap)

National Standard Mahjong has 144 cards
# Define max_reper1_144


Put all the cards in a constant array and save them.
Each card has four cards from 1 to 9, and the wind brand is in the southeast and northwest China. The wrigley is the medium white, and the flower brand is the spring, summer, and autumn 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, it can be disrupted.
Card disruption Function

// 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? As this is not coupled

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 <cbmaxcount );

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 <iostream> 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,0 09, // Wanzi 0x01, 0 x, // 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, 0 x, 0x29, // suo Zi 0x21, 0x24, 0x25, 0x26, 0x28, 0x29, // suo Zi 0x24, 0x25, 0x26, 0x27,0x28, 0x29, // suo Zi 0x24, 0x25, 0x26, 0 x, 0x29, // suo Zi 0x32, 0x34, // 0x34, // 0x34, // 0x34, 0x34, // 0x43, // Wrigley 0x43, // Wrigley 0x43, // Wrigley 0x43, // Wrigley 0x41,0x42,0X43, // Wrigley 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58, // card }; // chaotic poker static void randcardda TA (byte cbcarddata [], byte cbmaxcount) {// prepare byte cbcarddatatemp [countarray (m_cbcarddataarray)]; // Why not 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 <cbmaxcount); return;} int main (INT argc, const char * argv []) {// insert code here... byte _ carddata [max_repertory]; randcarddata (_ carddata, max_repertory); For (INT I = 0; I <max_repertory; I ++) {cout 


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 5
Program ended with exit code: 0




Cocos2dx standalone Mahjong (1)

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.