cocos2dx 制作单机麻将(二)

来源:互联网 发布:sql 包含某几个字符串 编辑:程序博客网 时间:2024/04/30 15:16

cocos2dx 制作单机麻将(二)

打乱麻将顺序2

前面讲解了如何打乱初始给定的麻将牌堆, 还有一种是打乱任意给定的麻将牌堆

//混乱扑克2

void RandAppointCardData(BYTE cbCardData[],BYTE cbMaxCount,BYTE OriginalData[]/*源牌堆数据*/)

{

    //混乱扑克

    BYTE cbRandCount=0,cbPosition=0;

    do

    {

        cbPosition=rand()%(cbMaxCount-cbRandCount);

        cbCardData[cbRandCount++]=OriginalData[cbPosition];

        OriginalData[cbPosition]=OriginalData[cbMaxCount-cbRandCount];

    } while (cbRandCount<cbMaxCount);

    

    return;

}

下面是完整控制台代码

[cpp] view plaincopy
  1. //  
  2. //  main.cpp  
  3. //  MajiangLogicTest  
  4. //  
  5. //  Created by TinyUlt on 14-8-16.  
  6. //  Copyright (c) 2014年 TinyUlt. All rights reserved.  
  7. //  
  8.   
  9. #include <iostream>  
  10. using namespace std;  
  11.   
  12. #define MAX_REPERTORY 144  
  13. typedef unsigned char BYTE;  
  14. typedef unsigned short WORD;  
  15. //数组维数  
  16. #ifndef CountArray  
  17. #define CountArray(Array) (sizeof(Array)/sizeof(Array[0]))  
  18. #endif  
  19. const BYTE m_cbCardDataArray[MAX_REPERTORY]=  
  20. {  
  21.     0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       //万子  
  22.     0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       //万子  
  23.     0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       //万子  
  24.     0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       //万子  
  25.     0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       //同子  
  26.     0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       //同子  
  27.     0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       //同子  
  28.     0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       //同子  
  29.     0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,                       //索子  
  30.     0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,                       //索子  
  31.     0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,                       //索子  
  32.     0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,                       //索子  
  33.       
  34.     0x31,0x32,0x33,0x34,                                                //风牌  
  35.     0x31,0x32,0x33,0x34,                                                //风牌  
  36.     0x31,0x32,0x33,0x34,                                                //风牌  
  37.     0x31,0x32,0x33,0x34,                                                //风牌  
  38.     0x41,0x42,0x43,                                                     //箭牌  
  39.     0x41,0x42,0x43,                                                     //箭牌  
  40.     0x41,0x42,0x43,                                                     //箭牌  
  41.     0x41,0x42,0x43,                                                     //箭牌  
  42.       
  43.     0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,                            //花牌  
  44.       
  45. };  
  46. //混乱扑克  
  47. static void RandCardData(BYTE cbCardData[],BYTE cbMaxCount)  
  48. {  
  49.     //混乱准备  
  50.     BYTE cbCardDataTemp[CountArray(m_cbCardDataArray)];//为什么直接用MAX_REPERTORY? 因为这样无耦合  
  51.     memcpy(cbCardDataTemp,m_cbCardDataArray,sizeof(m_cbCardDataArray));//拷贝一份到临时牌数组中  
  52.       
  53.     //混乱扑克(关键的核心打乱代码)  
  54.     BYTE cbRandCount=0,cbPosition=0;  
  55.     do  
  56.     {  
  57.         cbPosition=rand()%(cbMaxCount-cbRandCount);  
  58.         cbCardData[cbRandCount++]=cbCardDataTemp[cbPosition];  
  59.         cbCardDataTemp[cbPosition]=cbCardDataTemp[cbMaxCount-cbRandCount];  
  60.     } while (cbRandCount<cbMaxCount);  
  61.       
  62.     return;  
  63.       
  64. }  
  65. //混乱扑克2  
  66. void RandAppointCardData(BYTE cbCardData[],BYTE cbMaxCount,BYTE OriginalData[]/*源牌堆数据*/)  
  67. {  
  68.     //混乱扑克  
  69.     BYTE cbRandCount=0,cbPosition=0;  
  70.     do  
  71.     {  
  72.         cbPosition=rand()%(cbMaxCount-cbRandCount);  
  73.         cbCardData[cbRandCount++]=OriginalData[cbPosition];  
  74.         OriginalData[cbPosition]=OriginalData[cbMaxCount-cbRandCount];  
  75.     } while (cbRandCount<cbMaxCount);  
  76.       
  77.     return;  
  78. }  
  79.   
  80.   
  81. int main(int argc, const char * argv[]) {  
  82.     // insert code here...  
  83.       
  84.     /*第一种混乱*/  
  85.     //创建一个空牌堆  
  86.     BYTE _cardData1[MAX_REPERTORY];  
  87.     //把在该函数中创建并打乱牌堆,然后把指针传给_cardData;  
  88.     RandCardData(_cardData1, MAX_REPERTORY);  
  89.     //输出牌数据  
  90.     for (int i = 0 ; i < MAX_REPERTORY; i++) {  
  91.         cout<<hex<<int(_cardData1[i])<<" ";  
  92.     }  
  93.     cout<<endl;  
  94.       
  95.     /*第二种混乱*/  
  96.     //创建一个空牌堆  
  97.     BYTE _cardData2[MAX_REPERTORY];  
  98.     //把在该函数中创建并打乱牌堆,然后把指针传给_cardData;  
  99.     RandAppointCardData(_cardData2, MAX_REPERTORY,_cardData1);  
  100.     //输出牌数据  
  101.     for (int i = 0 ; i < MAX_REPERTORY; i++) {  
  102.         cout<<hex<<int(_cardData2[i])<<" ";  
  103.     }  
  104.     cout<<endl;  
  105.     return 0;  
  106. }  
