#include "WG_Math.h"
using namespace cocos2d;
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#include "iconv/iconv.h" //iconv.lib
int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode)
{
iconv_t iconvH;
iconvH = iconv_open(formCode,toCode);
if(iconvH == 0)
{ return -1; }
const char* strChar = gbkStr.c_str();
const char** pin = &strChar;
size_t strLength = gbkStr.length();
char* outbuf = (char*)malloc(strLength*4);
char* pBuff = outbuf; memset(outbuf,0,strLength*4);
size_t outLength = strLength*4;
if(-1 == iconv(iconvH,pin,&strLength,&outbuf,&outLength))
{ iconv_close(iconvH); return -1; }
gbkStr = pBuff; iconv_close(iconvH); return 0;
}
#endif
/** **在封装一层,直接传入一个string,转换后还回对应的编码给你 */
const char* WG_Math::GBKToUTF(std::string &gbkStr)
{
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(gbkStr,"gbk","utf-8");
#endif
return gbkStr.c_str();
}
cocos2d::CCArray* WG_Math::getCardNum()
{
CCArray * array = CCArray::create();
for (int i = 0; i < 4; i++) {
for (int j = C_2; j< C_A+1; j++) {
WG_Card * card = new WG_Card(j,i);
array->addObject(card);
// card->autorelease();
// delete card;
}
}
return array;
}
/**
获得牌型
@param ary 排序数组 从小到大 存储WG_Card类型的数据
@returns WG_Handpattern
*/
WG_Handpattern * WG_Math::getHandpatterns(cocos2d::CCArray* ary)
{
WG_Handpattern * handPattern = new WG_Handpattern();
handPattern->handpattern = -1;
//同花顺或者皇家同花顺或者顺子
if(true)
{
for (int i = 0; i < ary->count()-4; i++) {
bool isHandA = true;
bool isHandB = true;
WG_Card *card =(WG_Card *) ary->objectAtIndex(i);
//特殊顺子 A2345
if (i == 0 && card->cardnum == C_2) {
WG_Card *cardb = (WG_Card *)ary->lastObject();
if (cardb->cardnum == C_A) {
for (int j = i+1; j < i+4; j++) {
WG_Card * cardB = (WG_Card *) ary->objectAtIndex(j);
isHandA = isHandA && (cardB->cardnum == (card->cardnum+1)) && (cardB->cardsuit == card->cardsuit);
isHandB = isHandB && (cardB->cardnum == (card->cardnum+1));
if (!isHandB) {
break;
}
card = cardB;
}
if (isHandB) {
CCArray * aryBuf = WG_Math::copyArray(ary);
CCArray * aryHand = CCArray::create();
aryHand->addObject(WG_Card::getCard(C_1, cardb->cardsuit));
aryBuf->removeObject(cardb);
for (int k = i; k < i+4; k++) {
aryHand->addObject(ary->objectAtIndex(k));
aryBuf->removeObject(ary->objectAtIndex(k));
}
aryHand->addObjectsFromArray(aryBuf);
if (isHandA) {
handPattern->handpatterns = aryHand;
handPattern->handpattern = HAND_STRAIGHT_FLUSH;
}
else
{
handPattern->handpatterns = aryHand;
handPattern->handpattern = HAND_STRAIGHT;
}
}
}
}
card =(WG_Card *) ary->objectAtIndex(i);
for (int j = i+1; j < i+5; j++) {
WG_Card * cardB = (WG_Card *) ary->objectAtIndex(j);
isHandA = isHandA && (cardB->cardnum == (card->cardnum+1)) && (cardB->cardsuit == card->cardsuit);
isHandB = isHandB && (cardB->cardnum == (card->cardnum+1));
if (!isHandB) {
break;
}
card = cardB;
}
if (isHandB) {
CCArray * aryHand = CCArray::create();
CCArray * aryBuf = WG_Math::copyArray(ary);
for (int k = i; k < i+5; k++) {
aryHand->addObject(ary->objectAtIndex(k));
aryBuf->removeObject(ary->objectAtIndex(k));
}
aryHand->addObjectsFromArray(aryBuf);
if (isHandA) {
WG_Card * card3 = (WG_Card *) ary->objectAtIndex(i);
if (card3->cardnum == C_10) {
handPattern->handpatterns = aryHand;
handPattern->handpattern = HAND_ROYAL_FLUSH;
}
else{
handPattern->handpatterns = aryHand;
handPattern->handpattern = HAND_STRAIGHT_FLUSH;
}
}
else
{
handPattern->handpatterns = aryHand;
handPattern->handpattern = HAND_STRAIGHT;
}
}
}
if (handPattern->handpattern != -1) {
return handPattern;
}
}
//四条
if(true)
{
for (int i =0; i <(int)ary->count()-4; i++) {
bool isHand = true;
WG_Card *card =(WG_Card *) ary->objectAtIndex(i);
for (int j = i+1; j < i+5 ; j++) {
WG_Card * cardB = (WG_Card *) ary->objectAtIndex(j);
isHand = isHand && (card->cardnum == cardB->cardnum);
if (!isHand) {
break;
}
}
if (isHand) {
CCArray *aryHand = CCArray::create();
CCArray * aryBuf = WG_Math::copyArray(ary);
for (int m = i; m < i + 4; m++) {
aryHand->addObject(ary->objectAtIndex(m));
aryBuf ->removeObject(ary->objectAtIndex(m));
}
aryHand->addObjectsFromArray(aryBuf);
handPattern->handpatterns = aryHand;
handPattern->handpattern = HAND_FOUR_OF_A_KIND;
return handPattern;
}
}
}
//三条与葫芦与一对与两对
if(true)
{
CCArray * aryBuf = WG_Math::copyArray(ary);
CCArray * aryHand = CCArray::create();
CCArray * aryThree = CCArray::create();
CCArray * aryTwo = CCArray::create();
for (int i = 0; i<(int) ary->count()-2; i++) {
bool isHand= true;
CCArray * aryd = CCArray::create();
WG_Card *card =(WG_Card *) ary->objectAtIndex(i);
aryd->addObject(card);
for (int j = i+1; j < i+3 ; j++) {
WG_Card * cardB = (WG_Card *) ary->objectAtIndex(j);
isHand = isHand && (card->cardnum == cardB->cardnum);
if (!isHand) {
break;
}
aryd->addObject(cardB);
}
if (isHand) {
aryThree->addObjectsFromArray(aryd);
}
}
if (aryThree->count() == 6) {
for (int a = (int)aryThree->count()-1; a > 0; a--) {
aryHand->addObject(aryThree->objectAtIndex(a));
aryBuf->removeObject(aryThree->objectAtIndex(a));
}
aryHand->addObjectsFromArray(aryBuf);
handPattern->handpatterns = aryHand;
handPattern->handpattern = HAND_FULL_HOUSE;
return handPattern;
}
f
- 1
- 2
前往页