cocos2dx 制作单机麻将(三)

cocos2dx 制作单机麻将(三)

麻将逻辑4.得到手牌数据

我们已经保存了一个一维数组, 类似于一个表格,统计出所有牌对应的数量, 但我们怎样得到当前手中是什么牌呢

//扑克转换

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

            {

               // ASSERT(cbPosition

                cbCardData[cbPosition++]=SwitchToCardData(i);

            }

        }

    }

    

    return cbPosition;//返回手牌数

}

目前为止, 我们输出牌的类型都是用16进制表示的,看上去有点乱, 知道了索引和类型值的转换后,我们就可以用中文字代替数字了,
定义中文数组

const char* m_cbCardWordArray[MAX_INDEX]=

{

    "一万","二万","三万","四万","五万","六万","七万","八万","九万",

    "一筒","二筒","三筒","四筒","五筒","六筒","七筒","八筒","九筒",

    "一索","二索","三索","四索","五索","六索","七索","八索","九索",

    "", "", "西", "", "", "", "",

    "", "", "", "", "", "", "", ""

};

根据索引值得到中文.

下面是控制台代码

//
//  main.cpp
//  MajiangLogicTest
//
//  Created by TinyUlt on 14-8-17.
//  Copyright (c) 2014年 TinyUlt. All rights reserved.
//

#include 
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									//最大数目

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牌值)
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
输出:

混乱初始牌堆

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) 

Program ended with exit code: 0



你可能感兴趣的:(cocos2dx)