实例介绍
【实例简介】源码不完整,仅供参考
【实例截图】
【核心代码】
using System.Collections.Generic; using Ourgame.Define; using System.Linq; namespace Ourgame.Utils { public enum Pattern { // 皇家同花顺 RoyalFlush, // 同花顺 StraightFlush, // 四条 FourKind, // 葫芦 FullHouse, // 同花 Flush, // 顺子 Straight, // 三条 ThreeKind, // 两对 TwoPair, // 一对 OnePair, // 高牌 HighCard, } public class CardGroup { // 牌型 public Pattern Pattern; // 符合牌型的牌值列表(四条、三条、两对、一对的使用牌值,高牌为空数组) public List<Card> PatternCards; // 组成牌组的五张牌(若不足五张时,Cards == PatternCards) public List<Card> Cards; } public class PatternAnalyzer { private List<Card> _cardList = new List<Card>(); private Pattern cardType = Pattern.HighCard; private List<Card> PatternCards = new List<Card>(); private List<Card> Cards = new List<Card>(); public CardGroup GetGroup(List<Card> cards) { CardGroup cardGroup = new CardGroup(); _cardList = cards; SortByIDOrPoint(2); if (HaveRoyalFlush()) { cardType = Pattern.RoyalFlush; } else if (HaveStraightFlush()) { cardType = Pattern.StraightFlush; } else if (HaveFourKind()) { cardType = Pattern.FourKind; } else if (HaveFullHouse()) { cardType = Pattern.FullHouse; } else if (HaveFlush()) { cardType = Pattern.Flush; } else if (HaveStraight()) { cardType = Pattern.Straight; } else if (HaveThreeKind()) { cardType = Pattern.ThreeKind; } else if (HaveTwoPair()) { cardType = Pattern.TwoPair; } else if (HaveOnePair()) { cardType = Pattern.OnePair; } else { FindHigh(); cardType = Pattern.HighCard; } cardGroup.Pattern = cardType; cardGroup.PatternCards = PatternCards; cardGroup.Cards = Cards; return cardGroup; } // 皇家同花顺 public bool HaveRoyalFlush() { if (_cardList.Count < 5) // 条件不满足 return false; int num = 0; int max = 0; PokerColor pokerColor = PokerColor.UNKNOW; for (int i = 0; i < _cardList.Count; i ) { for (int j = 0; j < _cardList.Count; j ) { if (_cardList[i].Color == _cardList[j].Color) num ; } if (num > max) // 找到花色出现最多的那种 { max = num; pokerColor = (PokerColor)_cardList[i].Color; } num = 0; } List<Card> tempList = new List<Card>(); for (int i = 0; i < _cardList.Count; i ) // 寻找同花色牌 { if (_cardList[i].Color == (int)pokerColor) tempList.Add(_cardList[i]); } if (tempList.Count < 5) // 同花色牌小于5张 return false; if (tempList[0].Point != 0) // 必须为A return false; else Cards.Add(tempList[0]); if (tempList.Count == 6) { tempList.RemoveRange(0, 2); } else if (tempList.Count == 7) { tempList.RemoveRange(0, 3); } else { tempList.RemoveAt(0); } Cards.AddRange(tempList); Card card = new Card(); card.Point = 13; tempList.Add(card); // 是否连续 if (!IsContinuous(tempList)) return false; return true; } // 同花顺 public bool HaveStraightFlush() { if(HaveFlush() && HaveStraight()) return true; return false; } // 四条 public bool HaveFourKind() { if (_cardList.Count < 4) // 条件不满足 return false; Cards = new List<Card>(); PatternCards = new List<Card>(); SortByIDOrPoint(1); bool isEqual = true; for (int i = 0; i < _cardList.Count - 3; i ) // -3: 整体后三位不需要再比 { for (int j = i; j < i 4; j ) // 3 : 四位与后三位进行比较 { if (_cardList[i].Point != _cardList[j].Point) { isEqual = false; break; } } if (isEqual) { for (int j = i; j < i 4; j ) // 3 : 四位与后三位进行比较 { PatternCards.Add(_cardList[j]); } Cards.AddRange(PatternCards); PatternCards.AddRange(Cards); return true; } isEqual = true; } return false; } // 葫芦 public bool HaveFullHouse() { if (_cardList.Count < 5) // 条件不满足 return false; Cards = new List<Card>(); PatternCards = new List<Card>(); List<Card> tempList = new List<Card>(); int three = -1; tempList = IsContainThree(_cardList, out three); if (tempList != null) // 葫芦的三 Cards.AddRange(tempList); else return false; int two = -1; tempList = IsContainTwoAndBesides(_cardList, out two, three); if (tempList != null) // 葫芦的二 Cards.AddRange(tempList); else return false; return true; } // 同花 public bool HaveFlush() { if (_cardList.Count < 5) // 条件不满足 return false; Cards = new List<Card>(); PatternCards = new List<Card>(); SortByIDOrPoint(2); bool isEqual = true; for (int i = 0; i < _cardList.Count - 4; i ) { isEqual = true; for (int j = i 1; j < i 5; j ) { if (_cardList[i].Color != _cardList[j].Color) { isEqual = false; break; } } if (isEqual) { Cards.Add(_cardList[i]); for (int j = i 1; j < i 5; j ) { Cards.Add(_cardList[j]); } return true; } } return false; } // 顺子 public bool HaveStraight() { if (_cardList.Count < 5) // 条件不满足 return false; Cards = new List<Card>(); PatternCards = new List<Card>(); SortByIDOrPoint(1); List<Card> cardList = KillTheSame(_cardList); List<Card> tempList = new List<Card>(); cardList = cardList.OrderByDescending(a => a.Point).ToList(); for (int i = 0; i < cardList.Count; i ) { if(cardList[i].Point == 0) // 有A { tempList.Add(cardList[i]); for (int j = cardList.Count - 1; j > cardList.Count - 5; j--) { tempList.Add(cardList[j]); } if (IsContinuous(tempList)) { Cards.AddRange(tempList); return true; } } } tempList.Clear(); for (int i = 0; i < cardList.Count - 4; i ) { for (int j = i; j < i 5; j ) { tempList.Add(cardList[j]); } if (IsContinuous(tempList)) { Cards.AddRange(tempList); return true; } tempList.Clear(); } return false; } // 三条 public bool HaveThreeKind() { if (_cardList.Count < 3) // 条件不满足 return false; Cards = new List<Card>(); PatternCards = new List<Card>(); SortByIDOrPoint(1); int three = -1; List<Card> tempList = new List<Card>(); tempList = IsContainThree(_cardList, out three); if (tempList != null) // 存在三位 { PatternCards.AddRange(tempList); Cards.AddRange(PatternCards); return true; } return false; } // 二对 public bool HaveTwoPair() { if (_cardList.Count < 2) // 条件不满足 return false; Cards = new List<Card>(); PatternCards = new List<Card>(); int two = -1; List<Card> tempList = new List<Card>(); tempList = IsContainTwoAndBesides(_cardList,out two); if (tempList == null) // 第一对 return false; else PatternCards.AddRange(tempList); tempList = IsContainTwoAndBesides(_cardList, out two,two); if (tempList != null) // // 第二对 { PatternCards.AddRange(tempList); Cards.AddRange(PatternCards); return true; } return false; } // 一对 public bool HaveOnePair() { if (_cardList.Count < 2) // 条件不满足 return false; Cards = new List<Card>(); PatternCards = new List<Card>(); int two = -1; List<Card> tempList = new List<Card>(); tempList = IsContainTwoAndBesides(_cardList, out two); if ( tempList != null) // 找到一对 { PatternCards.AddRange(tempList); Cards.AddRange(tempList); return true; } return false; } // 寻找高牌 public void FindHigh() { Cards = new List<Card>(); PatternCards = new List<Card>(); for (int i = 0; i < _cardList.Count; i ) { if (_cardList[i].Point == 0) // 如果是1点 { PatternCards.Add(_cardList[i]); Cards.AddRange(PatternCards); return; } } PatternCards.Add(_cardList[_cardList.Count - 1]); Cards.AddRange(PatternCards); } // 除开besides连续的两个数 List<Card> IsContainTwoAndBesides(List<Card> cardList, out int two, int besides = -1) { List<Card> tempList = new List<Card>(); bool isEqual = false; for (int i = 0; i < cardList.Count - 2; i ) // -3: 整体后俩位不需要再比 { isEqual = false; for (int j = i 1; j < i 3; j ) // 3 : 四位与后三位进行比较 { if (cardList[i].Point == cardList[j].Point) { isEqual = true; break; } } if (isEqual && (cardList[i].Point != besides || besides == -1)) // 除开besides { two = cardList[i].Point; tempList.Add(cardList[i]); for (int j = i 1; j < i 3; j ) // 3 : 四位与后三位进行比较 { if(cardList[j].Point == cardList[i].Point) tempList.Add(cardList[j]); } return tempList; } } two = -1; return null; } // 是否包含三条 List<Card> IsContainThree(List<Card> cardList,out int three) { List<Card> tempList = new List<Card>(); bool isEqual = true; for (int i = 0; i < cardList.Count - 2; i ) // -3: 整体后俩位不需要再比 { isEqual = true; for (int j = i 1; j < i 3; j ) // 3 : 四位与后三位进行比较 { if (cardList[i].Point != cardList[j].Point) { isEqual = false; break; } } if (isEqual) { three = cardList[i].Point; tempList.Add(cardList[i]); for (int j = i 1; j < i 3; j ) // 3 : 四位与后三位进行比较 { tempList.Add(cardList[j]); } return tempList; } } three = -1; return null; } // Point是否是连续 bool IsContinuous(List<Card> cardList) { List<Card> tempList = new List<Card>(); for (int i = 0; i < cardList.Count; i ) { if(cardList[i].Point == 0) // 存在A { Card card = new Card(); card.Point = 13; // 只是将A的点数改到13,用于判断是否连续 tempList.Add(card); } else { tempList.Add(cardList[i]); } } tempList.Sort((a, b) => { if (a.Point > b.Point) { return 1; } else if (a.Point < b.Point) { return -1; } return 0; }); if(tempList.Count>0) if (tempList[cardList.Count - 1].Point - tempList[0].Point == cardList.Count - 1) // 因A两种身份 这里是第一种 return true; return cardList[cardList.Count - 1].Point - cardList[0].Point == cardList.Count - 1 ? true : false; // 因A两种身份 这里是第二种 } // 是否有五个连续的数 List<Card> IsFiveContinuousInSeven(List<Card> cardList) { List<Card> tempList = new List<Card>(); for (int i = 0; i < cardList.Count - 4; i ) { tempList.Clear(); for (int j = i; j < i 5; j ) { tempList.Add(cardList[j]); } if (IsContinuous(tempList)) // 有连续相同的数 return tempList; } return null; } // 去除相同点数的牌,用于判断顺子 List<Card> KillTheSame(List<Card> cardList) { List<Card> tempList = new List<Card>(); List<int> existList = new List<int>(); for (int i = 0; i < cardList.Count; i ) { if(existList.Contains(cardList[i].Point)) { continue; } else { tempList.Add(cardList[i]); existList.Add(cardList[i].Point); } } return tempList; } public void SortByIDOrPoint(int type) { if(type == 1) // 根据Point升序 { _cardList = _cardList.OrderBy(a => a.Point).ToList(); } else if(type == 2) // 根据ID升序 { _cardList = _cardList.OrderBy(a => a.ID).ToList(); } else if(type == 3) // 根据Point降序 { _cardList = _cardList.OrderByDescending(a => a.Point).ToList(); } else if(type == 4) // 根据ID降序 { _cardList = _cardList.OrderByDescending(a => a.ID).ToList(); } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论