麻雀胡牌アルゴリズム——C#
6671 ワード
ここでは普通のマージャン胡牌アルゴリズム、つまり7つのペアまたは1つのペア+3*Nしか紹介しません.N=3つの順子あるいは3つの同じで、その中の字札(東南西北の中で白くなります)は順子を計算することができません.
まず、カードごとにカードの数字が1-9で、カードの種類(万本筒と字)
だからまず1つのカードの包装類をパッケージします.サーバと対話するにはこのクラスのシーケンス化にSystemを加えるべきである.Serializable、それからJsonフォーマットを回して、ここでは胡札の基本的な判断しかしません.
まず1枚のカードについて7対を満たすかどうかを判断しなければならない.満足しなければ下向きに判断する.手札には交差配列が作成され、保管されます.すなわち
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 } };
各配列の1番目(handcards[i][0])は、現在のタイプのカードの総数を保存するために使用される.
各タイプに対応して、まず1つのペアが含まれているかどうか、またはペアが含まれていないかに分けられます.
したがって、4つのタイプについては、1つのペアしか含まれていないはずです.他のペアは含まれていません.
コードは次のとおりです.
まず、カードごとにカードの数字が1-9で、カードの種類(万本筒と字)
だからまず1つのカードの包装類をパッケージします.サーバと対話するにはこのクラスのシーケンス化に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;
}
}
まず1枚のカードについて7対を満たすかどうかを判断しなければならない.満足しなければ下向きに判断する.手札には交差配列が作成され、保管されます.すなわち
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 } };
各配列の1番目(handcards[i][0])は、現在のタイプのカードの総数を保存するために使用される.
各タイプに対応して、まず1つのペアが含まれているかどうか、またはペアが含まれていないかに分けられます.
したがって、4つのタイプについては、1つのペアしか含まれていないはずです.他のペアは含まれていません.
コードは次のとおりです.
//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;
}
}