今天开始打算讲解下cocos2dx下如何制作国标麻将
前半部分先讲解麻将的逻辑部分,因为都是代码,可能会比较枯燥无聊. 这部分讲完后,你也可以用其他游戏引擎来制作麻将
后半部分,就讲解余下的cocos2dx部分,因为要把这部分留到后面讲,主要是还在考虑用3d做还是用2d做.
到最后可以扩展AI部分的机器人 和 服务器模块
cocos2dx 制作单机麻将(一)
麻将逻辑1. 打乱麻将顺序(初始化牌堆)
国标麻将共有144张牌
#define MAX_REPERTORY144
每种牌都是1-9 共有4张,风牌就是东南西北,箭牌就是中发白,花牌就是春夏秋冬梅兰竹菊
const BYTE m_cbCardDataArray[ MAX_REPERTORY ]=
{
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//万子
0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,//同子
0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,//索子
//索子
0x31,0x32,0x33,0x34,//风牌
0x41,0x42,0x43,//箭牌
//箭牌
0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,//花牌
};
有了顺序的数组,然后就可以打乱
打乱牌的函数
//混乱扑克
#define CountArray(Array) (sizeof(Array)/sizeof(Array[0]))
staticvoidRandCardData(BYTEcbCardData[],BYTEcbMaxCount)
{
//混乱准备
BYTEcbCardDataTemp[CountArray(m_cbCardDataArray)];//为什么直接用MAX_REPERTORY?因为这样无耦合
memcpy(cbCardDataTemp,m_cbCardDataArray,sizeof(m_cbCardDataArray));//拷贝一份到临时牌数组中
//混乱扑克(关键的核心打乱代码)
BYTEcbRandCount=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
- //
- //CreatedbyTinyUlton14-8-16.
- //copyright(c)2014年TinyUlt.Allrightsreserved.
- #include<iostream>
- usingnamespacestd;
- #defineMAX_REPERTORY144
- typedefunsignedcharBYTE;
- //数组维数
- #ifndefCountArray
- #defineCountArray(Array)(sizeof(Array)/sizeof(Array[0]))
- #endif
- constBYTEm_cbCardDataArray[MAX_REPERTORY]=
- {
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//万子
- 0x01,0); background-color:inherit">//万子
- 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0); background-color:inherit">//同子
- 0x11,0); background-color:inherit">//同子
- 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0); background-color:inherit">//索子
- 0x21,0); background-color:inherit">//索子
- 0x31,0x32,0x33,0x34,0); background-color:inherit">//风牌
- 0x31,0); background-color:inherit">//风牌
- 0x41,0x42,0x43,0); background-color:inherit">//箭牌
- 0x41,0); background-color:inherit">//箭牌
- 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0); background-color:inherit">//花牌
- };
- //混乱扑克
- staticvoidRandCardData(BYTEcbCardData[],BYTEcbMaxCount)
- //混乱准备
- BYTEcbCardDataTemp[CountArray(m_cbCardDataArray)];//为什么不直接用MAX_REPERTORY?因为这样无耦合
- memcpy(cbCardDataTemp,m_cbCardDataArray,sizeof(m_cbCardDataArray));//拷贝一份到临时牌数组中
- //混乱扑克(关键的核心打乱代码)
- BYTEcbRandCount=0,cbPosition=0;
- do
- cbPosition=rand()%(cbMaxCount-cbRandCount);
- cbCardData[cbRandCount++]=cbCardDataTemp[cbPosition];
- cbCardDataTemp[cbPosition]=cbCardDataTemp[cbMaxCount-cbRandCount];
- }while(cbRandCount<cbMaxCount);
- return;
- }
- intmain(intargc,char*argv[]){
- //insertcodehere...
- BYTE_cardData[MAX_REPERTORY];
- RandCardData(_cardData,MAX_REPERTORY);
- for(inti=0;i<MAX_REPERTORY;i++){
- cout<<hex<<int(_cardData[i])<<"";
- }
- return0;
- }
输出: 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