首先讲一下思路吧。
德州扑克一把有7张牌,可能组成10种牌型,所以,在网络游戏中,不可能是两两比较,然后排序,最简单的做法就是对每个玩家的手牌计算一个权值,然后对最终玩家的权值进行排序即可得到最终的输赢顺序。
其实这个是我的毕业设计,在大三的时候,我曾在一位学长王总的带领下做过一个德州扑克的记录软件,那个软件比较简单,只是有记录员记录现实世界中正在进行的游戏,然后把玩家的牌,叫牌操作和游戏结果记录下来,以供将来重放时做教学使用。当时的项目并没有输赢判断的功能,而是由记录员自己输入输赢的玩家顺序。大四时的毕业设计,我就想可以在这个基础上实现一个在线的网络游戏,不再需要记录员这个角色,虽然并没有实现,但过程中的各种问题也都想到了解决方案,包括本文要讲的输赢算法的实现,当时就是太懒了,毕业论文就用了一个星期就写完了,下下周就是初版的截止时间,这周末我才开始写毕设,到下周日的时候检查了重复率就把初稿发给鲍老师了,以后再也没改过。。。=如今参加工作了,想着在下班没有事的时候做些自己的事情,而且当时在大三的时候我是想要做输赢算法的实现的,但苦于当时没有想到好的解决方案,到网上搜索又没有找到现成的容易理解的解决方案。所以首先就想到了把这个输赢的算法实现了。
下面讲一下实现的过程:
首先定义了两个类:Card和CardSum,Card保存玩家手中的牌,所以就两个属性:牌色和牌大小,CardSum是用来归类用的,要统计每个牌型大小的牌个数,下面是两个类的定义:
enum Color//花色定义 { A, S, D, B } class Card:IComparable<Card> //实现IComparable接口,用来后面的cardList排序 { public Color Hua { get; set; }// 牌的花色 public int Num { get; set; } // 牌的大小,2—14 public int CompareTo(Card other) { return this.Num.CompareTo(other.Num); } }
Card
class CardSum : IComparable<CardSum>//实现IComparable接口,用来后面的List排序 { public int Num { set; get; } public int Sum { set; get; } public int CompareTo(CardSum other) { return this.Num.CompareTo(other.Num); } }
CardSum
所以便有了函数原型:
public static double GetWeight(List<Card> cardList, out CardsType cardsType);
下面讲一下算法实现的过程:
其实根据各种牌型的特点很容易想到的,主要就是牌的花色,牌的相同的个数和牌的顺序这些,在我们知道这些之后就可以很轻松的判断出牌型,根据牌型,就可以获取到玩家手中最终有用的五张牌,根据这五张牌和牌型,进而计算玩家手牌的权重
下面的表格显示各种牌型的判断成立条件,而有效牌的查找方法相对简单,大家可以到代码里看看:
权重值大小=牌型基数+“.”+特征牌+非特征牌。
这样最终既可以获得牌型和权重大小。下面是关键算法实现代码:
public static void BubbleSort<T>(List<T> arrayList) where T : IComparable<T> { for (int i = 0; i < arrayList.Count - 1; i++) { for (int j = 0; j < arrayList.Count - i - 1; j++) { if (arrayList[j].CompareTo(arrayList[j + 1]) > 0) { T temp = arrayList[j]; arrayList[j] = arrayList[j + 1]; arrayList[j + 1] = temp; } } } } public static double GetWeight(List<Card> cardList, out CardsType cardsType) { string result = ""; List<Card> countList = new List<Card>(); cardsType = new CardsType(); BubbleSort(cardList); int A_Sum = 0, B_Sum = 0,D_Sum=0,S_Sum=0; List<CardSum> cardSumList = new List<CardSum>(); foreach (var item in cardList) { switch(item.Hua) { case Color.A: A_Sum++; break; case Color.B: B_Sum++; break; case Color.D: D_Sum++; break; case Color.S: S_Sum++; break; } bool EXIT = false; foreach (var carSum in cardSumList) { if(carSum.Num==item.Num) { carSum.Sum++; EXIT = true; break; } } if(!EXIT) { cardSumList.Add(new CardSum() { Num=item.Num,Sum=1}); } } int Four = 0, Three = 0, Two = 0,One=0; foreach (var item in cardSumList) { switch(item.Sum) { case 4: Four++; break; case 3: Three++; break; case 2: Two++; break; case 1: One++; break; } } BubbleSort(cardSumList); int[] card5=new int[5]; bool ISSHUNZI = false; if (cardSumList.Count >= 5 && cardSumList[0].Num + 1 == cardSumList[1].Num && cardSumList[1].Num + 1 == cardSumList[2].Num && cardSumList[2].Num + 1 == cardSumList[3].Num && cardSumList[3].Num + 1 == cardSumList[4].Num) { ISSHUNZI = true; for (int i = 0; i < 6; i++) { card5[i] = cardSumList[i].Num; } } else if (cardSumList.Count >= 6 && cardSumList[1].Num + 1 == cardSumList[2].Num && cardSumList[2].Num + 1 == cardSumList[3].Num && cardSumList[3].Num + 1 == cardSumList[4].Num && cardSumList[4].Num + 1 == cardSumList[5].Num) { ISSHUNZI = true; for (int i = 1; i < 7; i++) { card5[i-1] = cardSumList[i].Num; } } else if (cardSumList.Count >= 7 && cardSumList[2].Num + 1 == cardSumList[3].Num && cardSumList[3].Num + 1 == cardSumList[4].Num && cardSumList[4].Num + 1 == cardSumList[5].Num && cardSumList[5].Num + 1 == cardSumList[6].Num) { ISSHUNZI = true; for (int i = 2; i < 8; i++) { card5[i-2] = cardSumList[i].Num; } } if(Four==1) { cardsType = CardsType.SiTiao; foreach (var item in cardSumList) { if(item.Sum==4) { cardSumList.Remove(item); countList.Add(new Card() { Num = item.Num }); countList.Add(new Card() { Num = item.Num }); countList.Add(new Card() { Num = item.Num }); countList.Add(new Card() { Num = item.Num }); break; } } CardSum lastOne=cardSumList[cardSumList.Count-1]; countList.Add(new Card() { Num = lastOne.Num}); } else if(Three==1&&Two>0) { cardsType = CardsType.ManTangHon; foreach (var item in cardSumList) { if (item.Sum == 3) { cardSumList.Remove(item); countList.Add(new Card() { Num = item.Num }); countList.Add(new Card() { Num = item.Num }); countList.Add(new Card() { Num = item.Num }); break; } } for (int i = cardSumList.Count-1; i >=0; i--) { if (cardSumList[i].Sum == 2) { countList.Add(new Card() { Num = cardSumList[i].Num }); countList.Add(new Card() { Num = cardSumList[i].Num }); cardSumList.RemoveAt(i); break; } } } else if (ISSHUNZI && (A_Sum > 4 || B_Sum > 4 || D_Sum > 4 || S_Sum > 4)) { cardsType = CardsType.TongHuaShun; for (int i = 5; i >=0; i--) { countList.Add(new Card() { Num=card5[i]}); } } else if(A_Sum>4||B_Sum>4||D_Sum>4||S_Sum>4) { cardsType = CardsType.TongHua; if(A_Sum>4) { for (int i = cardList.Count-1; i >=0; i--) { if(cardList[i].Hua==Color.A&&countList.Count<5) { countList.Add(new Card() { Num=cardList[i].Num}); } } } else if (B_Sum > 4) { for (int i = cardList.Count - 1; i >= 0; i--) { if (cardList[i].Hua == Color.B && countList.Count < 5) { countList.Add(new Card() { Num = cardList[i].Num }); } } } else if (D_Sum > 4) { for (int i = cardList.Count - 1; i >= 0; i--) { if (cardList[i].Hua == Color.D&& countList.Count < 5) { countList.Add(new Card() { Num = cardList[i].Num }); } } } if (S_Sum > 4) { for (int i = cardList.Count - 1; i >= 0; i--) { if (cardList[i].Hua == Color.S && countList.Count < 5) { countList.Add(new Card() { Num = cardList[i].Num }); } } } } else if (ISSHUNZI) { cardsType = CardsType.ShunZi; for (int i = 5; i >= 0; i--) { countList.Add(new Card() { Num = card5[i] }); } } else if (Three == 1 && cardSumList.Count == 5) { cardsType = CardsType.SanTiao; foreach (var item in cardSumList) { if (item.Sum == 3) { cardSumList.Remove(item); countList.Add(new Card() { Num = item.Num }); countList.Add(new Card() { Num = item.Num }); countList.Add(new Card() { Num = item.Num }); break; } } countList.Add(new Card() { Num=cardSumList[cardSumList.Count-1].Num}); countList.Add(new Card() { Num = cardSumList[cardSumList.Count - 2].Num }); } else if(Two>=2) { cardsType = CardsType.LiangDui; for(int i=cardSumList.Count-1;i>=0;i--) { if(cardSumList[i].Sum==2&&countList.Count<4) { countList.Add(new Card() { Num = cardSumList[i].Num }); countList.Add(new Card() { Num = cardSumList[i].Num }); cardSumList.RemoveAt(i); } } countList.Add(new Card() { Num = cardSumList[cardSumList.Count - 1].Num }); } else if(Two==1&&cardSumList.Count==6) { cardsType = CardsType.YiDui; foreach (var item in cardSumList) { if (item.Sum == 2) { cardSumList.Remove(item); countList.Add(new Card() { Num = item.Num }); countList.Add(new Card() { Num = item.Num }); break; } } countList.Add(new Card() { Num = cardSumList[cardSumList.Count - 1].Num }); countList.Add(new Card() { Num = cardSumList[cardSumList.Count - 2].Num }); countList.Add(new Card() { Num = cardSumList[cardSumList.Count - 3].Num }); } else if(cardSumList.Count==7) { cardsType = CardsType.GaoPai; countList.Add(new Card() { Num = cardSumList[cardSumList.Count - 1].Num }); countList.Add(new Card() { Num = cardSumList[cardSumList.Count - 2].Num }); countList.Add(new Card() { Num = cardSumList[cardSumList.Count - 3].Num }); countList.Add(new Card() { Num = cardSumList[cardSumList.Count - 4].Num }); countList.Add(new Card() { Num = cardSumList[cardSumList.Count - 5].Num }); } result = result + (int)cardsType+"."; foreach (var item in countList) { if(item.Num>9) { result = result + item.Num; } else { result = result + "0" + item.Num; } } return Double.Parse(result); }
GetWeight
enum CardsType { TongHuaShun=9, SiTiao=8, ManTangHon=7, TongHua=6, ShunZi=5, SanTiao=4, LiangDui=3, YiDui=2, GaoPai=1 }
CardsType
第一次写博客,讲的有点乱。。。。
最后附上完整项目下载地址:http://files.cnblogs.com/files/LouisGuo/ConsoleApplication1.7z