在拆牌的时候,需要算出手牌中哪些牌是重复的,这样才能找出单牌、对牌、三张、四张。
不分花色,从牌3-K,定义牌值为3-15,小王是16,大王是17
如表:
牌面:
3
4
5
6
7
8
9
10
J
Q
K
A
2
大小王
定义牌值:
3
4
5
6
7
8
9
10
11
12
13
14
15
16、17
这样牌值一共有14种类型,大小王算一种类型。
然后给每个牌值定义一个整数型计数器(int count)数组,一个有14个计数器,初始化每个计数器值为0,表示0个牌值出现,
然后对牌LIST进行for循环,当出现牌值时候加一:
序号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
牌面:
3
4
5
6
7
8
9
10
J
Q
K
A
2
大小王
定义牌值:
3
4
5
6
7
8
9
10
11
12
13
14
15
16、17
计数器
count[0]
count[1]
count[2]
count[3]
count[4]
count[5]
count[6]
count[7]
count[8]
count[9]
count[10]
count[11]
count[12]
count[13]
加入现在的牌面是:334456777888JJK大王小王(17张)
那得到的计数应该是这样的:
序号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
牌面:
3
4
5
6
7
8
9
10
J
Q
K
A
2
大小王
定义牌值:
3
4
5
6
7
8
9
10
11
12
13
14
15
16、17
计数器
count[0]=2
2
1
1
3
3
0
0
2
0
1
0
0
2
这样就能算出各种牌的重复数量。
最后把各种重复数进行分类,有5中类型,分别是重复0次,重复1次....
定义4个list(A0,A1,A2,A3),重复0次的不要定义,因为用不到,然后使用for循环,按照重复次数把牌分到四个list中。
按照以上的牌,进行分类后是这样的:
list(int)数组
牌下标号
A0(同类牌有1张)
3,4,11
A1(同类牌有2张)
1,2,9,14
A2(同类牌有3张)
5,6
A3(同类牌有4张)
牌下标号,就是数组的位置,例如A0数组中的第一位A0[0]的值就是3,3代表序号为3的牌,牌面就是5(方块5或者红心5等)。
跟着这种思路,得到以下代码:
//get the max repeat of the card
public void getMaxRepeat(List list, CardIndex cardIndex)
{
//make a array that record the 14 types card's repeat times
//the card value 3-15 means 13 types ,the 2 big master is the 14 cardtype
//1-13 deputy the card value 3-15
int[] count = new int[14];
//initialize all the count to 0
for (int i = 0; i < 14; i++)
{
count[i] = 0;
}
//count the max repeat of the 14 card types
for (int i = 0, length = list.Count; i < length; i++)
{
if (getCardValue(list[i]) == 17 || getCardValue(list[i]) == 16)
{
//the 14th card type repeat:the big master
count[13]++;
}
else
{
//card values is 3-15,then -3
count[getCardValue(list[i]) - 3]++;
}
}
//add the result to the cardIndex,there are 4 situaiton
for (int i = 0; i < 14; i++)
{
switch (count[i])
{
case 1:
cardIndex.a0.Add(i + 1);
break;
case 2:
cardIndex.a1.Add(i + 1);
break;
case 3:
cardIndex.a2.Add(i + 1);
break;
case 4:
cardIndex.a3.Add(i + 1);
break;
}
}
}