输出:

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 5 

16 56 21 7 28 14 41 12 16 24 43 21 31 26 3 53 52 7 12 34 51 14 9 29 23 33 12 31 14 6 16 18 54 21 25 58 19 5 7 28 32 34 1 27 27 33 6 14 9 17 25 33 28 11 17 24 43 2 22 6 23 3 11 42 2 18 3 4 42 4 18 55 25 42 22 32 4 15 8 29 24 13 6 26 19 9 41 25 7 8 1 13 11 15 41 43 57 16 33 18 32 27 1 8 12 31 4 5 27 22 26 23 31 2 5 17 26 13 19 43 17 21 42 5 3 19 23 15 28 15 8 24 9 29 13 32 34 2 34 41 11 29 22 1 

Program ended with exit code: 0


麻将逻辑3. 初始化手牌

麻将的逻辑前提是有数据的支持, 所有有良好的数据存储方式是很有必要的.
麻将的牌记录一般采取比较通用的方法,即一个一维的数组, 长度是牌型的数量, 元素的值为牌的数量
例如

#define MAX_INDEX 42//最大索引
BYTE cbCardIndex[MAX_INDEX]

因为牌的类型共有42种  1万-9万 , 1筒-9筒, 1索-9索, 东南西北中发白(7),春夏秋冬梅兰竹菊(8)
9+9+9+7+8 = 42. 

如果每摸到一张 1万  只要 cbCardIndex[0]++, 摸到 3万  cbCardIndex[2]++ ,  摸到东风就cbCardIndex[0x31]++吗?
你会发现牌值是用16进制表示的(回顾第一讲), 所有我们不能用cbCardIndex[牌值] 来表示该类型牌的数量
比如 我想得到手中1筒的数量  不能用cbCardIndex[0x11], 应该用cbCardIndex[9]. 所有我们应该要有个可以让牌型值和索引互相转换的函数
即 实现这样的功能 cbCardIndex[func(9)] == cbCardIndex[0x11], 这样我们就可以用牌型值来取该类型牌的数量了
直接上代码了  不早了 该睡觉了

