今天開始打算解说下cocos2dx下怎样制作国标麻将
前半部分先解说麻将的逻辑部分,由于都是代码,可能会比較枯燥无聊. 这部分讲完后,你也能够用其它游戏引擎来制作麻将
后半部分,就解说余下的cocos2dx部分, 由于要把这部分留到后面讲,主要是还在考虑用3d做还是用2d做.
到最后能够扩展AI部分的机器人 和 server模块
麻将逻辑1. 打乱麻将顺序(初始化牌堆)
国标麻将共同拥有144张牌
#define MAX_REPERTORY 144
先把全部的牌放入一个常量数组中保存
每种牌都是1-9 共同拥有4张, 风牌就是东南西北,箭牌就是中发白,花牌就是春夏秋冬梅兰竹菊
const BYTE m_cbCardDataArray[MAX_REPERTORY]=
{
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//万子
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//万子
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//万子
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//万子
0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子
0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子
0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子
0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子
0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子
0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子
0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子
0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子
0x31,0x32,0x33,0x34,//风牌
0x31,0x32,0x33,0x34,//风牌
0x31,0x32,0x33,0x34,//风牌
0x31,0x32,0x33,0x34,//风牌
0x41,0x42,0x43,//箭牌
0x41,0x42,0x43,//箭牌
0x41,0x42,0x43,//箭牌
0x41,0x42,0x43,//箭牌
0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,//花牌
};
有了顺序的数组,然后就能够打乱
打乱牌的函数
//混乱扑克
#define CountArray(Array) (sizeof(Array)/sizeof(Array[0]))
static void RandCardData(BYTE cbCardData[],BYTE cbMaxCount)
{
//混乱准备
BYTE cbCardDataTemp[CountArray(m_cbCardDataArray)];//为什么直接用MAX_REPERTORY?由于这样无耦合
memcpy(cbCardDataTemp,m_cbCardDataArray,sizeof(m_cbCardDataArray));//拷贝一份到暂时牌数组中
//混乱扑克(关键的核心打乱代码)
BYTE cbRandCount=0,cbPosition=0;
do
{
cbPosition=rand()%(cbMaxCount-cbRandCount);
cbCardData[cbRandCount++]=cbCardDataTemp[cbPosition];
cbCardDataTemp[cbPosition]=cbCardDataTemp[cbMaxCount-cbRandCount];
} while (cbRandCount<cbMaxCount);
return;
}
这样调用
BYTE _cardData[MAX_REPERTORY];
RandCardData(_cardData, MAX_REPERTORY);
以下是完整的控制台代码
// // 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; //数组维数 #ifndef CountArray #define CountArray(Array) (sizeof(Array)/sizeof(Array[0])) #endif const BYTE m_cbCardDataArray[MAX_REPERTORY]= { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, //万子 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, //万子 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, //万子 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, //万子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, //同子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, //同子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, //同子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, //同子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, //索子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, //索子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, //索子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, //索子 0x31,0x32,0x33,0x34, //风牌 0x31,0x32,0x33,0x34, //风牌 0x31,0x32,0x33,0x34, //风牌 0x31,0x32,0x33,0x34, //风牌 0x41,0x42,0x43, //箭牌 0x41,0x42,0x43, //箭牌 0x41,0x42,0x43, //箭牌 0x41,0x42,0x43, //箭牌 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58, //花牌 }; //混乱扑克 static void RandCardData(BYTE cbCardData[],BYTE cbMaxCount) { //混乱准备 BYTE cbCardDataTemp[CountArray(m_cbCardDataArray)];//为什么不直接用MAX_REPERTORY? 由于这样无耦合 memcpy(cbCardDataTemp,m_cbCardDataArray,sizeof(m_cbCardDataArray));//拷贝一份到暂时牌数组中 //混乱扑克(关键的核心打乱代码) 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<<hex<<int(_cardData[i])<<" "; } return 0; }
输出:
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 18 51 11 42 12 28 2 57 25 16 4 33 15 18 21 42 33 29 41 25 3 23 55 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 52
16 15 27 3 27 31 9 1 26 22 3 32 17 26 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 5Program ended with exit code: 0