按钮五张【源码】
http://img.woaidaima.com//upload/image/20170511/1494465908147094924.jpghttp://img.woaidaima.com//upload/image/20170511/1494465910418052847.jpg#include "StdAfx.h"#include "GameLogic.h"
//////////////////////////////////////////////////////////////////////////
//扑克数据
BYTE CGameLogic::m_cbCardListData=
{
0x01,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D, //方块 A - K
0x11,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D, //梅花 A - K
0x21,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D, //红桃 A - K
0x31,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D, //黑桃 A - K
};
//////////////////////////////////////////////////////////////////////////
//构造函数
CGameLogic::CGameLogic()
{
}
//析构函数
CGameLogic::~CGameLogic()
{
}
//获取类型
BYTE CGameLogic::GetCardType(BYTE cbCardData[], BYTE cbCardCount)
{
//简单牌形
switch (cbCardCount)
{
case 1: //单牌
{
return CT_SINGLE;
}
case 2: //对牌
{
return (GetCardLogicValue(cbCardData)==GetCardLogicValue(cbCardData))?CT_ONE_DOUBLE:CT_SINGLE;
}
}
//五条类型
if (cbCardCount>=4)
{
//变量定义
bool cbSameColor=true,bLineCard=true;
BYTE cbFirstColor=GetCardColor(cbCardData);
BYTE cbFirstValue=GetCardLogicValue(cbCardData);
//牌形分析
for (BYTE i=1;i<cbCardCount;i++)
{
//数据分析
if (GetCardColor(cbCardData)!=cbFirstColor) cbSameColor=false;
if (cbFirstValue!=(GetCardLogicValue(cbCardData)+i)) bLineCard=false;
//结束判断
if ((cbSameColor==false)&&(bLineCard==false)) break;
}
#ifdef MAGIC_A_CARD
//特殊
if(!bLineCard && GetCardValue(cbCardData)==1)
{
//A J 10 9 8特殊
BYTE cbMinusCount = cbCardCount==4?3:2;
for (BYTE i=1;i<cbCardCount;i++)
{
if ((cbFirstValue-cbMinusCount)!=(GetCardLogicValue(cbCardData)+i)) break;
}
if(i==cbCardCount) bLineCard=true;
}
#endif
//顺子类型
if ((cbSameColor==false)&&(bLineCard==true)) return CT_SHUN_ZI;
//同花类型
if ((cbSameColor==true)&&(bLineCard==false)) return CT_TONG_HUA;
//同花顺类型
if ((cbSameColor==true)&&(bLineCard==true)) return CT_TONG_HUA_SHUN;
}
//扑克分析
tagAnalyseResult AnalyseResult;
AnalysebCardData(cbCardData,cbCardCount,AnalyseResult);
//类型判断
if (AnalyseResult.cbFourCount==1) return CT_TIE_ZHI;
if (AnalyseResult.cbDoubleCount==2) return CT_TWO_DOUBLE;
if ((AnalyseResult.cbDoubleCount==1)&&(AnalyseResult.cbThreeCount==1)) return CT_HU_LU;
if ((AnalyseResult.cbThreeCount==1)&&(AnalyseResult.cbDoubleCount==0)) return CT_THREE_TIAO;
if ((AnalyseResult.cbDoubleCount==1)&&(AnalyseResult.cbThreeCount==0)) return CT_ONE_DOUBLE;
return CT_SINGLE;
}
//排列扑克
void CGameLogic::SortCardList(BYTE cbCardData[], BYTE cbCardCount)
{
//转换数值
BYTE cbLogicValue;
for (BYTE i=0;i<cbCardCount;i++) cbLogicValue=GetCardLogicValue(cbCardData);
//排序操作
bool bSorted=true;
BYTE cbTempData,bLast=cbCardCount-1;
do
{
bSorted=true;
for (BYTE i=0;i<bLast;i++)
{
if ((cbLogicValue<cbLogicValue)||
((cbLogicValue==cbLogicValue)&&(cbCardData<cbCardData)))
{
//交换位置
cbTempData=cbCardData;
cbCardData=cbCardData;
cbCardData=cbTempData;
cbTempData=cbLogicValue;
cbLogicValue=cbLogicValue;
cbLogicValue=cbTempData;
bSorted=false;
}
}
bLast--;
} while(bSorted==false);
return;
}
//混乱扑克
void CGameLogic::RandCardList(BYTE cbCardBuffer[], BYTE cbBufferCount)
{
//混乱准备
BYTE cbCardData;
CopyMemory(cbCardData,m_cbCardListData,sizeof(m_cbCardListData));
//混乱扑克
BYTE bRandCount=0,bPosition=0;
do
{
bPosition=rand()%(CountArray(m_cbCardListData)-bRandCount);
cbCardBuffer=cbCardData;
cbCardData=cbCardData;
} while (bRandCount<cbBufferCount);
return;
}
//逻辑数值
BYTE CGameLogic::GetCardLogicValue(BYTE cbCardData)
{
//扑克属性
BYTE bCardColor=GetCardColor(cbCardData);
BYTE bCardValue=GetCardValue(cbCardData);
//转换数值
return (bCardValue<=2)?(bCardValue+13):bCardValue;
}
//对比扑克
bool CGameLogic::CompareCard(BYTE cbFirstData[], BYTE cbNextData[], BYTE cbCardCount)
{
//获取类型
BYTE cbNextType=GetCardType(cbNextData,cbCardCount);
BYTE cbFirstType=GetCardType(cbFirstData,cbCardCount);
//类型判断
if (cbFirstType!=cbNextType) return (cbFirstType>cbNextType);
//简单类型
switch(cbFirstType)
{
case CT_SINGLE: //单牌
{
//对比数值
for (BYTE i=0;i<cbCardCount;i++)
{
BYTE cbNextValue=GetCardLogicValue(cbNextData);
BYTE cbFirstValue=GetCardLogicValue(cbFirstData);
if (cbFirstValue!=cbNextValue) return cbFirstValue>cbNextValue;
}
//对比花色
return GetCardColor(cbFirstData)>GetCardColor(cbNextData);
}
case CT_HU_LU: //葫芦
case CT_TIE_ZHI: //铁支
case CT_ONE_DOUBLE: //对子
case CT_TWO_DOUBLE: //两对
case CT_THREE_TIAO: //三条
{
//分析扑克
tagAnalyseResult AnalyseResultNext;
tagAnalyseResult AnalyseResultFirst;
AnalysebCardData(cbNextData,cbCardCount,AnalyseResultNext);
AnalysebCardData(cbFirstData,cbCardCount,AnalyseResultFirst);
//四条数值
if (AnalyseResultFirst.cbFourCount>0)
{
BYTE cbNextValue=AnalyseResultNext.cbFourLogicVolue;
BYTE cbFirstValue=AnalyseResultFirst.cbFourLogicVolue;
return cbFirstValue>cbNextValue;
}
//三条数值
if (AnalyseResultFirst.cbThreeCount>0)
{
BYTE cbNextValue=AnalyseResultNext.cbThreeLogicVolue;
BYTE cbFirstValue=AnalyseResultFirst.cbThreeLogicVolue;
return cbFirstValue>cbNextValue;
}
//对子数值
for (BYTE i=0;i<AnalyseResultFirst.cbDoubleCount;i++)
{
BYTE cbNextValue=AnalyseResultNext.cbDoubleLogicVolue;
BYTE cbFirstValue=AnalyseResultFirst.cbDoubleLogicVolue;
if (cbFirstValue!=cbNextValue) return cbFirstValue>cbNextValue;
}
//散牌数值
for (BYTE i=0;i<AnalyseResultFirst.cbSignedCount;i++)
{
BYTE cbNextValue=AnalyseResultNext.cbSignedLogicVolue;
BYTE cbFirstValue=AnalyseResultFirst.cbSignedLogicVolue;
if (cbFirstValue!=cbNextValue) return cbFirstValue>cbNextValue;
}
//对子花色
if (AnalyseResultFirst.cbDoubleCount>0)
{
BYTE cbNextColor=GetCardColor(AnalyseResultNext.cbDoubleCardData);
BYTE cbFirstColor=GetCardColor(AnalyseResultFirst.cbDoubleCardData);
return cbFirstColor>cbNextColor;
}
//散牌花色
if (AnalyseResultFirst.cbSignedCount>0)
{
BYTE cbNextColor=GetCardColor(AnalyseResultNext.cbSignedCardData);
BYTE cbFirstColor=GetCardColor(AnalyseResultFirst.cbSignedCardData);
return cbFirstColor>cbNextColor;
}
break;
}
case CT_SHUN_ZI: //顺子
case CT_TONG_HUA: //同花
case CT_TONG_HUA_SHUN: //同花顺
{
//数值判断
BYTE cbNextValue=GetCardLogicValue(cbNextData);
BYTE cbFirstValue=GetCardLogicValue(cbFirstData);
#ifdef MAGIC_A_CARD
//特殊列:A J 10 9 8
if( cbFirstType != CT_TONG_HUA )
{
BYTE cbMinusCount = cbCardCount==5?3:4;
if( GetCardValue(cbNextData)==1 &&
GetCardLogicValue(cbNextData)-cbMinusCount == GetCardLogicValue(cbNextData) )
cbNextValue = GetCardLogicValue(cbNextData);
if( GetCardValue(cbFirstData)==1 &&
GetCardLogicValue(cbFirstData)-cbMinusCount == GetCardLogicValue(cbFirstData) )
cbFirstValue = GetCardLogicValue(cbFirstData);
}
#endif
if (cbFirstValue!=cbNextValue) return (cbFirstValue>cbNextValue);
//花色判断
BYTE cbNextColor=GetCardColor(cbNextData);
BYTE cbFirstColor=GetCardColor(cbFirstData);
return (cbFirstColor>cbNextColor);
}
}
return false;
}
//分析扑克
void CGameLogic::AnalysebCardData(const BYTE cbCardData[], BYTE cbCardCount, tagAnalyseResult & AnalyseResult)
{
//设置结果
ZeroMemory(&AnalyseResult,sizeof(AnalyseResult));
//扑克分析
for (BYTE i=0;i<cbCardCount;i++)
{
//变量定义
BYTE cbSameCount=1;
BYTE cbSameCardData={cbCardData,0,0,0};
BYTE cbLogicValue=GetCardLogicValue(cbCardData);
//获取同牌
for (int j=i+1;j<cbCardCount;j++)
{
//逻辑对比
if (GetCardLogicValue(cbCardData)!=cbLogicValue) break;
//设置扑克
cbSameCardData=cbCardData;
}
//保存结果
switch (cbSameCount)
{
case 1: //单张
{
AnalyseResult.cbSignedLogicVolue=cbLogicValue;
CopyMemory(&AnalyseResult.cbSignedCardData[(AnalyseResult.cbSignedCount++)*cbSameCount],cbSameCardData,cbSameCount);
break;
}
case 2: //两张
{
AnalyseResult.cbDoubleLogicVolue=cbLogicValue;
CopyMemory(&AnalyseResult.cbDoubleCardData[(AnalyseResult.cbDoubleCount++)*cbSameCount],cbSameCardData,cbSameCount);
break;
}
case 3: //三张
{
AnalyseResult.cbThreeLogicVolue=cbLogicValue;
CopyMemory(&AnalyseResult.cbThreeCardData[(AnalyseResult.cbThreeCount++)*cbSameCount],cbSameCardData,cbSameCount);
break;
}
case 4: //四张
{
AnalyseResult.cbFourLogicVolue=cbLogicValue;
CopyMemory(&AnalyseResult.cbFourCardData[(AnalyseResult.cbFourCount++)*cbSameCount],cbSameCardData,cbSameCount);
break;
}
}
//设置递增
i+=cbSameCount-1;
}
return;
}
//////////////////////////////////////////////////////////////////////////
页:
[1]