cocos2dx 制作单机麻将(五)
cocos2dx 制作单机麻将(五)
麻将逻辑6 最基础的4人麻将逻辑(轮流循环出牌, 之前学的都能用上 跑起来了!!!)
最基础的麻将逻辑
依据自己须要 设置麻将人数GAME_PLAYER
基本流程:
初始化牌堆,
推断庄家
玩家0摸13张牌
玩家1摸13张牌
玩家2摸13张牌
玩家3摸13张牌
庄家摸一张牌
庄家出牌
闲家摸牌
闲家出牌
闲家摸牌
闲家出牌
闲家摸牌
闲家出牌
庄家摸一张牌
庄家出牌
闲家摸牌
闲家出牌
.......
牌堆数为0 结束
其它功能 比方 碰吃杠听胡等等 先不实现
代码不难,非常easy看懂
// // main.cpp // MajiangLogicTest // // Created by TinyUlt on 14-8-16. // Copyright (c) 2014年 TinyUlt. All rights reserved. // /* 最基础的麻将逻辑 其它功能 比方 碰吃杠听胡等等 先不实现 */ #include <iostream> using namespace std; #define MAX_REPERTORY 144 typedef unsigned char BYTE; typedef unsigned short WORD; //数组维数 #ifndef CountArray #define CountArray(Array) (sizeof(Array)/sizeof(Array[0])) #endif //逻辑掩码 #define MASK_COLOR 0xF0 //花色掩码 #define MASK_VALUE 0x0F //数值掩码 #define MAX_INDEX 42 //最大索引 #define MAX_COUNT 14 //最大数目 #define GAME_PLAYER 4 //游戏人数 const BYTE m_cbCardDataArray[MAX_REPERTORY]= { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, //万子 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, //万子 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, //万子 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, //万子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, //同子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, //同子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, //同子 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19, //同子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, //索子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, //索子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, //索子 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29, //索子 0x31,0x32,0x33,0x34, //风牌 0x31,0x32,0x33,0x34, //风牌 0x31,0x32,0x33,0x34, //风牌 0x31,0x32,0x33,0x34, //风牌 0x41,0x42,0x43, //箭牌 0x41,0x42,0x43, //箭牌 0x41,0x42,0x43, //箭牌 0x41,0x42,0x43, //箭牌 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58, //花牌 }; const char* m_cbCardWordArray[MAX_INDEX]= { "一万","二万","三万","四万","五万","六万","七万","八万","九万", "一筒","二筒","三筒","四筒","五筒","六筒","七筒","八筒","九筒", "一索","二索","三索","四索","五索","六索","七索","八索","九索", "东", "南", "西", "北", "中", "发", "白", "春", "夏", "秋", "冬", "梅", "兰", "竹", "菊" }; //混乱扑克 static void RandCardData(BYTE cbCardData[],BYTE cbMaxCount) { //混乱准备 BYTE cbCardDataTemp[CountArray(m_cbCardDataArray)];//为什么直接用MAX_REPERTORY?由于这样无耦合 memcpy(cbCardDataTemp,m_cbCardDataArray,sizeof(m_cbCardDataArray));//拷贝一份到暂时牌数组中 //混乱扑克(关键的核心打乱代码) BYTE cbRandCount=0,cbPosition=0; do { cbPosition=rand()%(cbMaxCount-cbRandCount); cbCardData[cbRandCount++]=cbCardDataTemp[cbPosition]; cbCardDataTemp[cbPosition]=cbCardDataTemp[cbMaxCount-cbRandCount]; } while (cbRandCount<cbMaxCount); return; } //混乱扑克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; } //扑克转换(索引->牌值) BYTE SwitchToCardData(BYTE cbCardIndex) { //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); return 0; } //扑克转换(牌型->索引) BYTE SwitchToCardIndex(BYTE cbCardData) { // 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); return 0; } //扑克转换 BYTE SwitchToCardData(BYTE cbCardIndex[MAX_INDEX]/*传入统计全部牌数量的表格*/, BYTE cbCardData[MAX_COUNT]/*传出手牌数据*/) { //转换扑克 BYTE cbPosition=0; for (BYTE i=0;i<MAX_INDEX;i++) { if (cbCardIndex[i]!=0) { for (BYTE j=0;j<cbCardIndex[i];j++) { // ASSERT(cbPosition<MAX_COUNT); cbCardData[cbPosition++]=SwitchToCardData(i); } } } return cbPosition;//返回手牌数 } //依据中文牌,得到牌索引 int getIndexByWord(const char* ch) { for (int i = 0; i < MAX_INDEX; i++) { if (!strcmp(ch,m_cbCardWordArray[i])) { return i; } } return -1; } //删除扑克 bool RemoveCard(BYTE cbCardIndex[MAX_INDEX], BYTE cbRemoveCard) { //效验扑克 //ASSERT(IsValidCard(cbRemoveCard)); BYTE cbRemoveIndex=SwitchToCardIndex(cbRemoveCard); //ASSERT(cbCardIndex[cbRemoveIndex]>0); //删除扑克 if (cbCardIndex[cbRemoveIndex]>0) { cbCardIndex[cbRemoveIndex]--; return true; } //失败效验 // ASSERT(FALSE); return false; } int changePlayer(int id) { id += 1; if (id>=GAME_PLAYER) { id = 0; } return id; } int main(int argc, const char * argv[]) { // insert code here... //创建一个空牌堆 BYTE _cardPile[MAX_REPERTORY]; //庄家用户索引 BYTE _bankerId = rand()%GAME_PLAYER; //玩家的手牌表格 BYTE _playerIndex[GAME_PLAYER][MAX_INDEX]; //当前牌堆取牌索引 BYTE _currentInde = 0; //出牌的次数 BYTE _outCardTime = 0; /*第一种混乱发*/ //得到牌堆数据 RandCardData(_cardPile, MAX_REPERTORY); //输出牌堆数据 cout<<"混乱初始牌堆"<<endl; for (int i = 0 ; i < MAX_REPERTORY; i++) { cout<<hex<<"0x"<<int(_cardPile[i])<<" "; } cout<<endl<<endl; //初始化玩家的牌数据 for (int i = 0; i < GAME_PLAYER; i++) { for ( ; _currentInde < (MAX_COUNT-1)*(i+1); _currentInde++) { BYTE _cardValue = _cardPile[_currentInde];//得到牌堆中的牌 int _index = SwitchToCardIndex(_cardValue);//得到该牌相应的索引 _playerIndex[i][_index]++;//该牌型加一 } } cout<<"-----------------------------------"<<endl; for (int j = 0; j< GAME_PLAYER; j++) { cout<<"玩家"<<j<<"的牌数据:"<<endl; for (int i = 0; i< MAX_INDEX; i++) { cout<<hex<<m_cbCardWordArray[i]<<"(0x"<<int(SwitchToCardData(i))<<"):"<<dec<<(int)_playerIndex[j][i]<<" ";//输出手牌中全部牌型相应的数量 } cout<<endl<<endl; } cout<<"-----------------------------------"<<endl; for (int j = 0; j< GAME_PLAYER; j++) { cout<<"玩家"<<j<<"的手牌:"<<endl; BYTE _handCardData[MAX_COUNT]; int _handandsCount = (int)SwitchToCardData(_playerIndex[j],_handCardData); for (int i = 0; i< _handandsCount; i++) { cout<<m_cbCardWordArray[SwitchToCardIndex(_handCardData[i])]<<"(0x"<<hex<<(int)_handCardData[i]<<") "; } cout<<endl<<endl; } cout<<"-----------------------------------"<<endl; //初始化工作做好后, 開始麻将的出牌逻辑循环 cout<<"庄家: 玩家"<<(int)_bankerId<<endl; int _currentPlayer = _bankerId; while (_currentInde < MAX_REPERTORY) { //出牌次数 cout<<"共出牌次数:"<<dec<<(int)_outCardTime<<endl; //准备出牌的玩家先从牌堆中取牌 BYTE _cardValue = _cardPile[_currentInde++];//得到牌堆中的牌 int _index = SwitchToCardIndex(_cardValue);//得到该牌相应的索引 _playerIndex[_currentPlayer][_index]++;//该牌型加一 cout<<"从牌堆中取牌:"<<m_cbCardWordArray[SwitchToCardIndex(_cardValue)]<<endl; cout<<"牌堆剩余:"<<dec<<MAX_REPERTORY-_currentInde<<endl; //显示手牌 { BYTE _handCardData[MAX_COUNT]; int _handandsCount = (int)SwitchToCardData(_playerIndex[_currentPlayer],_handCardData); cout<<"玩家"<<_currentPlayer<<"的手牌有"<<dec<<_handandsCount<<"张:"<<endl; for (int i = 0; i< _handandsCount; i++) { cout<<m_cbCardWordArray[SwitchToCardIndex(_handCardData[i])]<<"(0x"<<hex<<(int)_handCardData[i]<<") "; } cout<<endl; } //输入要出的牌 recin: char ch[20]; cout<<"玩家"<<_currentPlayer<<"要出的牌(比方 三万): "; cin>>ch; int _outCardIndex = getIndexByWord(ch); if (_outCardIndex == -1) { cout<<"输入错误,请又一次输入"<<endl; goto recin; } //依据索引得到麻将牌型值 BYTE _outCardValue = SwitchToCardData(_outCardIndex); //依据牌型值删除手中的牌 bool _b = RemoveCard(_playerIndex[_currentPlayer], _outCardValue); if (_b) { cout<<"出牌成功,轮到下家"<<endl; _outCardTime++; } else { cout<<"输入错误,请又一次输入"<<endl; goto recin; } //显示手牌 { BYTE _handCardData[MAX_COUNT]; int _handandsCount = (int)SwitchToCardData(_playerIndex[_currentPlayer],_handCardData); cout<<"出牌后玩家"<<_currentPlayer<<"的手牌有"<<dec<<_handandsCount<<"张:"<<endl; for (int i = 0; i< _handandsCount; i++) { cout<<m_cbCardWordArray[SwitchToCardIndex(_handCardData[i])]<<"(0x"<<hex<<(int)_handCardData[i]<<") "; } cout<<endl; } cout<<"-----------------------------------"<<endl; _currentPlayer = changePlayer(_currentPlayer); } return 0; }
输出:
混乱初始牌堆
0x28 0x19 0x42 0x25 0x43 0x31 0x33 0x6 0x32 0x11 0x12 0x24 0x13 0x34 0x5 0x2 0x9 0x32 0x2 0x19 0x43 0x21 0x15 0x21 0x24 0x26 0x42 0x19 0x5 0x43 0x57 0x5 0x23 0x51 0x41 0x23 0x22 0x18 0x7 0x58 0x8 0x54 0x21 0x15 0x56 0x23 0x14 0x24 0x25 0x4 0x26 0x31 0x55 0x6 0x16 0x11 0x16 0x7 0x8 0x29 0x18 0x26 0x41 0x32 0x34 0x21 0x11 0x4 0x28 0x29 0x52 0x23 0x18 0x22 0x12 0x9 0x53 0x9 0x6 0x29 0x19 0x1 0x2 0x25 0x9 0x8 0x5 0x4 0x42 0x32 0x16 0x1 0x24 0x33 0x22 0x14 0x27 0x1 0x25 0x17 0x2 0x16 0x34 0x7 0x17 0x34 0x33 0x41 0x13 0x28 0x7 0x14 0x13 0x14 0x6 0x27 0x4 0x3 0x33 0x41 0x43 0x26 0x11 0x15 0x27 0x3 0x3 0x3 0x1 0x17 0x12 0x22 0x42 0x8 0x18 0x29 0x31 0x15 0x31 0x27 0x13 0x12 0x17 0x28
-----------------------------------
玩家0的牌数据:
一万(0x1):0 二万(0x2):0 三万(0x3):0四万(0x4):0五万(0x5):0六万(0x6):1七万(0x7):0八万(0x8):0九万(0x9):0一筒(0x11):1二筒(0x12):1三筒(0x13):1四筒(0x14):0五筒(0x15):0六筒(0x16):0七筒(0x17):0八筒(0x18):0九筒(0x19):1一索(0x21):0二索(0x22):0三索(0x23):0四索(0x24):1五索(0x25):1六索(0x26):0七索(0x27):0八索(0x28):1九索(0x29):0东(0x31):1南(0x32):1西(0x33):1北(0x34):0中(0x41):0发(0x42):1白(0x43):1春(0x51):0夏(0x52):0秋(0x53):0冬(0x54):0梅(0x55):0兰(0x56):0竹(0x57):0菊(0x58):0
玩家1的牌数据:
一万(0x1):0 二万(0x2):2 三万(0x3):0四万(0x4):0五万(0x5):1六万(0x6):0七万(0x7):0八万(0x8):0九万(0x9):1一筒(0x11):0二筒(0x12):0三筒(0x13):0四筒(0x14):0五筒(0x15):1六筒(0x16):0七筒(0x17):0八筒(0x18):0九筒(0x19):1一索(0x21):2二索(0x22):0三索(0x23):0四索(0x24):1五索(0x25):0六索(0x26):1七索(0x27):0八索(0x28):0九索(0x29):0东(0x31):0南(0x32):1西(0x33):0北(0x34):1中(0x41):0发(0x42):0白(0x43):1春(0x51):0夏(0x52):0秋(0x53):0冬(0x54):0梅(0x55):0兰(0x56):0竹(0x57):0菊(0x58):0
玩家2的牌数据:
一万(0x1):0 二万(0x2):0 三万(0x3):0四万(0x4):0五万(0x5):2六万(0x6):0七万(0x7):1八万(0x8):0九万(0x9):0一筒(0x11):0二筒(0x12):0三筒(0x13):0四筒(0x14):0五筒(0x15):0六筒(0x16):0七筒(0x17):0八筒(0x18):1九筒(0x19):1一索(0x21):0二索(0x22):1三索(0x23):2四索(0x24):0五索(0x25):0六索(0x26):0七索(0x27):0八索(0x28):0九索(0x29):0东(0x31):0南(0x32):0西(0x33):0北(0x34):0中(0x41):1发(0x42):1白(0x43):1春(0x51):1夏(0x52):0秋(0x53):0冬(0x54):0梅(0x55):0兰(0x56):0竹(0x57):1菊(0x58):0
玩家3的牌数据:
一万(0x1):0 二万(0x2):0 三万(0x3):0四万(0x4):1五万(0x5):0六万(0x6):0七万(0x7):0八万(0x8):1九万(0x9):0一筒(0x11):0二筒(0x12):0三筒(0x13):0四筒(0x14):1五筒(0x15):1六筒(0x16):0七筒(0x17):0八筒(0x18):0九筒(0x19):0一索(0x21):1二索(0x22):0三索(0x23):1四索(0x24):1五索(0x25):1六索(0x26):1七索(0x27):0八索(0x28):0九索(0x29):0东(0x31):1南(0x32):0西(0x33):0北(0x34):0中(0x41):0发(0x42):0白(0x43):0春(0x51):0夏(0x52):0秋(0x53):0冬(0x54):1梅(0x55):0兰(0x56):1竹(0x57):0菊(0x58):1
-----------------------------------
玩家0的手牌:
六万(0x6) 一筒(0x11)二筒(0x12)三筒(0x13)九筒(0x19)四索(0x24)五索(0x25)八索(0x28)东(0x31)南(0x32)西(0x33)发(0x42)白(0x43)
玩家1的手牌:
二万(0x2) 二万(0x2)五万(0x5)九万(0x9)五筒(0x15)九筒(0x19)一索(0x21)一索(0x21)四索(0x24)六索(0x26)南(0x32)北(0x34)白(0x43)
玩家2的手牌:
五万(0x5) 五万(0x5)七万(0x7)八筒(0x18)九筒(0x19)二索(0x22)三索(0x23)三索(0x23)中(0x41)发(0x42)白(0x43)春(0x51)竹(0x57)
玩家3的手牌:
四万(0x4) 八万(0x8)四筒(0x14)五筒(0x15)一索(0x21)三索(0x23)四索(0x24)五索(0x25)六索(0x26)东(0x31)冬(0x54)兰(0x56)菊(0x58)
-----------------------------------
庄家: 玩家3
共出牌次数:0
从牌堆中取牌:梅
牌堆剩余:91
玩家3的手牌有14张:
四万(0x4) 八万(0x8)四筒(0x14)五筒(0x15)一索(0x21)三索(0x23)四索(0x24)五索(0x25)六索(0x26)东(0x31)冬(0x54)梅(0x55)兰(0x56)菊(0x58)
玩家3要出的牌(比方三万):五万
输入错误,请又一次输入
玩家3要出的牌(比方三万):八万
出牌成功,轮到下家
出牌后玩家3的手牌有13张:
四万(0x4) 四筒(0x14)五筒(0x15)一索(0x21)三索(0x23)四索(0x24)五索(0x25)六索(0x26)东(0x31)冬(0x54)梅(0x55)兰(0x56)菊(0x58)
-----------------------------------
共出牌次数:1
从牌堆中取牌:六万
牌堆剩余:90
玩家0的手牌有14张:
六万(0x6) 六万(0x6)一筒(0x11)二筒(0x12)三筒(0x13)九筒(0x19)四索(0x24)五索(0x25)八索(0x28)东(0x31)南(0x32)西(0x33)发(0x42)白(0x43)
玩家0要出的牌(比方三万):六万
出牌成功,轮到下家
出牌后玩家0的手牌有13张:
六万(0x6) 一筒(0x11)二筒(0x12)三筒(0x13)九筒(0x19)四索(0x24)五索(0x25)八索(0x28)东(0x31)南(0x32)西(0x33)发(0x42)白(0x43)
-----------------------------------
共出牌次数:2
从牌堆中取牌:六筒
牌堆剩余:89
玩家1的手牌有14张:
二万(0x2) 二万(0x2)五万(0x5)九万(0x9)五筒(0x15)六筒(0x16)九筒(0x19)一索(0x21)一索(0x21)四索(0x24)六索(0x26)南(0x32)北(0x34)白(0x43)
玩家1要出的牌(比方三万):五万
出牌成功,轮到下家
出牌后玩家1的手牌有13张:
二万(0x2) 二万(0x2)九万(0x9)五筒(0x15)六筒(0x16)九筒(0x19)一索(0x21)一索(0x21)四索(0x24)六索(0x26)南(0x32)北(0x34)白(0x43)
-----------------------------------
共出牌次数:3
从牌堆中取牌:一筒
牌堆剩余:88
玩家2的手牌有14张:
五万(0x5) 五万(0x5)七万(0x7)一筒(0x11)八筒(0x18)九筒(0x19)二索(0x22)三索(0x23)三索(0x23)中(0x41)发(0x42)白(0x43)春(0x51)竹(0x57)
玩家2要出的牌(比方三万):七万
出牌成功,轮到下家
出牌后玩家2的手牌有13张:
五万(0x5) 五万(0x5)一筒(0x11)八筒(0x18)九筒(0x19)二索(0x22)三索(0x23)三索(0x23)中(0x41)发(0x42)白(0x43)春(0x51)竹(0x57)
-----------------------------------
共出牌次数:4
从牌堆中取牌:六筒
牌堆剩余:87
玩家3的手牌有14张:
四万(0x4) 四筒(0x14)五筒(0x15)六筒(0x16)一索(0x21)三索(0x23)四索(0x24)五索(0x25)六索(0x26)东(0x31)冬(0x54)梅(0x55)兰(0x56)菊(0x58)
玩家3要出的牌(比方三万):五筒
出牌成功,轮到下家
出牌后玩家3的手牌有13张:
四万(0x4) 四筒(0x14)六筒(0x16)一索(0x21)三索(0x23)四索(0x24)五索(0x25)六索(0x26)东(0x31)冬(0x54)梅(0x55)兰(0x56)菊(0x58)
-----------------------------------
共出牌次数:5
从牌堆中取牌:七万
牌堆剩余:86
玩家0的手牌有14张:
六万(0x6) 七万(0x7)一筒(0x11)二筒(0x12)三筒(0x13)九筒(0x19)四索(0x24)五索(0x25)八索(0x28)东(0x31)南(0x32)西(0x33)发(0x42)白(0x43)
玩家0要出的牌(比方三万):