[cpp] view plaincopy
  1. //  
  2. //  main.cpp  
  3. //  MajiangLogicTest  
  4. //  
  5. //  Created by TinyUlt on 14-8-17.  
  6. //  Copyright (c) 2014年 TinyUlt. All rights reserved.  
  7. //  
  8.   
  9. #include <iostream>  
  10. using namespace std;  
  11.   
  12. #define MAX_REPERTORY 144  
  13. typedef unsigned char BYTE;  
  14. typedef unsigned short WORD;  
  15. //数组维数  
  16. #ifndef CountArray  
  17. #define CountArray(Array) (sizeof(Array)/sizeof(Array[0]))  
  18. #endif  
  19. //逻辑掩码  
  20.   
  21. #define MASK_COLOR                  0xF0                                //花色掩码  
  22. #define MASK_VALUE                  0x0F                                //数值掩码  
  23. #define MAX_INDEX   42  //最大索引  
  24. #define MAX_COUNT                   14                                  //最大数目  
  25.   
  26. const BYTE m_cbCardDataArray[MAX_REPERTORY]=  
  27. {  
  28.     0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       //万子  
  29.     0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       //万子  
  30.     0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       //万子  
  31.     0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       //万子  
  32.     0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       //同子  
  33.     0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       //同子  
  34.     0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       //同子  
  35.     0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       //同子  
  36.     0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,                       //索子  
  37.     0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,                       //索子  
  38.     0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,                       //索子  
  39.     0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,                       //索子  
  40.       
  41.     0x31,0x32,0x33,0x34,                                                //风牌  
  42.     0x31,0x32,0x33,0x34,                                                //风牌  
  43.     0x31,0x32,0x33,0x34,                                                //风牌  
  44.     0x31,0x32,0x33,0x34,                                                //风牌  
  45.     0x41,0x42,0x43,                                                     //箭牌  
  46.     0x41,0x42,0x43,                                                     //箭牌  
  47.     0x41,0x42,0x43,                                                     //箭牌  
  48.     0x41,0x42,0x43,                                                     //箭牌  
  49.       
  50.     0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,                            //花牌  
  51.       
  52. };  
  53. //混乱扑克  
  54. static void RandCardData(BYTE cbCardData[],BYTE cbMaxCount)  
  55. {  
  56.     //混乱准备  
  57.     BYTE cbCardDataTemp[CountArray(m_cbCardDataArray)];//为什么直接用MAX_REPERTORY? 因为这样无耦合  
  58.     memcpy(cbCardDataTemp,m_cbCardDataArray,sizeof(m_cbCardDataArray));//拷贝一份到临时牌数组中  
  59.       
  60.     //混乱扑克(关键的核心打乱代码)  
  61.     BYTE cbRandCount=0,cbPosition=0;  
  62.     do  
  63.     {  
  64.         cbPosition=rand()%(cbMaxCount-cbRandCount);  
  65.         cbCardData[cbRandCount++]=cbCardDataTemp[cbPosition];  
  66.         cbCardDataTemp[cbPosition]=cbCardDataTemp[cbMaxCount-cbRandCount];  
  67.     } while (cbRandCount<cbMaxCount);  
  68.       
  69.     return;  
  70.       
  71. }  
  72. //混乱扑克2  
  73. void RandAppointCardData(BYTE cbCardData[],BYTE cbMaxCount,BYTE OriginalData[]/*源牌堆数据*/)  
  74. {  
  75.     //混乱扑克  
  76.     BYTE cbRandCount=0,cbPosition=0;  
  77.     do  
  78.     {  
  79.         cbPosition=rand()%(cbMaxCount-cbRandCount);  
  80.         cbCardData[cbRandCount++]=OriginalData[cbPosition];  
  81.         OriginalData[cbPosition]=OriginalData[cbMaxCount-cbRandCount];  
  82.     } while (cbRandCount<cbMaxCount);  
  83.       
  84.     return;  
  85. }  
  86. //扑克转换(索引->牌值)  
  87. BYTE SwitchToCardData(BYTE cbCardIndex)  
  88. {  
  89.     //assert(cbCardIndex<42);  
  90.     if(cbCardIndex<31)   return ((cbCardIndex/9)<<4)|(cbCardIndex%9+1);  
  91.     if(cbCardIndex>=31&&cbCardIndex<=33)  return(((cbCardIndex/7)<<4)+cbCardIndex%10 );  
  92.     if(cbCardIndex>33)   return(cbCardIndex+0x2F);  
  93.     //assert(false);  
  94.     return 0;  
  95. }  
  96. //扑克转换(牌型->索引)  
  97. BYTE SwitchToCardIndex(BYTE cbCardData)  
  98. {  
  99.   //  ASSERT(IsValidCard(cbCardData));  
  100.     if((cbCardData&MASK_COLOR)<=0x30)  
  101.         return (((cbCardData&MASK_COLOR)>>4)*9+(cbCardData&MASK_VALUE)-1);  
  102.     if((cbCardData&MASK_COLOR)==0x40)  
  103.         return (31+(cbCardData&MASK_VALUE)-1);  
  104.     if((cbCardData&MASK_COLOR)==0x50)  
  105.         return (34+(cbCardData&MASK_VALUE)-1);  
  106.     //ASSERT(false);  
  107.     return 0;  
  108. }  
  109.   
  110. int main(int argc, const char * argv[]) {  
  111.     // insert code here...  
  112.       
  113.     /*第一种混乱发*/  
  114.     //创建一个空牌堆  
  115.     BYTE _cardData1[MAX_REPERTORY];  
  116.     //把在该函数中创建并打乱牌堆,然后把指针传给_cardData;  
  117.     RandCardData(_cardData1, MAX_REPERTORY);  
  118.     //输出牌数据  
  119.     cout<<"混乱初始牌堆"<<endl;  
  120.     for (int i = 0 ; i < MAX_REPERTORY; i++) {  
  121.         cout<<hex<<"0x"<<int(_cardData1[i])<<" ";  
  122.     }  
  123.     cout<<endl;  
  124.     cout<<endl;  
  125.   
  126.     /*第二种混乱发*/  
  127.     //创建一个空牌堆  
  128.     BYTE _cardData2[MAX_REPERTORY];  
  129.     //把在该函数中创建并打乱牌堆,然后把指针传给_cardData;  
  130.     RandAppointCardData(_cardData2, MAX_REPERTORY,_cardData1);  
  131.     //输出牌数据  
  132.     cout<<"混乱指定牌堆"<<endl;  
  133.     for (int i = 0 ; i < MAX_REPERTORY; i++) {  
  134.         cout<<"0x"<<int(_cardData2[i])<<" ";  
  135.     }  
  136.     cout<<endl;  
  137.     cout<<endl;  
  138.       
  139.     //虚拟一副手牌 开始游戏时,每人13张手牌,然后庄家再摸一张牌即14张  
  140.     //我们使用上面初始化好的牌堆,进行摸牌,假设只有一个玩家  
  141.     BYTE cbCardIndex[MAX_INDEX];  
  142.     for (int i = 0; i < MAX_COUNT; i++)  
  143.     {  
  144.         BYTE _cardValue = _cardData2[i];//得到牌堆中的牌  
  145.         int _index = SwitchToCardIndex(_cardValue);//得到该牌对应的索引  
  146.         cbCardIndex[_index]++;//该牌型加一  
  147.     }  
  148.       
  149.     cout<<"输出手牌中所有牌型对应的数量"<<endl;  
  150.     for (int i = 0; i< MAX_INDEX; i++) {  
  151.         cout<<"0x"<<hex<<int(SwitchToCardData(i))<<":"<<dec<<(int)cbCardIndex[i]<<" ";//输出手牌中所有牌型对应的数量  
  152.     }  
  153.     cout<<endl;  
  154.     cout<<endl;  
  155.   
  156.     return 0;  
  157. }  

