320 lines
8.2 KiB
C++
320 lines
8.2 KiB
C++
|
|
#include "StdAfx.h"
|
|||
|
|
#include "GameLogic.h"
|
|||
|
|
|
|||
|
|
BYTE CGameLogic::m_cbCardList[FULL_COUNT] = {
|
|||
|
|
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D, //<2F><><EFBFBD><EFBFBD>
|
|||
|
|
0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D, //÷<><C3B7>
|
|||
|
|
0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D, //<2F><><EFBFBD><EFBFBD>
|
|||
|
|
0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D, //<2F><><EFBFBD><EFBFBD>
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
bool CGameLogic::IsValidCard(BYTE cbCard)
|
|||
|
|
{
|
|||
|
|
const BYTE cbColor = GetCardColor(cbCard);
|
|||
|
|
const BYTE cbValue = GetCardValue(cbCard);
|
|||
|
|
return (cbColor<=3 && cbValue>=0x01 && cbValue<=0x0D) ? true : false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
BYTE CGameLogic::GetLogicValue(BYTE cbCard)
|
|||
|
|
{
|
|||
|
|
ASSERT(IsValidCard(cbCard));
|
|||
|
|
const BYTE cbValue = GetCardValue(cbCard);
|
|||
|
|
return (cbValue>10) ? 10 : cbValue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void CGameLogic::SortCardList(BYTE cbCardData[], BYTE cbCardCount)
|
|||
|
|
{
|
|||
|
|
ASSERT((MAX_COUNT-2)==cbCardCount || MAX_COUNT==cbCardCount);
|
|||
|
|
if ((MAX_COUNT-2)!=cbCardCount && MAX_COUNT!=cbCardCount) return;
|
|||
|
|
|
|||
|
|
BYTE cbCardValue[MAX_COUNT] = { 0 };
|
|||
|
|
for (BYTE i=0; i<cbCardCount; ++i)
|
|||
|
|
{
|
|||
|
|
cbCardValue[i] = GetCardValue(cbCardData[i]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool bSorted = true;
|
|||
|
|
BYTE cbTempData = 0;
|
|||
|
|
BYTE cbLast = cbCardCount - 1;
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
bSorted = true;
|
|||
|
|
for (BYTE i=0; i<cbLast; ++i)
|
|||
|
|
{
|
|||
|
|
if ((cbCardValue[i]<cbCardValue[i+1]) || (cbCardValue[i]==cbCardValue[i+1]&&cbCardData[i]<cbCardData[i+1]))
|
|||
|
|
{
|
|||
|
|
cbTempData = cbCardData[i];
|
|||
|
|
cbCardData[i] = cbCardData[i+1];
|
|||
|
|
cbCardData[i+1] = cbTempData;
|
|||
|
|
cbTempData = cbCardValue[i];
|
|||
|
|
cbCardValue[i] = cbCardValue[i+1];
|
|||
|
|
cbCardValue[i+1] = cbTempData;
|
|||
|
|
bSorted = false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
cbLast--;
|
|||
|
|
} while (!bSorted);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void CGameLogic::RandCardList(BYTE cbCardData[], BYTE cbCardCount, bool bNoHuaCard)
|
|||
|
|
{
|
|||
|
|
BYTE cbRemoveCount = 0;
|
|||
|
|
const BYTE cbFullCount = CountArray(m_cbCardList);
|
|||
|
|
BYTE cbTempList[cbFullCount] = { 0 };
|
|||
|
|
CopyMemory(cbTempList, m_cbCardList, sizeof(m_cbCardList));
|
|||
|
|
ZeroMemory(m_cbRepertoryCard, sizeof(m_cbRepertoryCard));
|
|||
|
|
|
|||
|
|
BYTE cbRandCount = 0, cbPosition = 0;
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
cbPosition = rand() % (cbFullCount - cbRandCount);
|
|||
|
|
m_cbRepertoryCard[cbRandCount++] = cbTempList[cbPosition];
|
|||
|
|
cbTempList[cbPosition] = cbTempList[cbFullCount - cbRandCount];
|
|||
|
|
} while (cbRandCount < cbFullCount);
|
|||
|
|
|
|||
|
|
if (bNoHuaCard)
|
|||
|
|
{
|
|||
|
|
cbRemoveCount = 12;
|
|||
|
|
BYTE cbHuaCard[12] = { 0x0B, 0x1B, 0x2B, 0x3B, 0x0C, 0x1C, 0x2C, 0x3C, 0x0D, 0x1D, 0x2D, 0x3D };
|
|||
|
|
RemoveCard(m_cbRepertoryCard, cbFullCount, cbHuaCard, cbRemoveCount);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (BYTE i = 0; i < cbCardCount; ++i)
|
|||
|
|
{
|
|||
|
|
cbCardData[i] = m_cbRepertoryCard[cbFullCount - cbRemoveCount - i - 1];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool CGameLogic::RemoveCard(BYTE cbCardData[], BYTE cbCardCount, BYTE cbRemoveCard[], BYTE cbRemoveCount)
|
|||
|
|
{
|
|||
|
|
ASSERT(cbRemoveCount <= cbCardCount);
|
|||
|
|
if (cbRemoveCount > cbCardCount) return false;
|
|||
|
|
|
|||
|
|
BYTE cbDeleteCount = 0, cbDeleteCard[FULL_COUNT] = { 0 };
|
|||
|
|
ASSERT(cbCardCount <= sizeof(cbDeleteCard) / sizeof(cbDeleteCard[0]));
|
|||
|
|
if (cbCardCount > sizeof(cbDeleteCard) / sizeof(cbDeleteCard[0])) return false;
|
|||
|
|
CopyMemory(cbDeleteCard, cbCardData, sizeof(cbCardData[0])*cbCardCount);
|
|||
|
|
|
|||
|
|
for (BYTE i = 0; i < cbRemoveCount; ++i)
|
|||
|
|
{
|
|||
|
|
for (BYTE j = 0; j < cbCardCount; ++j)
|
|||
|
|
{
|
|||
|
|
if (cbRemoveCard[i] == cbDeleteCard[j])
|
|||
|
|
{
|
|||
|
|
cbDeleteCount++;
|
|||
|
|
cbDeleteCard[j] = 0;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
ASSERT(cbDeleteCount == cbRemoveCount);
|
|||
|
|
if (cbDeleteCount != cbRemoveCount) return false;
|
|||
|
|
|
|||
|
|
BYTE cbCardPos = 0;
|
|||
|
|
for (BYTE i = 0; i < cbCardCount; ++i)
|
|||
|
|
{
|
|||
|
|
if (0 != cbDeleteCard[i])
|
|||
|
|
{
|
|||
|
|
cbCardData[cbCardPos++] = cbDeleteCard[i];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
BYTE CGameLogic::GetCardType(const BYTE cbCardData[], const BYTE cbCardCount, BYTE cbSortCard[], bool IsHasWHN, bool IsHasBomb, bool IsHasWXN)
|
|||
|
|
{
|
|||
|
|
ASSERT((MAX_COUNT-2)==cbCardCount || MAX_COUNT==cbCardCount);
|
|||
|
|
|
|||
|
|
BYTE cbTempData[MAX_COUNT] = { 0 };
|
|||
|
|
CopyMemory(cbTempData, cbCardData, sizeof(BYTE)*cbCardCount);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ;
|
|||
|
|
//if (IsSpeical)
|
|||
|
|
{
|
|||
|
|
if (IsHasWHN && (MAX_COUNT == cbCardCount) && IsWuHuaNiu(cbTempData, cbCardCount))
|
|||
|
|
{
|
|||
|
|
if (nullptr != cbSortCard)
|
|||
|
|
{
|
|||
|
|
CopyMemory(cbSortCard, cbCardData, sizeof(BYTE)*cbCardCount);
|
|||
|
|
}
|
|||
|
|
return NIU_TYPE_WHN;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (IsHasWXN && (MAX_COUNT == cbCardCount) && IsWuXiaoNiu(cbTempData, cbCardCount))
|
|||
|
|
{
|
|||
|
|
if (nullptr != cbSortCard)
|
|||
|
|
{
|
|||
|
|
CopyMemory(cbSortCard, cbCardData, sizeof(BYTE)*cbCardCount);
|
|||
|
|
}
|
|||
|
|
return NIU_TYPE_WXN;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (IsHasBomb && (MAX_COUNT == cbCardCount) && IsZhaDan(cbTempData, cbCardCount))
|
|||
|
|
{
|
|||
|
|
if (nullptr != cbSortCard)
|
|||
|
|
{
|
|||
|
|
CopyMemory(cbSortCard, cbCardData, sizeof(BYTE)*cbCardCount);
|
|||
|
|
}
|
|||
|
|
return NIU_TYPE_SHN;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
BYTE cbValueSum = 0;
|
|||
|
|
BYTE cbLogicValue[MAX_COUNT] = { 0 };
|
|||
|
|
for (BYTE i=0; i<cbCardCount; ++i)
|
|||
|
|
{
|
|||
|
|
cbLogicValue[i] = GetLogicValue(cbTempData[i]);
|
|||
|
|
cbValueSum += cbLogicValue[i];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ((MAX_COUNT-2) == cbCardCount)
|
|||
|
|
{
|
|||
|
|
for (BYTE i=0; i<cbCardCount; ++i)
|
|||
|
|
{
|
|||
|
|
if (0 == (cbValueSum-cbLogicValue[i])%10)
|
|||
|
|
{
|
|||
|
|
if (nullptr != cbSortCard)
|
|||
|
|
{
|
|||
|
|
CopyMemory(cbSortCard, cbTempData, sizeof(BYTE)*cbCardCount);
|
|||
|
|
VERIFY_RETURN(RemoveCard(cbSortCard,cbCardCount,&cbTempData[i],1),0);
|
|||
|
|
cbSortCard[cbCardCount - 1] = cbTempData[i];
|
|||
|
|
}
|
|||
|
|
return cbLogicValue[i];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
for (BYTE i=0; i<cbCardCount-1; ++i)
|
|||
|
|
{
|
|||
|
|
for (BYTE j=i+1; j<cbCardCount; ++j)
|
|||
|
|
{
|
|||
|
|
BYTE cbCowValue = cbLogicValue[i] + cbLogicValue[j];
|
|||
|
|
if (0 == (cbValueSum-cbCowValue)%10)
|
|||
|
|
{
|
|||
|
|
if (nullptr != cbSortCard)
|
|||
|
|
{
|
|||
|
|
BYTE cbDeleteCard[2] = { cbTempData[i],cbTempData[j] };
|
|||
|
|
CopyMemory(cbSortCard, cbTempData, sizeof(BYTE)*cbCardCount);
|
|||
|
|
VERIFY_RETURN(RemoveCard(cbSortCard,cbCardCount,cbDeleteCard,2),0);
|
|||
|
|
cbSortCard[cbCardCount-2] = cbTempData[i];
|
|||
|
|
cbSortCard[cbCardCount-1] = cbTempData[j];
|
|||
|
|
}
|
|||
|
|
return (cbCowValue>10) ? cbCowValue-10 : cbCowValue;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (nullptr != cbSortCard)
|
|||
|
|
{
|
|||
|
|
CopyMemory(cbSortCard, cbCardData, sizeof(BYTE)*cbCardCount);
|
|||
|
|
}
|
|||
|
|
return NIU_TYPE_0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
BYTE CGameLogic::GetWinNiuFan(const BYTE cbCardType)
|
|||
|
|
{
|
|||
|
|
switch (cbCardType)
|
|||
|
|
{
|
|||
|
|
case NIU_TYPE_WXN:
|
|||
|
|
return 6;
|
|||
|
|
case NIU_TYPE_SHN:
|
|||
|
|
return 5;
|
|||
|
|
case NIU_TYPE_WHN:
|
|||
|
|
return 4;
|
|||
|
|
case NIU_TYPE_10:
|
|||
|
|
return 3;
|
|||
|
|
case NIU_TYPE_9:
|
|||
|
|
case NIU_TYPE_8:
|
|||
|
|
case NIU_TYPE_7:
|
|||
|
|
return 2;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
ASSERT(cbCardType >= NIU_TYPE_0 && cbCardType < NIU_TYPE_7);
|
|||
|
|
return 1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
BYTE CGameLogic::GetMaxCard(const BYTE cbCardData[], const BYTE cbCardCount)
|
|||
|
|
{
|
|||
|
|
BYTE cbTempData[MAX_COUNT] = { 0 };
|
|||
|
|
CopyMemory(cbTempData, cbCardData, sizeof(BYTE)*cbCardCount);
|
|||
|
|
SortCardList(cbTempData, cbCardCount);
|
|||
|
|
return cbTempData[0];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool CGameLogic::CompareCard(const tagOpenCardRlt stFirstData, const tagOpenCardRlt stNextData)
|
|||
|
|
{
|
|||
|
|
if (stFirstData.cbCardType != stNextData.cbCardType)
|
|||
|
|
{
|
|||
|
|
return (stFirstData.cbCardType > stNextData.cbCardType) ? true : false;
|
|||
|
|
}
|
|||
|
|
BYTE cbFirstValue = GetCardValue(stFirstData.cbMaxCard);
|
|||
|
|
BYTE cbNextValue = GetCardValue(stNextData.cbMaxCard);
|
|||
|
|
if (cbFirstValue != cbNextValue)
|
|||
|
|
{
|
|||
|
|
return (cbFirstValue > cbNextValue) ? true : false;
|
|||
|
|
}
|
|||
|
|
BYTE cbFirstColor = GetCardColor(stFirstData.cbMaxCard);
|
|||
|
|
BYTE cbNextColor = GetCardColor(stNextData.cbMaxCard);
|
|||
|
|
return (cbFirstColor > cbNextColor) ? true : false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool CGameLogic::IsWuHuaNiu(const BYTE cbCardData[], const BYTE cbCardCount)
|
|||
|
|
{
|
|||
|
|
ASSERT(MAX_COUNT==cbCardCount || (MAX_COUNT-2)==cbCardCount);
|
|||
|
|
if (MAX_COUNT!=cbCardCount && (MAX_COUNT-2)!=cbCardCount) return false;
|
|||
|
|
|
|||
|
|
for (BYTE i=0; i<cbCardCount; ++i)
|
|||
|
|
{
|
|||
|
|
BYTE cbCardValue = GetCardValue(cbCardData[i]);
|
|||
|
|
if (cbCardValue < 11) return false;
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool CGameLogic::IsWuXiaoNiu(const BYTE cbCardData[], const BYTE cbCardCount)
|
|||
|
|
{
|
|||
|
|
ASSERT(MAX_COUNT==cbCardCount || (MAX_COUNT-2)==cbCardCount);
|
|||
|
|
if (MAX_COUNT!=cbCardCount && (MAX_COUNT-2)!=cbCardCount) return false;
|
|||
|
|
|
|||
|
|
BYTE cbValueSum = 0;
|
|||
|
|
for (BYTE i=0; i<cbCardCount; ++i)
|
|||
|
|
{
|
|||
|
|
BYTE cbCardValue = GetCardValue(cbCardData[i]);
|
|||
|
|
if (cbCardValue >= 5)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
cbValueSum += cbCardValue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return (cbValueSum<=10) ? true : false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>ը<EFBFBD><D5A8>;
|
|||
|
|
bool CGameLogic::IsZhaDan(const BYTE cbCardData[], const BYTE cbCardCount)
|
|||
|
|
{
|
|||
|
|
ASSERT(MAX_COUNT == cbCardCount || (MAX_COUNT - 2) == cbCardCount);
|
|||
|
|
if (MAX_COUNT != cbCardCount && (MAX_COUNT - 2) != cbCardCount) return false;
|
|||
|
|
|
|||
|
|
BYTE aryCardCount[14] = {0};
|
|||
|
|
|
|||
|
|
for (BYTE i = 0; i < cbCardCount; ++i)
|
|||
|
|
{
|
|||
|
|
BYTE cbCardValue = GetCardValue(cbCardData[i]);
|
|||
|
|
aryCardCount[cbCardValue]++;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (BYTE j = 0; j < 14; j++)
|
|||
|
|
{
|
|||
|
|
if (4 == aryCardCount[j])
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return false;
|
|||
|
|
}
|