cocos2dx 制作单机麻将(四)

前端之家收集整理的这篇文章主要介绍了cocos2dx 制作单机麻将(四)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2dx 制作单机麻将(四)

麻将逻辑5.模拟出牌

  1. //
  2. //main.cpp
  3. //MajiangLogicTest
  4. //
  5. //CreatedbyTinyUlton14-8-16.
  6. //Copyright(c)2014年TinyUlt.Allrightsreserved.
  7. #include<iostream>
  8. usingnamespacestd;
  9. #defineMAX_REPERTORY144
  10. typedefunsignedcharBYTE;
  11. shortWORD;
  12. //数组维数
  13. #ifndefCountArray
  14. #defineCountArray(Array)(sizeof(Array)/sizeof(Array[0]))
  15. #endif
  16. //逻辑掩码
  17. #defineMASK_COLOR0xF0//花色掩码
  18. #defineMASK_VALUE0x0F//数值掩码
  19. #defineMAX_INDEX42//最大索引
  20. #defineMAX_COUNT14//最大数目
  21. constBYTEm_cbCardDataArray[MAX_REPERTORY]=
  22. {
  23. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,//万子
  24. 0x01,0); background-color:inherit">//万子
  25. 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0); background-color:inherit">//同子
  26. 0x11,0); background-color:inherit">//同子
  27. 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0); background-color:inherit">//索子
  28. 0x21,0); background-color:inherit">//索子
  29. 0x31,0x32,0x33,0x34,0); background-color:inherit">//风牌
  30. 0x31,0); background-color:inherit">//风牌
  31. 0x41,0x42,0x43,0); background-color:inherit">//箭牌
  32. 0x41,0); background-color:inherit">//箭牌
  33. 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0); background-color:inherit">//花牌
  34. };
  35. char*m_cbCardWordArray[MAX_INDEX]=
  36. {
  37. "一万","二万","三万","四万","五万","六万","七万","八万","九万",
  38. "一筒","二筒","三筒","四筒","五筒","六筒","七筒","八筒","九筒",
  39. "一索","二索","三索","四索","五索","六索","七索","八索","九索",248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "东","南","西","北","中","发","白",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "春","夏","秋","冬","梅","兰","竹","菊"
  40. //混乱扑克
  41. staticvoidRandCardData(BYTEcbCardData[],BYTEcbMaxCount)
  42. //混乱准备
  43. BYTEcbCardDataTemp[CountArray(m_cbCardDataArray)];//为什么直接用MAX_REPERTORY?因为这样无耦合
  44. memcpy(cbCardDataTemp,m_cbCardDataArray,sizeof(m_cbCardDataArray));//拷贝一份到临时牌数组中
  45. //混乱扑克(关键的核心打乱代码)
  46. BYTEcbRandCount=0,cbPosition=0;
  47. do
  48. cbPosition=rand()%(cbMaxCount-cbRandCount);
  49. cbCardData[cbRandCount++]=cbCardDataTemp[cbPosition];
  50. cbCardDataTemp[cbPosition]=cbCardDataTemp[cbMaxCount-cbRandCount];
  51. }while(cbRandCount<cbMaxCount);
  52. return;
  53. }
  54. //混乱扑克2
  55. voidRandAppointCardData(BYTEcbMaxCount,87); background-color:inherit; font-weight:bold">BYTEOriginalData[]/*源牌堆数据*/)
  56. do
  57. cbPosition=rand()%(cbMaxCount-cbRandCount);
  58. cbCardData[cbRandCount++]=OriginalData[cbPosition];
  59. OriginalData[cbPosition]=OriginalData[cbMaxCount-cbRandCount];
  60. }while(cbRandCount<cbMaxCount);
  61. return;
  62. //扑克转换(索引->牌值)
  63. BYTESwitchToCardData(BYTEcbCardIndex)
  64. //assert(cbCardIndex<42);
  65. if(cbCardIndex<31)return((cbCardIndex/9)<<4)|(cbCardIndex%9+1);
  66. if(cbCardIndex>=31&&cbCardIndex<=33)return(((cbCardIndex/7)<<4)+cbCardIndex%10);
  67. if(cbCardIndex>33)return(cbCardIndex+0x2F);
  68. //assert(false);
  69. return0;
  70. //扑克转换(牌型->索引)
  71. BYTESwitchToCardIndex(BYTEcbCardData)
  72. //ASSERT(IsValidCard(cbCardData));
  73. if((cbCardData&MASK_COLOR)<=0x30)
  74. return(((cbCardData&MASK_COLOR)>>4)*9+(cbCardData&MASK_VALUE)-1);
  75. if((cbCardData&MASK_COLOR)==0x40)
  76. return(31+(cbCardData&MASK_VALUE)-1);
  77. if((cbCardData&MASK_COLOR)==0x50)
  78. return(34+(cbCardData&MASK_VALUE)-1);
  79. //ASSERT(false);
  80. return0;
  81. }
  82. //扑克转换
  83. BYTEcbCardIndex[MAX_INDEX]/*传入统计所有牌数量的表格*/,87); background-color:inherit; font-weight:bold">BYTEcbCardData[MAX_COUNT]/*传出手牌数据*/)
  84. //转换扑克
  85. BYTEcbPosition=0;
  86. for(BYTEi=0;i<MAX_INDEX;i++)
  87. if(cbCardIndex[i]!=0)
  88. BYTEj=0;j<cbCardIndex[i];j++)
  89. //ASSERT(cbPosition<MAX_COUNT);
  90. cbCardData[cbPosition++]=SwitchToCardData(i);
  91. returncbPosition;//返回手牌数
  92. //根据中文牌,得到牌索引
  93. intgetIndexByWord(char*ch)
  94. inti=0;i<MAX_INDEX;i++)
  95. if(!strcmp(ch,m_cbCardWordArray[i]))
  96. returni;
  97. return-1;
  98. //删除扑克
  99. boolRemoveCard(BYTEcbCardIndex[MAX_INDEX],87); background-color:inherit; font-weight:bold">BYTEcbRemoveCard)
  100. //效验扑克
  101. //ASSERT(IsValidCard(cbRemoveCard));
  102. BYTEcbRemoveIndex=SwitchToCardIndex(cbRemoveCard);
  103. //ASSERT(cbCardIndex[cbRemoveIndex]>0);
  104. //删除扑克
  105. if(cbCardIndex[cbRemoveIndex]>0)
  106. cbCardIndex[cbRemoveIndex]--;
  107. returntrue;
  108. //失败效验
  109. //ASSERT(FALSE);
  110. false;
  111. intmain(intargc,char*argv[])
  112. //insertcodehere...
  113. /*第一种混乱发*/
  114. //创建一个空牌堆
  115. BYTE_cardData1[MAX_REPERTORY];
  116. //把在该函数中创建并打乱牌堆,然后把指针传给_cardData;
  117. RandCardData(_cardData1,MAX_REPERTORY);
  118. //输出牌数据
  119. cout<<"混乱初始牌堆"<<endl;
  120. inti=0;i<MAX_REPERTORY;i++)
  121. cout<<hex<<"0x"<<int(_cardData1[i])<<"";
  122. cout<<endl;
  123. cout<<endl;
  124. /*第二种混乱发*/
  125. BYTE_cardData2[MAX_REPERTORY];
  126. RandAppointCardData(_cardData2,MAX_REPERTORY,_cardData1);
  127. cout<<"混乱指定牌堆"<<endl;
  128. cout<<"0x"<<int(_cardData2[i])<<"";
  129. /*添加手牌*/
  130. //虚拟一副手牌开始游戏时,每人13张手牌,然后庄家再摸一张牌即14张
  131. //我们使用上面初始化好的牌堆,进行摸牌,假设只有一个玩家
  132. BYTEcbCardIndex[MAX_INDEX];
  133. inti=0;i<MAX_COUNT;i++)
  134. BYTE_cardValue=_cardData2[i];//得到牌堆中的牌
  135. int_index=SwitchToCardIndex(_cardValue);//得到该牌对应的索引
  136. cbCardIndex[_index]++;//该牌型加一
  137. cout<<"输出所有牌型对应的数量"<<endl;
  138. inti=0;i<MAX_INDEX;i++)
  139. cout<<hex<<m_cbCardWordArray[i]<<"(0x"<<int(SwitchToCardData(i))<<"):"<<dec<<(int)cbCardIndex[i]<<"";//输出手牌中所有牌型对应的数量
  140. cout<<"输出手牌数据"<<endl;
  141. BYTEcbCardData[MAX_COUNT];
  142. int_handsCount=(int)SwitchToCardData(cbCardIndex,cbCardData);
  143. cout<<"手牌数量为:"<<_handsCount<<endl;
  144. inti=0;i<MAX_COUNT;i++)
  145. cout<<m_cbCardWordArray[SwitchToCardIndex(cbCardData[i])]<<"(0x"<<hex<<(int)cbCardData[i]<<")";
  146. /*出牌*/
  147. charch[20];
  148. cout<<"输入要出的牌(比如三万):";
  149. cin>>ch;
  150. int_outCardIndex=getIndexByWord(ch);
  151. if(_outCardIndex==-1)
  152. cout<<"输入错误"<<endl;
  153. BYTE_outCardValue=SwitchToCardData(_outCardIndex);
  154. bool_b=RemoveCard(cbCardIndex,_outCardValue);
  155. if(_b)
  156. cout<<"出牌成功"<<endl;
  157. else
  158. cout<<"该牌不存在"<<endl;
  159. BYTE_leftCardData[MAX_COUNT];
  160. int_leftHandsCount=( cout<<"手牌数量为:"<<dec<<_leftHandsCount<<endl;
  161. cout<<"手牌数据为:"<<endl;
  162. inti=0;i<_leftHandsCount;i++)
  163. cout<<m_cbCardWordArray[SwitchToCardIndex(_leftCardData[i])]<<"(0x"<<hex<<(int)_leftCardData[i]<<")";
  164. cout<<endl<<endl;
  165. }
  166. @H_16_1404@
输出:

混乱初始牌堆

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

输出手牌数据

手牌数量:14

七万(0x7)二筒(0x12)四筒(0x14)六筒(0x16)六筒(0x16)一索(0x21)一索(0x21)四索(0x24)六索(0x26)八索(0x28)(0x31)(0x41)(0x43)(0x56)

输入要出的牌(比如三万):七万

出牌成功

手牌数量:13

手牌数据为:

二筒(0x12)四筒(0x14)六筒(0x16)六筒(0x16)一索(0x21)一索(0x21)四索(0x24)六索(0x26)八索(0x28)(0x31)(0x41)(0x43)(0x56)


Program ended with exit code: 0

原文链接:https://www.f2er.com/cocos2dx/345606.html

猜你在找的Cocos2d-x相关文章