斗牛算法C#
斗牛算法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace ConsoleApplication1 { public enum Suits { Spade = 4, Heart = 3, Diamond = 2, Club = 1, } public struct CardChip { public Suits suit; public int point; public string name; public byte suitName; } /// <summary> /// 结果类型 /// </summary> public struct CardsResult { public ResultType type; public int resultPoint; public CardChip theMax; } public enum ResultType { NoCow = 0, CowOne, CowTwo, CowThree, CowFour, CowFive, CowSix, CowSeven, CowEight, CowNine, CowTen, SilverCow, GoldCow, FiveSmall, Boom } public class Player { public int playerID; public List<CardChip> cardList = new List<CardChip>(); public CardsResult Result { get { return this.CalcResult(); } } /// <summary> /// show1 /// </summary> public void ShowCards() { Console.WriteLine("Player:" + playerID); foreach (CardChip cp in cardList) { string a = cp.name + ""; string b = ""; if (cp.suit == Suits.Heart) b = (char)3 + ""; else if (cp.suit == Suits.Diamond) b = (char)4 + ""; else if (cp.suit == Suits.Club) b = (char)5 + ""; else b = (char)6 + ""; Console.Write(b + a + ","); } Console.WriteLine(); } /// <summary> /// show2 /// </summary> public void ShowCrads2() { Console.WriteLine("Player:" + playerID); foreach (CardChip cp in cardList) { string a = cp.name + ""; string b = ""; if (cp.suit == Suits.Heart) b = (char)3 + ""; else if (cp.suit == Suits.Diamond) b = (char)4 + ""; else if (cp.suit == Suits.Club) b = (char)5 + ""; else b = (char)6 + ""; Console.WriteLine("┌───┐"); Console.WriteLine("│" + b + " │"); Console.WriteLine("│ " + a.PadRight(4) + "│"); Console.WriteLine("│ " + b + "│"); Console.WriteLine("└───┘"); } Console.WriteLine(); } /// <summary> /// 排序 /// </summary> public void Sort() { cardList.Sort( delegate(CardChip cc1, CardChip cc2) { if (cc1.point == cc2.point) { return cc1.suit.CompareTo(cc2.suit); } return cc1.point.CompareTo(cc2.point); }); } /// <summary> /// 计算牌点数 /// </summary> /// <returns></returns> private CardsResult CalcResult() { CardsResult result; result.type = ResultType.NoCow; result.resultPoint = 0; result.theMax = new CardChip(); if (Boom(cardList)) { result.theMax = cardList[cardList.Count - 1]; result.resultPoint = (int)ResultType.Boom; result.type = ResultType.Boom; return result; } if (FiveSmall(cardList)) { result.theMax = cardList[cardList.Count - 1]; result.resultPoint = (int)ResultType.FiveSmall; result.type = ResultType.FiveSmall; return result; } List<CardChip> colorCards = new List<CardChip>(); List<CardChip> noColorCards = new List<CardChip>(); for (int i = 0; i < cardList.Count; i++) { if (cardList[i].point >= 10) { colorCards.Add(cardList[i]); } else { noColorCards.Add(cardList[i]); } } switch (colorCards.Count) { case 0: return NoColorCard(cardList); case 1: return OneColorCard(cardList); case 2: return TwoColorCard(cardList); case 3: case 4: case 5: return MoreThanThreeColorCard(cardList); } return result; } /// <summary> /// 炸弹 /// </summary> /// <param name="list"></param> /// <returns></returns> private bool Boom(List<CardChip> list) { //(b|c|d)==a if ((list[1].point | list[2].point | list[3].point) == list[0].point) { return true; } return false; } /// <summary> /// 五小 /// </summary> /// <param name="list"></param> /// <returns></returns> private bool FiveSmall(List<CardChip> list) { int res = list[0].point + list[1].point + list[2].point + list[3].point + list[4].point; if (res <= 10) { return true; } return false; } /// <summary> /// 三四五花 /// </summary> /// <param name="list"></param> /// <returns></returns> private CardsResult MoreThanThreeColorCard(List<CardChip> list) { CardsResult cdResult; cdResult.theMax = list[list.Count - 1]; int res = (list[0].point + list[1].point) % 10; if (res == 0) { cdResult.type = ResultType.GoldCow; cdResult.resultPoint = 10; } else { cdResult.resultPoint = res; cdResult.type = (ResultType)res; } return cdResult; } /// <summary> /// 二花 /// </summary> /// <param name="list"></param> /// <returns></returns> private CardsResult TwoColorCard(List<CardChip> list) { CardsResult cdResult; cdResult.theMax = list[list.Count - 1]; if ((list[0].point + list[1].point) % 10 == 0) { cdResult.resultPoint = list[2].point; cdResult.type = (ResultType)list[2].point; } else if ((list[0].point + list[2].point) % 10 == 0) { cdResult.resultPoint = list[1].point; cdResult.type = (ResultType)list[1].point; } else if ((list[1].point + list[2].point) % 10 == 0) { cdResult.resultPoint = list[0].point; cdResult.type = (ResultType)list[0].point; } else if ((list[0].point + list[1].point + list[2].point) % 10 == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; } else { cdResult.resultPoint = 0; cdResult.type = ResultType.NoCow; } return cdResult; } /// <summary> /// 一花 /// </summary> /// <param name="list"></param> /// <returns></returns> private CardsResult OneColorCard(List<CardChip> list) { CardsResult cdResult; int temp = 0; if ((list[0].point + list[1].point) % 10 == 0) { temp = (list[2].point + list[3].point) % 10; if (temp == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; } else { cdResult.resultPoint = temp; cdResult.type = (ResultType)temp; } } else if ((list[0].point + list[2].point) % 10 == 0) { temp = (list[1].point + list[3].point) % 10; if (temp == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; } else { cdResult.resultPoint = temp; cdResult.type = (ResultType)temp; } } else if ((list[0].point + list[3].point) % 10 == 0) { temp = (list[1].point + list[2].point) % 10; if (temp == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; } else { cdResult.resultPoint = temp; cdResult.type = (ResultType)temp; } } else if ((list[1].point + list[2].point) % 10 == 0) { temp = (list[0].point + list[3].point) % 10; if (temp == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; } else { cdResult.resultPoint = temp; cdResult.type = (ResultType)temp; } } else if ((list[1].point + list[3].point) % 10 == 0) { temp = (list[0].point + list[2].point) % 10; if (temp == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; } else { cdResult.resultPoint = temp; cdResult.type = (ResultType)temp; } } else if ((list[2].point + list[3].point) % 10 == 0) { temp = (list[0].point + list[1].point) % 10; if (temp == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; } else { cdResult.resultPoint = temp; cdResult.type = (ResultType)temp; } } else if ((list[0].point + list[1].point + list[2].point) % 10 == 0) { cdResult.resultPoint = list[3].point; cdResult.type = (ResultType)list[3].point; } else if ((list[0].point + list[1].point + list[3].point) % 10 == 0) { cdResult.resultPoint = list[2].point; cdResult.type = (ResultType)list[2].point; } else if ((list[0].point + list[2].point + list[3].point) % 10 == 0) { cdResult.resultPoint = list[1].point; cdResult.type = (ResultType)list[1].point; } else if ((list[1].point + list[2].point + list[3].point) % 10 == 0) { cdResult.resultPoint = list[0].point; cdResult.type = (ResultType)list[0].point; } else { cdResult.resultPoint = 0; cdResult.type = ResultType.NoCow; } cdResult.theMax = list[list.Count - 1]; return cdResult; } /// <summary> /// 无花 /// </summary> /// <param name="list"></param> /// <returns></returns> private CardsResult NoColorCard(List<CardChip> list) { CardsResult cdResult; cdResult.theMax = list[list.Count - 1]; if ((list[0].point + list[1].point + list[2].point) % 10 == 0 && (list[3].point + list[4].point) % 10 == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; return cdResult; } else if ((list[0].point + list[1].point + list[2].point) % 10 == 0 && (list[3].point + list[4].point) % 10 != 0) { cdResult.resultPoint = (list[3].point + list[4].point) % 10; cdResult.type = (ResultType)((list[3].point + list[4].point) % 10); return cdResult; } if ((list[0].point + list[1].point + list[3].point) % 10 == 0 && (list[2].point + list[4].point) % 10 == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; return cdResult; } else if ((list[0].point + list[1].point + list[3].point) % 10 == 0 && (list[2].point + list[4].point) % 10 != 0) { cdResult.resultPoint = (list[2].point + list[4].point) % 10; cdResult.type = (ResultType)((list[2].point + list[4].point) % 10); return cdResult; } if ((list[0].point + list[1].point + list[4].point) % 10 == 0 && (list[2].point + list[3].point) % 10 == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; return cdResult; } else if ((list[0].point + list[1].point + list[4].point) % 10 == 0 && (list[2].point + list[3].point) % 10 != 0) { cdResult.resultPoint = (list[2].point + list[3].point) % 10; cdResult.type = (ResultType)((list[2].point + list[3].point) % 10); return cdResult; } if ((list[0].point + list[2].point + list[3].point) % 10 == 0 && (list[1].point + list[4].point) % 10 == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; return cdResult; } else if ((list[0].point + list[2].point + list[3].point) % 10 == 0 && (list[1].point + list[4].point) % 10 != 0) { cdResult.resultPoint = (list[1].point + list[4].point) % 10; cdResult.type = (ResultType)((list[1].point + list[4].point) % 10); return cdResult; } if ((list[0].point + list[2].point + list[4].point) % 10 == 0 && (list[1].point + list[3].point) % 10 == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; return cdResult; } else if ((list[0].point + list[2].point + list[4].point) % 10 == 0 && (list[1].point + list[3].point) % 10 != 0) { cdResult.resultPoint = (list[1].point + list[3].point) % 10; cdResult.type = (ResultType)((list[1].point + list[3].point) % 10); return cdResult; } if ((list[1].point + list[2].point + list[3].point) % 10 == 0 && (list[0].point + list[4].point) % 10 == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; return cdResult; } else if ((list[1].point + list[2].point + list[3].point) % 10 == 0 && (list[0].point + list[4].point) % 10 != 0) { cdResult.resultPoint = (list[0].point + list[4].point) % 10; cdResult.type = (ResultType)((list[0].point + list[4].point) % 10); return cdResult; } if ((list[1].point + list[2].point + list[4].point) % 10 == 0 && (list[0].point + list[3].point) % 10 == 0) { cdResult.resultPoint = 10; cdResult.type = cdResult.type = ResultType.CowTen; return cdResult; } else if ((list[1].point + list[2].point + list[4].point) % 10 == 0 && (list[0].point + list[3].point) % 10 != 0) { cdResult.resultPoint = (list[0].point + list[3].point) % 10; cdResult.type = (ResultType)((list[0].point + list[3].point) % 10); return cdResult; } if ((list[2].point + list[3].point + list[4].point) % 10 == 0 && (list[0].point + list[1].point) % 10 == 0) { cdResult.resultPoint = 10; cdResult.type = ResultType.CowTen; return cdResult; } else if ((list[2].point + list[3].point + list[4].point) % 10 == 0 && (list[0].point + list[1].point) % 10 != 0) { cdResult.resultPoint = (list[0].point + list[1].point) % 10; cdResult.type = (ResultType)((list[0].point + list[1].point) % 10); return cdResult; } cdResult.resultPoint = 0; cdResult.type = ResultType.NoCow; return cdResult; } } public class Card { /// <summary> /// The maximum player count /// </summary> public const int MAX_PLAYERS = 10; /// <summary> /// 构造 /// </summary> public Card() { } public void Start() { //Initialize crads List<CardChip> cards = this.InitCards(); //Random crads this.RandomCards(cards); //Deal cards to players Player[] players = Deal(cards, 5, 5); //print to screen for (int i = 0; i < players.Length; i++) { players[i].ShowCards(); } //calculate the result for each player Console.WriteLine("-----------------------------------------------------"); for (int k = 0; k < players.Length; k++) { CardsResult cr = players[k].Result; string str = string.Format("Player{0}:牌型-{1} 点数-{2} 最大牌-{3}{4}", k, (ResultType)cr.type, cr.resultPoint, (char)cr.theMax.suitName, cr.theMax.name); Console.WriteLine(str); } CalcWinner(players); } /// <summary> /// 初始化牌13*4 游戏蛮牛http://www.unitymanual.com/space-uid-13769.html CSDN博客http://write.blog.csdn.net/postlist /// </summary> /// <returns></returns> public List<CardChip> InitCards() { List<CardChip> cards = new List<CardChip>(); for (int i = 1; i <= 13; i++) { for (int j = 1; j <= 4; j++) { CardChip cc; cc.point = i; cc.suit = (Suits)j; if (j == 1) cc.suitName = 4; else if (j == 2) cc.suitName = 5; else if (j == 3) cc.suitName = 3; else cc.suitName = 6; if (i >= 2 && i <= 10) cc.name = i.ToString(); else if (i == 1) cc.name = "A"; else if (i == 11) cc.name = "J"; else if (i == 12) cc.name = "Q"; else cc.name = "K"; cards.Add(cc); } } return cards; } /// <summary> /// 洗牌 /// </summary> /// <param name="cds"></param> public void RandomCards(List<CardChip> cds) { Permute(cds); } /// <summary> /// Deal crads /// </summary> /// <param name="cds">洗好的牌</param> /// <param name="players">玩家数</param> /// <param name="num">每个玩家的牌数</param> /// <returns></returns> public Player[] Deal(List<CardChip> cds, int players, int num) { if (players > MAX_PLAYERS) { Console.WriteLine("Too much players"); return null; } Console.WriteLine("----------------------Deal cards----------------------"); Player[] playerList = new Player[players]; for (int i = 0; i < playerList.Length; i++) { playerList[i] = new Player(); playerList[i].playerID = i; playerList[i].cardList = new List<CardChip>(); } for (int j = 0; j < players * num; j++) { playerList[j % players].cardList.Add(cds[j]); } for (int k = 0; k < playerList.Length; k++) { playerList[k].Sort(); } return playerList; } /// <summary> /// Calculate the winner /// </summary> /// <param name="players"></param> public void CalcWinner(Player[] players) { List<Player> p = players.ToList<Player>(); p.Sort( delegate(Player p1, Player p2) { if (p1.Result.type == p2.Result.type) { if (p1.Result.theMax.point == p2.Result.theMax.point) { return p1.Result.theMax.suit.CompareTo(p2.Result.theMax.suit); } return p1.Result.theMax.point.CompareTo(p2.Result.theMax.point); } return p1.Result.type.CompareTo(p2.Result.type); }); Console.WriteLine("----------------------结果---------------------------"); for (int k = p.Count - 1; k >= 0; k--) { CardsResult cr = p[k].Result; string str = string.Format("Player{0}:牌型-{1} 点数-{2} 最大牌-{3}{4}", p[k].playerID, (ResultType)cr.type, cr.resultPoint, (char)cr.theMax.suitName, cr.theMax.name); Console.WriteLine(str); } Console.WriteLine("-----------------------------------------------------"); Console.WriteLine(string.Format("玩家{0}获胜!", p[4].playerID)); } #region Random Cards in Array private void Permute<T>(List<T> array) { Random random = new Random(); for (int i = 1; i < array.Count; i++) { Swap<T>(array, i, random.Next(0, i)); } } private void Swap<T>(List<T> array, int indexA, int indexB) { T temp = array[indexA]; array[indexA] = array[indexB]; array[indexB] = temp; } #endregion } }
本文出自 码农,转载时请注明出处及相应链接。
发表评论