斗地主【源码】
http://img.woaidaima.com//upload/image/20170517/1494984093530035796.jpghttp://img.woaidaima.com//upload/image/20170517/1494984094742043425.jpg#include "StdAfx.h"#include "GameLogic.h"
//////////////////////////////////////////////////////////////////////////////////
//静态变量
//索引变量
const BYTE cbIndexCount=5;
//扑克数据
const BYTE CGameLogic::m_cbCardData=
{
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D, //方块 A - K
0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D, //梅花 A - K
0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D, //红桃 A - K
0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D, //黑桃 A - K
0x4E,0x4F,
};
//////////////////////////////////////////////////////////////////////////////////
//构造函数
CGameLogic::CGameLogic()
{
}
//析构函数
CGameLogic::~CGameLogic()
{
}
//获取类型
BYTE CGameLogic::GetCardType(const BYTE cbCardData[], BYTE cbCardCount)
{
//简单牌型
switch (cbCardCount)
{
case 0: //空牌
{
return CT_ERROR;
}
case 1: //单牌
{
return CT_SINGLE;
}
case 2: //对牌火箭
{
//牌型判断
if ((cbCardData==0x4F)&&(cbCardData==0x4E)) return CT_MISSILE_CARD;
if (GetCardLogicValue(cbCardData)==GetCardLogicValue(cbCardData)) return CT_DOUBLE;
return CT_ERROR;
}
}
//分析扑克
tagAnalyseResult AnalyseResult;
AnalysebCardData(cbCardData,cbCardCount,AnalyseResult);
//四牌判断
if (AnalyseResult.cbBlockCount>0)
{
//牌型判断
if ((AnalyseResult.cbBlockCount==1)&&(cbCardCount==4)) return CT_BOMB_CARD;
if ((AnalyseResult.cbBlockCount==1)&&(cbCardCount==6)) return CT_FOUR_TAKE_ONE;
if ((AnalyseResult.cbBlockCount==1)&&(cbCardCount==8)&&(AnalyseResult.cbBlockCount==2)) return CT_FOUR_TAKE_TWO;
return CT_ERROR;
}
//三牌判断
if (AnalyseResult.cbBlockCount>0)
{
//连牌判断
if (AnalyseResult.cbBlockCount>1)
{
//变量定义
BYTE cbCardData=AnalyseResult.cbCardData;
BYTE cbFirstLogicValue=GetCardLogicValue(cbCardData);
//错误过虑
if (cbFirstLogicValue>=15) return CT_ERROR;
//连牌判断
for (BYTE i=1;i<AnalyseResult.cbBlockCount;i++)
{
BYTE cbCardData=AnalyseResult.cbCardData;
if (cbFirstLogicValue!=(GetCardLogicValue(cbCardData)+i)) return CT_ERROR;
}
}
else if( cbCardCount == 3 ) return CT_THREE;
//牌形判断
if (AnalyseResult.cbBlockCount*3==cbCardCount) return CT_THREE_LINE;
if (AnalyseResult.cbBlockCount*4==cbCardCount) return CT_THREE_TAKE_ONE;
if ((AnalyseResult.cbBlockCount*5==cbCardCount)&&(AnalyseResult.cbBlockCount==AnalyseResult.cbBlockCount)) return CT_THREE_TAKE_TWO;
return CT_ERROR;
}
//两张类型
if (AnalyseResult.cbBlockCount>=3)
{
//变量定义
BYTE cbCardData=AnalyseResult.cbCardData;
BYTE cbFirstLogicValue=GetCardLogicValue(cbCardData);
//错误过虑
if (cbFirstLogicValue>=15) return CT_ERROR;
//连牌判断
for (BYTE i=1;i<AnalyseResult.cbBlockCount;i++)
{
BYTE cbCardData=AnalyseResult.cbCardData;
if (cbFirstLogicValue!=(GetCardLogicValue(cbCardData)+i)) return CT_ERROR;
}
//二连判断
if ((AnalyseResult.cbBlockCount*2)==cbCardCount) return CT_DOUBLE_LINE;
return CT_ERROR;
}
//单张判断
if ((AnalyseResult.cbBlockCount>=5)&&(AnalyseResult.cbBlockCount==cbCardCount))
{
//变量定义
BYTE cbCardData=AnalyseResult.cbCardData;
BYTE cbFirstLogicValue=GetCardLogicValue(cbCardData);
//错误过虑
if (cbFirstLogicValue>=15) return CT_ERROR;
//连牌判断
for (BYTE i=1;i<AnalyseResult.cbBlockCount;i++)
{
BYTE cbCardData=AnalyseResult.cbCardData;
if (cbFirstLogicValue!=(GetCardLogicValue(cbCardData)+i)) return CT_ERROR;
}
return CT_SINGLE_LINE;
}
return CT_ERROR;
}
//排列扑克
VOID CGameLogic::SortCardList(BYTE cbCardData[], BYTE cbCardCount, BYTE cbSortType)
{
//数目过虑
if (cbCardCount==0) return;
if (cbSortType==ST_CUSTOM) return;
//转换数值
BYTE cbSortValue;
for (BYTE i=0;i<cbCardCount;i++) cbSortValue=GetCardLogicValue(cbCardData);
//排序操作
bool bSorted=true;
BYTE cbSwitchData=0,cbLast=cbCardCount-1;
do
{
bSorted=true;
for (BYTE i=0;i<cbLast;i++)
{
if ((cbSortValue<cbSortValue)||
((cbSortValue==cbSortValue)&&(cbCardData<cbCardData)))
{
//设置标志
bSorted=false;
//扑克数据
cbSwitchData=cbCardData;
cbCardData=cbCardData;
cbCardData=cbSwitchData;
//排序权位
cbSwitchData=cbSortValue;
cbSortValue=cbSortValue;
cbSortValue=cbSwitchData;
}
}
cbLast--;
} while(bSorted==false);
//数目排序
if (cbSortType==ST_COUNT)
{
//变量定义
BYTE cbCardIndex=0;
//分析扑克
tagAnalyseResult AnalyseResult;
AnalysebCardData(&cbCardData,cbCardCount-cbCardIndex,AnalyseResult);
//提取扑克
for (BYTE i=0;i<CountArray(AnalyseResult.cbBlockCount);i++)
{
//拷贝扑克
BYTE cbIndex=CountArray(AnalyseResult.cbBlockCount)-i-1;
CopyMemory(&cbCardData,AnalyseResult.cbCardData,AnalyseResult.cbBlockCount*(cbIndex+1)*sizeof(BYTE));
//设置索引
cbCardIndex+=AnalyseResult.cbBlockCount*(cbIndex+1)*sizeof(BYTE);
}
}
return;
}
//混乱扑克
VOID CGameLogic::RandCardList(BYTE cbCardBuffer[], BYTE cbBufferCount)
{
//混乱准备
BYTE cbCardData;
CopyMemory(cbCardData,m_cbCardData,sizeof(m_cbCardData));
//混乱扑克
BYTE cbRandCount=0,cbPosition=0;
do
{
cbPosition=rand()%(cbBufferCount-cbRandCount);
cbCardBuffer=cbCardData;
cbCardData=cbCardData;
} while (cbRandCount<cbBufferCount);
return;
}
//删除扑克
bool CGameLogic::RemoveCardList(const BYTE cbRemoveCard[], BYTE cbRemoveCount, BYTE cbCardData[], BYTE cbCardCount)
{
//检验数据
ASSERT(cbRemoveCount<=cbCardCount);
//定义变量
BYTE cbDeleteCount=0,cbTempCardData;
if (cbCardCount>CountArray(cbTempCardData)) return false;
CopyMemory(cbTempCardData,cbCardData,cbCardCount*sizeof(cbCardData));
//置零扑克
for (BYTE i=0;i<cbRemoveCount;i++)
{
for (BYTE j=0;j<cbCardCount;j++)
{
if (cbRemoveCard==cbTempCardData)
{
cbDeleteCount++;
cbTempCardData=0;
break;
}
}
}
if (cbDeleteCount!=cbRemoveCount) return false;
//清理扑克
BYTE cbCardPos=0;
for (BYTE i=0;i<cbCardCount;i++)
{
if (cbTempCardData!=0) cbCardData=cbTempCardData;
}
return true;
}
//逻辑数值
BYTE CGameLogic::GetCardLogicValue(BYTE cbCardData)
{
//扑克属性
BYTE cbCardColor=GetCardColor(cbCardData);
BYTE cbCardValue=GetCardValue(cbCardData);
//转换数值
if (cbCardColor==0x40) return cbCardValue+2;
return (cbCardValue<=2)?(cbCardValue+13):cbCardValue;
}
//对比扑克
bool CGameLogic::CompareCard(const BYTE cbFirstCard[], const BYTE cbNextCard[], BYTE cbFirstCount, BYTE cbNextCount)
{
//获取类型
BYTE cbNextType=GetCardType(cbNextCard,cbNextCount);
BYTE cbFirstType=GetCardType(cbFirstCard,cbFirstCount);
//类型判断
if (cbNextType==CT_ERROR) return false;
if (cbNextType==CT_MISSILE_CARD) return true;
//炸弹判断
if ((cbFirstType!=CT_BOMB_CARD)&&(cbNextType==CT_BOMB_CARD)) return true;
if ((cbFirstType==CT_BOMB_CARD)&&(cbNextType!=CT_BOMB_CARD)) return false;
//规则判断
if ((cbFirstType!=cbNextType)||(cbFirstCount!=cbNextCount)) return false;
//开始对比
switch (cbNextType)
{
case CT_SINGLE:
case CT_DOUBLE:
case CT_THREE:
case CT_SINGLE_LINE:
case CT_DOUBLE_LINE:
case CT_THREE_LINE:
case CT_BOMB_CARD:
{
//获取数值
BYTE cbNextLogicValue=GetCardLogicValue(cbNextCard);
BYTE cbFirstLogicValue=GetCardLogicValue(cbFirstCard);
//对比扑克
return cbNextLogicValue>cbFirstLogicValue;
}
case CT_THREE_TAKE_ONE:
case CT_THREE_TAKE_TWO:
{
//分析扑克
tagAnalyseResult NextResult;
tagAnalyseResult FirstResult;
AnalysebCardData(cbNextCard,cbNextCount,NextResult);
AnalysebCardData(cbFirstCard,cbFirstCount,FirstResult);
//获取数值
BYTE cbNextLogicValue=GetCardLogicValue(NextResult.cbCardData);
BYTE cbFirstLogicValue=GetCardLogicValue(FirstResult.cbCardData);
//对比扑克
return cbNextLogicValue>cbFirstLogicValue;
}
case CT_FOUR_TAKE_ONE:
case CT_FOUR_TAKE_TWO:
{
//分析扑克
tagAnalyseResult NextResult;
tagAnalyseResult FirstResult;
AnalysebCardData(cbNextCard,cbNextCount,NextResult);
AnalysebCardData(cbFirstCard,cbFirstCount,FirstResult);
//获取数值
BYTE cbNextLogicValue=GetCardLogicValue(NextResult.cbCardData);
BYTE cbFirstLogicValue=GetCardLogicValue(FirstResult.cbCardData);
//对比扑克
return cbNextLogicValue>cbFirstLogicValue;
}
}
return false;
}
//构造扑克
BYTE CGameLogic::MakeCardData(BYTE cbValueIndex, BYTE cbColorIndex)
{
return (cbColorIndex<<4)|(cbValueIndex+1);
}
//分析扑克
VOID CGameLogic::AnalysebCardData(const BYTE cbCardData[], BYTE cbCardCount, tagAnalyseResult & AnalyseResult)
{
//设置结果
ZeroMemory(&AnalyseResult,sizeof(AnalyseResult));
//扑克分析
for (BYTE i=0;i<cbCardCount;i++)
{
//变量定义
BYTE cbSameCount=1,cbCardValueTemp=0;
BYTE cbLogicValue=GetCardLogicValue(cbCardData);
//搜索同牌
for (BYTE j=i+1;j<cbCardCount;j++)
{
//获取扑克
if (GetCardLogicValue(cbCardData)!=cbLogicValue) break;
//设置变量
cbSameCount++;
}
//设置结果
BYTE cbIndex=AnalyseResult.cbBlockCount++;
for (BYTE j=0;j<cbSameCount;j++) AnalyseResult.cbCardData=cbCardData;
//设置索引
i+=cbSameCount-1;
}
return;
}
//分析分布
VOID CGameLogic::AnalysebDistributing(const BYTE cbCardData[], BYTE cbCardCount, tagDistributing & Distributing)
{
//设置变量
ZeroMemory(&Distributing,sizeof(Distributing));
//设置变量
for (BYTE i=0;i<cbCardCount;i++)
{
if (cbCardData==0) continue;
//获取属性
BYTE cbCardColor=GetCardColor(cbCardData);
BYTE cbCardValue=GetCardValue(cbCardData);
//分布信息
Distributing.cbCardCount++;
Distributing.cbDistributing++;
Distributing.cbDistributing++;
}
return;
}
//////////////////////////////////////////////////////////////////////////////////
页:
[1]