cocos2dx 制作单机麻将(四)

cocos2dx 制作单机麻将(四),第1张

概述cocos2dx 制作单机麻将(四) 麻将逻辑5.模拟出牌 [cpp]  view plain copy //   //  main.cpp   //  MajiangLogicTest   //   //  Created by TinyUlt on 14-8-16.   //  Copyright (c) 2014年 TinyUlt. All rights reserved.   //    

cocos2dx 制作单机麻将(四)

麻将逻辑5.模拟出牌

[cpp] view plain copy // //main.cpp //MajiangLogicTest // //CreatedbyTinyulton14-8-16. //copyright(c)2014年Tinyult.Allrightsreserved. #include<iostream> usingnamespacestd; #defineMAX_REPERTORY144 typedefunsignedcharBYTE; shortWORD; //数组维数 #ifndefCountArray #defineCountArray(Array)(sizeof(Array)/sizeof(Array[0])) #endif //逻辑掩码 #defineMASK_color0xF0//花色掩码 #defineMASK_VALUE0x0F//数值掩码 #defineMAX_INDEX42//最大索引 #defineMAX_COUNT14//最大数目 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">//花牌 }; char*m_cbCarDWordArray[MAX_INDEX]= { "一万","二万","三万","四万","五万","六万","七万","八万","九万", "一筒","二筒","三筒","四筒","五筒","六筒","七筒","八筒","九筒", "一索","二索","三索","四索","五索","六索","七索","八索","九索",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"> "春","夏","秋","冬","梅","兰","竹","菊" //混乱扑克 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; } //混乱扑克2 voIDRandAppointCardData(BYTEcbMaxCount,87); background-color:inherit; Font-weight:bold">BYTEOriginalData[]/*源牌堆数据*/) cbposition=rand()%(cbMaxCount-cbRandCount); cbCardData[cbRandCount++]=OriginalData[cbposition]; OriginalData[cbposition]=OriginalData[cbMaxCount-cbRandCount]; }while(cbRandCount<cbMaxCount); return; //扑克转换(索引->牌值) BYTESwitchToCardData(BYTEcbCardindex) //assert(cbCardindex<42); if(cbCardindex<31)return((cbCardindex/9)<<4)|(cbCardindex%9+1); if(cbCardindex>=31&&cbCardindex<=33)return(((cbCardindex/7)<<4)+cbCardindex%10); if(cbCardindex>33)return(cbCardindex+0x2F); //assert(false); return0; //扑克转换(牌型->索引) BYTESwitchToCardindex(BYTEcbCardData) //ASSERT(IsValIDCard(cbCardData)); if((cbCardData&MASK_color)<=0x30) return(((cbCardData&MASK_color)>>4)*9+(cbCardData&MASK_VALUE)-1); if((cbCardData&MASK_color)==0x40) return(31+(cbCardData&MASK_VALUE)-1); if((cbCardData&MASK_color)==0x50) return(34+(cbCardData&MASK_VALUE)-1); //ASSERT(false); return0; } //扑克转换 BYTEcbCardindex[MAX_INDEX]/*传入统计所有牌数量的表格*/,87); background-color:inherit; Font-weight:bold">BYTEcbCardData[MAX_COUNT]/*传出手牌数据*/) //转换扑克 BYTEcbposition=0; for(BYTEi=0;i<MAX_INDEX;i++) if(cbCardindex[i]!=0) BYTEj=0;j<cbCardindex[i];j++) //ASSERT(cbposition<MAX_COUNT); cbCardData[cbposition++]=SwitchToCardData(i); returncbposition;//返回手牌数 //根据中文牌,得到牌索引 intgetIndexByWord(char*ch) inti=0;i<MAX_INDEX;i++) if(!strcmp(ch,m_cbCarDWordArray[i])) returni; return-1; //删除扑克 boolRemoveCard(BYTEcbCardindex[MAX_INDEX],87); background-color:inherit; Font-weight:bold">BYTEcbRemoveCard) //效验扑克 //ASSERT(IsValIDCard(cbRemoveCard)); BYTEcbRemoveIndex=SwitchToCardindex(cbRemoveCard); //ASSERT(cbCardindex[cbRemoveIndex]>0); //删除扑克 if(cbCardindex[cbRemoveIndex]>0) cbCardindex[cbRemoveIndex]--; returntrue; //失败效验 //ASSERT(FALSE); false; intmain(intargc,char*argv[]) //insertcodehere... /*第一种混乱发*/ //创建一个空牌堆 BYTE_cardData1[MAX_REPERTORY]; //把在该函数中创建并打乱牌堆,然后把指针传给_cardData; RandCardData(_cardData1,MAX_REPERTORY); //输出牌数据 cout<<"混乱初始牌堆"<<endl; inti=0;i<MAX_REPERTORY;i++) cout<<hex<<"0x"<<int(_cardData1[i])<<""; cout<<endl; cout<<endl; /*第二种混乱发*/ BYTE_cardData2[MAX_REPERTORY]; RandAppointCardData(_cardData2,MAX_REPERTORY,_cardData1); cout<<"混乱指定牌堆"<<endl; cout<<"0x"<<int(_cardData2[i])<<""; /*添加手牌*/ //虚拟一副手牌开始游戏时,每人13张手牌,然后庄家再摸一张牌即14张 //我们使用上面初始化好的牌堆,进行摸牌,假设只有一个玩家 BYTEcbCardindex[MAX_INDEX]; inti=0;i<MAX_COUNT;i++) BYTE_cardValue=_cardData2[i];//得到牌堆中的牌 int_index=SwitchToCardindex(_cardValue);//得到该牌对应的索引 cbCardindex[_index]++;//该牌型加一 cout<<"输出所有牌型对应的数量"<<endl; inti=0;i<MAX_INDEX;i++) cout<<hex<<m_cbCarDWordArray[i]<<"(0x"<<int(SwitchToCardData(i))<<"):"<<dec<<(int)cbCardindex[i]<<"";//输出手牌中所有牌型对应的数量 cout<<"输出手牌数据"<<endl; BYTEcbCardData[MAX_COUNT]; int_handsCount=(int)SwitchToCardData(cbCardindex,cbCardData); cout<<"手牌数量为:"<<_handsCount<<endl; inti=0;i<MAX_COUNT;i++) cout<<m_cbCarDWordArray[SwitchToCardindex(cbCardData[i])]<<"(0x"<<hex<<(int)cbCardData[i]<<")"; /*出牌*/ charch[20]; cout<<"输入要出的牌(比如三万):"; cin>>ch; int_outCardindex=getIndexByWord(ch); if(_outCardindex==-1) cout<<"输入错误"<<endl; BYTE_outCardValue=SwitchToCardData(_outCardindex); bool_b=RemoveCard(cbCardindex,_outCardValue); if(_b) cout<<"出牌成功"<<endl; else cout<<"该牌不存在"<<endl; BYTE_@R_404_6823@CardData[MAX_COUNT]; int_@R_404_6823@HandsCount=(cout<<"手牌数量为:"<<dec<<_@R_404_6823@HandsCount<<endl; cout<<"手牌数据为:"<<endl; inti=0;i<_@R_404_6823@HandsCount;i++) cout<<m_cbCarDWordArray[SwitchToCardindex(_@R_404_6823@CardData[i])]<<"(0x"<<hex<<(int)_@R_404_6823@CardData[i]<<")"; cout<<endl<<endl; } @H_491_1404@ 输出:

混乱初始牌堆

@H_415_1419@ 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


混乱指定牌堆

@H_415_1419@ 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

输出所有牌型对应的数量

@H_415_1419@ 一万(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

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

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

出牌成功

手牌数量为:13

手牌数据为:

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


@H_415_1419@ Program ended with exit code: 0

总结

以上是内存溢出为你收集整理的cocos2dx 制作单机麻将(四)全部内容,希望文章能够帮你解决cocos2dx 制作单机麻将(四)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1021139.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存