这里只介绍普通的麻将胡牌算法,也就是7个对子或者 1个对子+3*N; N = 三个顺子或者三个一样的 ,其中字牌(东南西北中发白)不能算顺子。
首先对于每张牌 它有牌的的数字 1-9,牌的种类 (万条筒以及字)
所以先封装一个牌的包装类。如果要和服务器交互的话就应该给这个类序列化 即加上System.Serializable,然后再转Json格式,这里只做胡牌的基本判断。
public class Card
{
private int number;
public int Number
{
get { return number; }
set { number = value; }
}
private int type;
public int Type
{
get { return type; }
set { type = value; }
}
public Card(int n, int t)
{
number = n;
type = t;
}
}
首先对于一副牌应该先判断是否满足七对。不满足则向下判断。对于手牌会创建一个交错数组来保管。即
int[][] handcards = new int[4][] { new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
每个数组的第一位(handcards[i][0])用来保存当前类型牌的总数。
对应每种类型首先分为是否包含一个对子或者不包含对子。
所以对于四种类型应该是只有一种包含一个对子其他不包含对子。
代码如下:
//type为0,1,2,3则为万条筒字
class Program
{
public void asd(){}
static void Main(string[] args)
{
Card c1 = new Card(1, 1);
Card c2 = new Card(1, 1);
Card c3 = new Card(2, 1);
Card c4 = new Card(2, 1);
Card c5 = new Card(2, 1);
Card c6 = new Card(4, 1);
Card c7 = new Card(4, 1);
Card c8 = new Card(4, 1);
Card c9 = new Card(4, 3);
Card c10 = new Card(5, 3);
Card c11 = new Card(6, 3);
Card c12 = new Card(6, 1);
Card c13 = new Card(6, 1);
Card c14 = new Card(6, 1);
Card[] cs = { c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14 };
Console.WriteLine(isHu(cs));
}
public static bool isHu(Card[] cards)
{
int countCards = cards.Length;
//判断是否是七小对
if (countCards == 14)
{
int count = 0;
for (int i = 0; i < cards.Length; i += 2)
{
if (cards[i].Number == cards[i + 1].Number)
{
count++;
}
else
{
break;
}
}
if (count == 7)
{
return true;
}
}
int[][] handcards = new int[4][] { new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
for (int i = 0; i < cards.Length; i++)
{
//type为0,1,2,3则为万条筒字。 [type = 0,1,2,3 , 0]代表每种牌型的总数量
switch (cards[i].Type)
{
case 0: handcards[0][cards[i].Number]++;
handcards[0][0]++;
break;
case 1: handcards[1][cards[i].Number]++;
handcards[1][0]++;
break;
case 2: handcards[2][cards[i].Number]++;
handcards[2][0]++;
break;
case 3: handcards[3][cards[i].Number]++;
handcards[3][0]++;
break;
}
}
bool isJiang = false; //判断是否有对子
int jiangNumber = -1;
for (int i = 0; i < handcards.GetLength(0); i++)
{
if (handcards[i][0] % 3 == 2)
{
if (isJiang)
{
return false;
}
isJiang = true;
jiangNumber = i;
}
//因为对应四种牌型只能有一种且仅包含一个对子
}
//先求没有将牌的情况判断其是不是都是由刻子或者砍组成
for (int i = 0; i < handcards.GetLength(0); i++)
{
if (i != jiangNumber)
{
if (!(IsKanOrShun(handcards[i], i == 3)))
{
return false;
}
}
}
bool success = false;
//有将牌的情况下
for (int i = 1; i <= 9; i++)
{
if (handcards[jiangNumber][i] >= 2)
{
handcards[jiangNumber][i] -= 2;
handcards[jiangNumber][0] -= 2;
if (IsKanOrShun(handcards[jiangNumber], jiangNumber == 3))
{
success = true;
break;
}
else
{
handcards[jiangNumber][i] += 2;
handcards[jiangNumber][0] += 2;
}
}
}
return success;
}
//判断是否满足牌组为顺子或砍组成
public static bool IsKanOrShun(int[] arr, bool isZi)
{
if (arr[0] == 0)
{
return true;
}
int index = -1;
for (int i = 1; i < arr.Length; i++)
{
if (arr[i] > 0)
{
index = i;
break;
}
}
bool result;
//是否满足全是砍
if (arr[index] >= 3)
{
arr[index] -= 3;
arr[0] -= 3;
result = IsKanOrShun(arr, isZi);
arr[index] += 3;
arr[0] += 3;
return result;
}
//是否满足为顺子
if (!isZi)
{
if (index < 8 && arr[index + 1] >= 1 && arr[index + 2] >= 1)
{
arr[index] -= 1;
arr[index + 1] -= 1;
arr[index + 2] -= 1;
arr[0] -= 3;
result = IsKanOrShun(arr, isZi);
arr[index] += 1;
arr[index + 1] += 1;
arr[index + 2] += 1;
arr[0] += 3;
return result;
}
}
return false;
}
}