当前位置:Gxlcms > mysql > cocos2dx制作单机麻将(四)

cocos2dx制作单机麻将(四)

时间:2021-07-01 10:21:17 帮助过:49人阅读

cocos2dx 制作单机麻将(四) 麻将逻辑5.模拟出牌 [cpp] view plaincopy // //main.cpp //MajiangLogicTest // //CreatedbyTinyUlton14-8-16. //Copyright(c)2014年TinyUlt.Allrightsreserved. // #includeiostream using namespace std; #defineMAX_REPERTORY

cocos2dx 制作单机麻将(四)

麻将逻辑5.模拟出牌

[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. #include
  9. using namespace std;
  10. #define MAX_REPERTORY 144
  11. typedef unsigned char BYTE;
  12. typedef unsigned short WORD;
  13. //数组维数
  14. #ifndef CountArray
  15. #define CountArray(Array) (sizeof(Array)/sizeof(Array[0]))
  16. #endif
  17. //逻辑掩码
  18. #define MASK_COLOR 0xF0 //花色掩码
  19. #define MASK_VALUE 0x0F //数值掩码
  20. #define MAX_INDEX 42 //最大索引
  21. #define MAX_COUNT 14 //最大数目
  22. const BYTE m_cbCardDataArray[MAX_REPERTORY]=
  23. {
  24. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, //万子
  25. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, //万子
  26. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, //万子
  27. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, //万子
  28. 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, //同子
  29. 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, //同子
  30. 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, //同子
  31. 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, //同子
  32. 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, //索子
  33. 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, //索子
  34. 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, //索子
  35. 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, //索子
  36. 0x31,0x32,0x33,0x34, //风牌
  37. 0x31,0x32,0x33,0x34, //风牌
  38. 0x31,0x32,0x33,0x34, //风牌
  39. 0x31,0x32,0x33,0x34, //风牌
  40. 0x41,0x42,0x43, //箭牌
  41. 0x41,0x42,0x43, //箭牌
  42. 0x41,0x42,0x43, //箭牌
  43. 0x41,0x42,0x43, //箭牌
  44. 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58, //花牌
  45. };
  46. const char* m_cbCardWordArray[MAX_INDEX]=
  47. {
  48. "一万","二万","三万","四万","五万","六万","七万","八万","九万",
  49. "一筒","二筒","三筒","四筒","五筒","六筒","七筒","八筒","九筒",
  50. "一索","二索","三索","四索","五索","六索","七索","八索","九索",
  51. "东", "南", "西", "北", "中", "发", "白",
  52. "春", "夏", "秋", "冬", "梅", "兰", "竹", "菊"
  53. };
  54. //混乱扑克
  55. static void RandCardData(BYTE cbCardData[],BYTE cbMaxCount)
  56. {
  57. //混乱准备
  58. BYTE cbCardDataTemp[CountArray(m_cbCardDataArray)];//为什么直接用MAX_REPERTORY? 因为这样无耦合
  59. memcpy(cbCardDataTemp,m_cbCardDataArray,sizeof(m_cbCardDataArray));//拷贝一份到临时牌数组中
  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
  68. return;
  69. }
  70. //混乱扑克2
  71. void RandAppointCardData(BYTE cbCardData[],BYTE cbMaxCount,BYTE OriginalData[]/*源牌堆数据*/)
  72. {
  73. //混乱扑克
  74. BYTE cbRandCount=0,cbPosition=0;
  75. do
  76. {
  77. cbPosition=rand()%(cbMaxCount-cbRandCount);
  78. cbCardData[cbRandCount++]=OriginalData[cbPosition];
  79. OriginalData[cbPosition]=OriginalData[cbMaxCount-cbRandCount];
  80. } while (cbRandCount
  81. return;
  82. }
  83. //扑克转换(索引->牌值)
  84. BYTE SwitchToCardData(BYTE cbCardIndex)
  85. {
  86. //assert(cbCardIndex<42);
  87. if(cbCardIndex<31) return ((cbCardIndex/9)<<4)|(cbCardIndex%9+1);
  88. if(cbCardIndex>=31&&cbCardIndex<=33) return(((cbCardIndex/7)<<4)+cbCardIndex%10 );
  89. if(cbCardIndex>33) return(cbCardIndex+0x2F);
  90. //assert(false);
  91. return 0;
  92. }
  93. //扑克转换(牌型->索引)
  94. BYTE SwitchToCardIndex(BYTE cbCardData)
  95. {
  96. // ASSERT(IsValidCard(cbCardData));
  97. if((cbCardData&MASK_COLOR)<=0x30)
  98. return (((cbCardData&MASK_COLOR)>>4)*9+(cbCardData&MASK_VALUE)-1);
  99. if((cbCardData&MASK_COLOR)==0x40)
  100. return (31+(cbCardData&MASK_VALUE)-1);
  101. if((cbCardData&MASK_COLOR)==0x50)
  102. return (34+(cbCardData&MASK_VALUE)-1);
  103. //ASSERT(false);
  104. return 0;
  105. }
  106. //扑克转换
  107. BYTE SwitchToCardData(BYTE cbCardIndex[MAX_INDEX]/*传入统计所有牌数量的表格*/, BYTE cbCardData[MAX_COUNT]/*传出手牌数据*/)
  108. {
  109. //转换扑克
  110. BYTE cbPosition=0;
  111. for (BYTE i=0;i
  112. {
  113. if (cbCardIndex[i]!=0)
  114. {
  115. for (BYTE j=0;j
  116. {
  117. // ASSERT(cbPosition
  118. cbCardData[cbPosition++]=SwitchToCardData(i);
  119. }
  120. }
  121. }
  122. return cbPosition;//返回手牌数
  123. }
  124. //根据中文牌,得到牌索引
  125. int getIndexByWord(const char* ch)
  126. {
  127. for (int i = 0; i < MAX_INDEX; i++)
  128. {
  129. if (!strcmp(ch,m_cbCardWordArray[i]))
  130. {
  131. return i;
  132. }
  133. }
  134. return -1;
  135. }
  136. //删除扑克
  137. bool RemoveCard(BYTE cbCardIndex[MAX_INDEX], BYTE cbRemoveCard)
  138. {
  139. //效验扑克
  140. //ASSERT(IsValidCard(cbRemoveCard));
  141. BYTE cbRemoveIndex=SwitchToCardIndex(cbRemoveCard);
  142. //ASSERT(cbCardIndex[cbRemoveIndex]>0);
  143. //删除扑克
  144. if (cbCardIndex[cbRemoveIndex]>0)
  145. {
  146. cbCardIndex[cbRemoveIndex]--;
  147. return true;
  148. }
  149. //失败效验
  150. // ASSERT(FALSE);
  151. return false;
  152. }
  153. int main(int argc, const char * argv[])
  154. {
  155. // insert code here...
  156. /*第一种混乱发*/
  157. //创建一个空牌堆
  158. BYTE _cardData1[MAX_REPERTORY];
  159. //把在该函数中创建并打乱牌堆,然后把指针传给_cardData;
  160. RandCardData(_cardData1, MAX_REPERTORY);
  161. //输出牌数据
  162. cout<<"混乱初始牌堆"<
  163. for (int i = 0 ; i < MAX_REPERTORY; i++)
  164. {
  165. cout<"0x"<<int(_cardData1[i])<<" ";
  166. }
  167. cout<
  168. cout<
  169. /*第二种混乱发*/
  170. //创建一个空牌堆
  171. BYTE _cardData2[MAX_REPERTORY];
  172. //把在该函数中创建并打乱牌堆,然后把指针传给_cardData;
  173. RandAppointCardData(_cardData2, MAX_REPERTORY,_cardData1);
  174. //输出牌数据
  175. cout<<"混乱指定牌堆"<
  176. for (int i = 0 ; i < MAX_REPERTORY; i++)
  177. {
  178. cout<<"0x"<<int(_cardData2[i])<<" ";
  179. }
  180. cout<
  181. cout<
  182. /*添加手牌*/
  183. //虚拟一副手牌 开始游戏时,每人13张手牌,然后庄家再摸一张牌即14张
  184. //我们使用上面初始化好的牌堆,进行摸牌,假设只有一个玩家
  185. BYTE cbCardIndex[MAX_INDEX];
  186. for (int i = 0; i < MAX_COUNT; i++)
  187. {
  188. BYTE _cardValue = _cardData2[i];//得到牌堆中的牌
  189. int _index = SwitchToCardIndex(_cardValue);//得到该牌对应的索引
  190. cbCardIndex[_index]++;//该牌型加一
  191. }
  192. cout<<"输出所有牌型对应的数量"<
  193. for (int i = 0; i< MAX_INDEX; i++)
  194. {
  195. cout<"(0x"<<int(SwitchToCardData(i))<<"):"<int)cbCardIndex[i]<<" ";//输出手牌中所有牌型对应的数量
  196. }
  197. cout<
  198. cout<
  199. cout<<"输出手牌数据"<
  200. BYTE cbCardData[MAX_COUNT];
  201. int _handsCount = (int)SwitchToCardData(cbCardIndex,cbCardData);
  202. cout<<"手牌数量为:"<<_handsCount<
  203. for (int i = 0 ; i < MAX_COUNT; i++)
  204. {
  205. cout<"(0x"<int)cbCardData[i]<<") ";
  206. }
  207. cout<
  208. cout<
  209. /*出牌*/
  210. char ch[20];
  211. cout<<"输入要出的牌(比如 三万): ";
  212. cin>>ch;
  213. int _outCardIndex = getIndexByWord(ch);
  214. if (_outCardIndex == -1)
  215. {
  216. cout<<"输入错误"<
  217. return 0;
  218. }
  219. BYTE _outCardValue = SwitchToCardData(_outCardIndex);
  220. bool _b = RemoveCard(cbCardIndex, _outCardValue);
  221. if (_b)
  222. {
  223. cout<<"出牌成功"<
  224. }
  225. else
  226. {
  227. cout<<"该牌不存在"<
  228. }
  229. BYTE _leftCardData[MAX_COUNT];
  230. int _leftHandsCount = (int)SwitchToCardData(cbCardIndex,_leftCardData);
  231. cout<<"手牌数量为:"<
  232. cout<<"手牌数据为:"<
  233. for (int i = 0 ; i < _leftHandsCount; i++)
  234. {
  235. cout<"(0x"<int)_leftCardData[i]<<") ";
  236. }
  237. cout<
  238. return 0;
  239. }

输出:

混乱初始牌堆

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

人气教程排行