输出:

混乱初始牌堆

0x25 0x13 0x1 0x3 0x21 0x43 0x54 0x14 0x9 0x12 0x13 0x8 0x31 0x24 0x13 0x31 0x6 0x4 0x28 0x31 0x34 0x18 0x7 0x27 0x15 0x18 0x51 0x11 0x42 0x12 0x28 0x2 0x57 0x25 0x16 0x4 0x33 0x15 0x18 0x21 0x42 0x33 0x29 0x41 0x25 0x3 0x23 0x55 0x14 0x41 0x27 0x22 0x34 0x21 0x2 0x9 0x29 0x19 0x43 0x23 0x22 0x22 0x19 0x34 0x16 0x15 0x32 0x58 0x6 0x28 0x17 0x21 0x18 0x8 0x43 0x28 0x33 0x32 0x6 0x33 0x2 0x25 0x14 0x11 0x29 0x19 0x26 0x13 0x4 0x24 0x53 0x52 0x16 0x15 0x27 0x3 0x27 0x31 0x9 0x1 0x26 0x22 0x3 0x32 0x17 0x26 0x26 0x7 0x12 0x42 0x41 0x32 0x17 0x8 0x7 0x9 0x34 0x8 0x7 0x16 0x17 0x41 0x19 0x5 0x29 0x2 0x23 0x6 0x4 0x24 0x42 0x24 0x1 0x56 0x11 0x1 0x12 0x5 0x23 0x11 0x14 0x43 0x5 0x5 


