1. 程式人生 > >cocos2dx 制作單機麻將(五)

cocos2dx 制作單機麻將(五)

ostream 結束 size clas false main chan i+1 ntc

cocos2dx 制作單機麻將(五)

麻將邏輯6 最基礎的4人麻將邏輯(輪流循環出牌, 之前學的都能用上 跑起來了!!!)

最基礎的麻將邏輯

依據自己須要 設置麻將人數GAME_PLAYER

基本流程:

初始化牌堆,

推斷莊家

玩家013張牌

玩家113張牌

玩家213張牌

玩家313張牌

莊家摸一張牌

莊家出牌

閑家摸牌

閑家出牌

閑家摸牌

閑家出牌

閑家摸牌

閑家出牌

莊家摸一張牌

莊家出牌

閑家摸牌

閑家出牌

.......

牌堆數為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要出的牌(比方三萬):


cocos2dx 制作單機麻將(五)