欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

cocos2dx 制作单机麻将(四)

程序员文章站 2022-05-29 17:52:01
...

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 plaincopycocos2dx 制作单机麻将(四)cocos2dx 制作单机麻将(四)

  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
  87. if(cbCardIndexreturn ((cbCardIndex/9)
  88. if(cbCardIndex>=31&&cbCardIndexreturn(((cbCardIndex/7)
  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)
  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
  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
  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
  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
  187. {
  188. BYTE _cardValue = _cardData2[i];//得到牌堆中的牌
  189. int _index = SwitchToCardIndex(_cardValue);//得到该牌对应的索引
  190. cbCardIndex[_index]++;//该牌型加一
  191. }
  192. cout"输出所有牌型对应的数量"
  193. for (int i = 0; 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"手牌数量为:"
  203. for (int i = 0 ; i
  204. {
  205. cout"(0x"int)cbCardData[i]") ";
  206. }
  207. cout
  208. cout
  209. /*出牌*/
  210. char ch[20];
  211. cout"输入要出的牌(比如 三万): ";