混乱指定牌堆

0x16 0x56 0x21 0x7 0x28 0x14 0x41 0x12 0x16 0x24 0x43 0x21 0x31 0x26 0x3 0x53 0x52 0x7 0x12 0x34 0x51 0x14 0x9 0x29 0x23 0x33 0x12 0x31 0x14 0x6 0x16 0x18 0x54 0x21 0x25 0x58 0x19 0x5 0x7 0x28 0x32 0x34 0x1 0x27 0x27 0x33 0x6 0x14 0x9 0x17 0x25 0x33 0x28 0x11 0x17 0x24 0x43 0x2 0x22 0x6 0x23 0x3 0x11 0x42 0x2 0x18 0x3 0x4 0x42 0x4 0x18 0x55 0x25 0x42 0x22 0x32 0x4 0x15 0x8 0x29 0x24 0x13 0x6 0x26 0x19 0x9 0x41 0x25 0x7 0x8 0x1 0x13 0x11 0x15 0x41 0x43 0x57 0x16 0x33 0x18 0x32 0x27 0x1 0x8 0x12 0x31 0x4 0x5 0x27 0x22 0x26 0x23 0x31 0x2 0x5 0x17 0x26 0x13 0x19 0x43 0x17 0x21 0x42 0x5 0x3 0x19 0x23 0x15 0x28 0x15 0x8 0x24 0x9 0x29 0x13 0x32 0x34 0x2 0x34 0x41 0x11 0x29 0x22 0x1 


输出牌堆中所有牌型对应的数量

0x1:0 0x2:0 0x3:0 0x4:0 0x5:0 0x6:0 0x7:1 0x8:0 0x9:0 0x11:0 0x12:1 0x13:0 0x14:1 0x15:0 0x16:2 0x17:0 0x18:0 0x19:0 0x21:2 0x22:0 0x23:0 0x24:1 0x25:0 0x26:1 0x27:0 0x28:1 0x29:0 0x31:1 0x32:0 0x33:0 0x34:0 0x41:1 0x42:0 0x43:1 0x51:0 0x52:0 0x53:0 0x54:0 0x55:0 0x56:1 0x57:0 0x58:0 


Program ended with exit code: 0

0 0
原创粉丝点击