自己寫的帶混牌麻將算法
// MJdaihun.cpp : 定義控制臺應用程序的入口點。
//
#include "stdafx.h"
#include <iostream>
#include <windows.h>
int iAllPai[4][10] = {
{6,3,0,1,1,1,0,0,0,0},
{3,3,0,0,0,0,0,0,0,0},
{3,0,0,0,1,0,1,1,0,0},
{3,0,1,1,1,0,0,0,0,0}
};
int iImagicPai[2] = { 0 };
int iImagicPaiNum = 2;
bool AnalyzePai(int aKindPai[], bool bIsZi, bool bHasJiang);
bool IsHu(int iAllPaiTemp[4][10]);
bool IsHu(int iAllPaiTemp[4][10])
{
bool bHasJiang = false;
for (int i = 0; i < 4; i++)
{
if (!AnalyzePai(iAllPaiTemp[i], i == 3, bHasJiang))
return false;
}
return true;
}
bool AnalyzePai(int aKindPai[], bool bIsZi,bool bHasJiang)
{
bool bResult = false;
if (aKindPai[0] == 0){
if (bHasJiang){
return true;
}
else
{
if (iImagicPaiNum % 3 == 2)
{
return true;
}
}
}
for (int j = 1; j < 10; j++){
if (bIsZi)
{
if (aKindPai[j] > 0)
{
if (bHasJiang){
int iNeedImagic = aKindPai[j];
if (iImagicPaiNum < (3 - iNeedImagic)){ return false; }
aKindPai[j] = 0;
aKindPai[0] -= iNeedImagic;
iImagicPaiNum -= (3 - iNeedImagic);
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] = iNeedImagic;
aKindPai[0] += iNeedImagic;
iImagicPaiNum += (3 - iNeedImagic);
return bResult;
}
int iNeedImagic = aKindPai[j];
if (iImagicPaiNum < (2- iNeedImagic)){ return false; }
aKindPai[j] = 0;
aKindPai[0] -= iNeedImagic;
bHasJiang = true;
iImagicPaiNum -= (2 - iNeedImagic);
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] = iNeedImagic;
aKindPai[0] += iNeedImagic;
iImagicPaiNum += (2 - iNeedImagic);
return bResult;
}
}
else
{
//處理牌的數量為1
if (aKindPai[j] == 1){
if (aKindPai[j + 1] > 0 && aKindPai[j + 2] > 0)
{
if ((aKindPai[j + 1] + aKindPai[j + 2]) > 4){
if (bHasJiang){
if (iImagicPaiNum < 2){ return false; }
aKindPai[j] -= 1;
iImagicPaiNum -= 2;
aKindPai[0] -= 1;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 1;
aKindPai[0] += 1;
iImagicPaiNum += 2;
return bResult;
}
if (iImagicPaiNum < 1){ return false; }
aKindPai[j] -= 1;
iImagicPaiNum -= 1;
bHasJiang = true;
aKindPai[0] -= 1;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[0] += 1;
aKindPai[j] += 1;
iImagicPaiNum += 1;
return bResult;
}
aKindPai[j] -= 1;
aKindPai[j + 1] -= 1;
aKindPai[j + 2] -= 1;
aKindPai[0] -= 3;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 1;
aKindPai[j + 1] += 1;
aKindPai[j + 2] += 1;
aKindPai[0] += 3;
return bResult;
}
//當沒有可以與它組成順子的牌,開始判斷有沒有比它大一的牌
else if (aKindPai[j + 1]>0){
switch (aKindPai[j + 1])
{
case 1:
if (iImagicPaiNum < 1){return false;}
aKindPai[j] -= 1;
aKindPai[j + 1] -= 1;
iImagicPaiNum -= 1;
aKindPai[0] -= 2;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 1;
aKindPai[j + 1] += 1;
aKindPai[0] += 2;
iImagicPaiNum += 1;
return bResult;
break;
case 2:
if (bHasJiang){
if (iImagicPaiNum < 3){ return false; }
aKindPai[j] -= 1;
aKindPai[j + 1] -= 2;
aKindPai[0] -= 3;
iImagicPaiNum -= 3;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 1;
aKindPai[j + 1] += 2;
aKindPai[0] += 3;
iImagicPaiNum += 3;
return bResult;
}
else{
if (iImagicPaiNum < 2){ return false; }
aKindPai[j] -= 1;
aKindPai[j + 1] -= 2;
aKindPai[0] -= 3;
iImagicPaiNum -= 2;
bHasJiang = true;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 1;
aKindPai[j + 1] += 2;
aKindPai[0] += 3;
iImagicPaiNum += 2;
return bResult;
}
break;
case 3:
aKindPai[j + 1] -= 3;
aKindPai[0] -= 3;
bResult = (aKindPai, bIsZi, bHasJiang);
aKindPai[j + 1] += 3;
aKindPai[0] += 3;
return bResult;
/*if (bHasJiang){
if (iImagicPaiNum < 2){ return false; }
aKindPai[j] -= 1;
aKindPai[j + 1] -= 3;
iImagicPaiNum -= 2;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 1;
aKindPai[j + 1] += 3;
iImagicPaiNum += 2;
return bResult;
}
else{
if (iImagicPaiNum < 1){ return false; }
aKindPai[j] -= 1;
aKindPai[j] -= 3;
iImagicPaiNum -= 1;
bHasJiang = true;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 1;
aKindPai[j + 1] += 3;
iImagicPaiNum += 1;
return bResult;
}*/
break;
case 4:
aKindPai[j + 1] -= 3;
aKindPai[0] -= 3;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j + 1] += 3;
aKindPai[0] += 3;
return bResult;
break;
}
}
//孤立的一張牌
else{
if (bHasJiang){
if (iImagicPaiNum < 2){ return false; }
aKindPai[j] -= 1;
iImagicPaiNum -= 2;
aKindPai[0] -= 1;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 1;
aKindPai[0] += 1;
iImagicPaiNum += 2;
return bResult;
}
else
{
if (iImagicPaiNum < 1){ return false; }
aKindPai[j] -= 1;
aKindPai[0] -= 1;
iImagicPaiNum -= 1;
bHasJiang = true;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 1;
aKindPai[0] += 1;
iImagicPaiNum += 1;
return bResult;
}
}
}
//處理牌的數量為2
else if (aKindPai[j] == 2)
{
if (((aKindPai[j + 1] + aKindPai[j + 2]) <= 4) && (aKindPai[j + 1] > 0) && (aKindPai[j + 2] > 0))
{
int iNeedBigOne = aKindPai[j + 1];
int iNeedBigTwo = aKindPai[j + 2];
if (iImagicPaiNum <( 4-(iNeedBigOne + iNeedBigTwo))){ return false; }
aKindPai[j] -= 2;
aKindPai[j + 1] = 0;
aKindPai[j + 2] = 0;
iImagicPaiNum -= iNeedBigOne;
iImagicPaiNum -= iNeedBigTwo;
aKindPai[0] = aKindPai[0] - 2 - iNeedBigOne - iNeedBigTwo;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 2;
aKindPai[j + 1] += iNeedBigOne;
aKindPai[j + 2] += iNeedBigTwo;
aKindPai[0] = aKindPai[0] + 2 + iNeedBigOne + iNeedBigTwo;
return bResult;
}
else {
if (bHasJiang){
if (iImagicPaiNum < 1){ return false; }
aKindPai[j] -= 2;
iImagicPaiNum -= 1;
aKindPai[0] -= 2;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 2;
aKindPai[0] += 2;
iImagicPaiNum += 1;
return bResult;
}
aKindPai[j] -= 2;
aKindPai[0] -= 2;
bHasJiang = true;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 2;
aKindPai[0] += 2;
return bResult;
}
}
//處理牌數量為3或4
else if ((aKindPai[j] == 3) || (aKindPai[j] == 4))
{
aKindPai[j] -= 3;
aKindPai[0] -= 3;
bResult = AnalyzePai(aKindPai, bIsZi, bHasJiang);
aKindPai[j] += 3;
aKindPai[0] += 3;
return bResult;
}
}
}
return false;
}
自己寫的帶混牌麻將算法