# Unity IUnityLinkerProcessor Unity IUnityLinkerProcessor是Unity引擎中的一個介面,它允許開發者在Unity項目構建時對代碼進行鏈接處理。這個介面可以用來優化項目構建大小,減少不必要的代碼和資源,提高項目的性能和載入速度。 ## 介面定義 ...
德州撲克是一種牌類游戲,可多人參與,它的玩法是,玩家每人發兩張底牌,桌面依次發5張公共牌,玩家用自己的兩張底牌和5張公共牌自由組合,按大小決定勝負。
使用c#完成功能Hand()以返回手牌類型和按重要性遞減順序排列的等級列表,用於與同類型的其他手牌進行比較,即最佳手牌。
可能的手牌按價值降序排列:
同花順(同一套衣服的連續五個等級)。級別越高越好。
四張(四張等級相同的牌)。平局決勝先是等級,然後是剩餘牌的等級。
滿座(三張等級相同的牌,兩張等級相同)。決勝局首先是三張牌的等級,然後是一對牌的等級。
同花順(五張同花色的牌)。從高到低,級別越高越好。
直(連續五個等級)。級別越高越好。
三張牌(三張等級相同的牌)。決勝局是三張牌中排名第一的,然後是其他排名最高的,然後才是其他排名第二的。
兩對(兩張相同等級的牌,兩張不同等級的牌)。決勝局首先是高牌對的等級,然後是低牌對的級別,然後是剩餘牌的等級。
配對(兩張等級相同的牌)。平局決勝是先是兩張牌的等級,然後是其他三張牌的級別。
演算法實現:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 public static class Edm 7 { 8 public static (string type, string[] ranks) Hand(string[] holeCards, string[] communityCards) 9 { 10 Card[] allCards = holeCards.Concat(communityCards).Select( x=> new Card(x)).OrderByDescending(card =>card.value).ToArray(); 11 12 var rulesChain = createChainOfCommand(); 13 var powerhands = rulesChain.Execute(allCards); 14 return (powerhands.Item1, getReturnlist(allCards, powerhands.Item2)); 15 16 } 17 public static string[] getReturnlist(Card[] cards, Card[] powerhand) 18 { 19 var remainderHand = cards.Where(x => !powerhand.Any(y => y.Equals(x))).Take(5-powerhand.Length); 20 var result = powerhand.Select(x =>x.number).Distinct().Concat(remainderHand.Select(x=>x.number)).Take(5).Select(x => x.ToString()).ToArray(); 21 return result; 22 } 23 24 public static Rule createChainOfCommand() 25 { 26 Rule straightFlush = new StraightFlushRule(); 27 Rule fourOfAKind = new FourOfAKindRule(); 28 Rule fullHouse = new FullHouseRule(); 29 Rule flush = new FlushRule(); 30 Rule straight = new StraightRule(); 31 Rule threeOfAKind = new ThreeOfAKindRule(); 32 Rule pairTwoPair = new PairTwoPairRule(); 33 straightFlush.SetSuccessor(fourOfAKind); 34 fourOfAKind.SetSuccessor(fullHouse); 35 fullHouse.SetSuccessor(flush); 36 flush.SetSuccessor(straight); 37 straight.SetSuccessor(threeOfAKind); 38 threeOfAKind.SetSuccessor(pairTwoPair); 39 return straightFlush; 40 } 41 } 42 public abstract class Rule 43 { 44 private Rule nextRule; 45 public void SetSuccessor(Rule next) 46 { 47 nextRule = next; 48 } 49 public virtual (string, Card[]) Execute(Card[] cards) 50 { 51 if (nextRule != null) 52 { 53 return nextRule.Execute(cards); 54 } 55 return ("nothing", cards.Take(5).ToArray()); 56 } 57 } 58 59 public class PairTwoPairRule : Rule 60 { 61 public override (string, Card[]) Execute(Card[] cards) 62 { 63 var pairs = cards.GroupBy(x => x.number).Where(g => g.Count() >= 2).SelectMany(card => card).ToList(); 64 if (pairs.Any()) 65 { 66 if(pairs.Count() >= 4) 67 { 68 return ("two pair", pairs.Take(4).ToArray()); 69 } 70 return ("pair", pairs.Take(2).ToArray()); 71 } 72 return base.Execute(cards); 73 } 74 } 75 public class ThreeOfAKindRule : Rule 76 { 77 public override (string, Card[]) Execute(Card[] cards) 78 { 79 var triple = cards.GroupBy(x => x.number).Where(g => g.Count() >= 3).SelectMany(card => card).ToList(); 80 if (triple.Any()) 81 { 82 return ("three-of-a-kind", triple.Take(3).ToArray()); 83 } 84 return base.Execute(cards); 85 } 86 } 87 public class StraightRule : Rule 88 { 89 public override (string, Card[]) Execute(Card[] cards) 90 { 91 for (int i = 0; i < cards.Length - 4; i++) 92 { 93 List<Card> rtnList = new List<Card>() { cards[i] }; // "A♥","J♦","10♥" "9♠", "9♥", "8♠", "7♣" 94 int counter = 4; 95 int j = i; 96 while (counter >= 0 && j < cards.Length - 1) 97 { 98 if (cards[j].value - cards[j + 1].value == 1) 99 { 100 rtnList.Add(cards[j + 1]); 101 102 if (rtnList.Count() == 5) 103 { 104 return ("straight", rtnList.ToArray()); 105 } 106 counter--; 107 j++; 108 } 109 else if (cards[j].value - cards[j + 1].value == 0) 110 { 111 j++; 112 } 113 else 114 { 115 break; 116 } 117 } 118 } 119 return base.Execute(cards); 120 } 121 } 122 public class FlushRule : Rule 123 { 124 public override (string, Card[]) Execute(Card[] cards) 125 { 126 var flush = cards.GroupBy(x => x.suit).Where(g => g.Count() >= 5).SelectMany(card => card).ToList(); 127 if (flush.Any()) 128 { 129 return ("flush", flush.ToArray()); 130 } 131 return base.Execute(cards); 132 } 133 } 134 135 public class FullHouseRule : Rule 136 { 137 public override (string, Card[]) Execute(Card[] cards) 138 { 139 var triple = new ThreeOfAKindRule(); 140 var pair = new PairTwoPairRule(); 141 142 var powerhands = triple.Execute(cards); 143 if (!powerhands.Item1.Equals("nothing")) 144 { 145 if (powerhands.Item2.Count() == 6) // then 2 three of a kind found 146 { 147 return ("full house", powerhands.Item2.Take(5).ToArray()); 148 } 149 var remainderHand = cards.Where(x => !powerhands.Item2.Any(y => y.Equals(x))).ToArray(); 150 var pairHand = pair.Execute(remainderHand); 151 if (!pairHand.Item1.Equals("nothing")) 152 { 153 var fullhouseHand = powerhands.Item2.Concat(pairHand.Item2.Take(2)).ToArray(); 154 return ("full house", fullhouseHand.Take(5).ToArray()); 155 } 156 } 157 return base.Execute(cards); 158 } 159 } 160 public class FourOfAKindRule : Rule 161 { 162 public override (string, Card[]) Execute(Card[] cards) 163 { 164 var fourOfAKind = cards.GroupBy(x => x.number).Where(g => g.Count() >= 4).SelectMany(card => card).ToList(); 165 if (fourOfAKind.Any()) 166 { 167 return ("four-of-a-kind", fourOfAKind.Take(4).ToArray()); 168 } 169 return base.Execute(cards); 170 } 171 } 172 public class StraightFlushRule : Rule 173 { 174 public override (string, Card[]) Execute(Card[] cards) 175 { 176 var flushRule = new FlushRule(); 177 var straightRule = new StraightRule(); 178 var flushHand = flushRule.Execute(cards); 179 var straightHand = straightRule.Execute(flushHand.Item2); 180 if (!straightHand.Item1.Equals("nothing") && !flushHand.Item1.Equals("nothing")) 181 { 182 return ("straight-flush", straightHand.Item2.Take(5).ToArray()); 183 } 184 return base.Execute(cards); 185 } 186 } 187 188 public class Card{ 189 public String number { get; set; } 190 public int value { get; set; } 191 public char suit { get; set; } 192 public Dictionary<char, int> mapping = new Dictionary<char, int>() 193 { 194 { 'A',14 }, 195 { 'K',13 }, 196 { 'Q',12 }, 197 { 'J',11 }, 198 { '1', 10} 199 }; 200 public Card(String s) 201 { 202 number = (s[0] == '1')? "10": Char.ToString(s[0]); 203 value = mapping.ContainsKey(s[0])? mapping[s[0]]: (int) Char.GetNumericValue(s[0]); 204 suit = s[s.Length-1]; 205 } 206 public override string ToString() 207 { 208 return number.ToString(); 209 } 210 211 public bool equals(Card s) 212 { 213 return this.value == s.value && this.suit.Equals(s.suit); 214 } 215 }
測試用例:
1 namespace Solution 2 { 3 using NUnit.Framework; 4 using System; 5 using System.Collections.Generic; 6 using System.Diagnostics; 7 using System.Linq; 8 using System.Text; 9 10 [TestFixture] 11 public class SolutionTest 12 { 13 #region Sample Tests 14 15 [Test(Description = "Fixed Tests")] 16 public void FixedTests() 17 { 18 SampleTest(("nothing", new[] { "A", "K", "Q", "J", "9" }), new[] { "K♠", "A♦" }, new[] { "J♣", "Q♥", "9♥", "2♥", "3♦" }); 19 SampleTest(("pair", new[] { "Q", "K", "J", "9" }), new[] { "K♠", "Q♦" }, new[] { "J♣", "Q♥", "9♥", "2♥", "3♦" }); 20 SampleTest(("two pair", new[] { "K", "J", "9" }), new[] { "K♠", "J♦" }, new[] { "J♣", "K♥", "9♥", "2♥", "3♦" }); 21 SampleTest(("three-of-a-kind", new[] { "Q", "J", "9" }), new[] { "4♠", "9♦" }, new[] { "J♣", "Q♥", "Q♠", "2♥", "Q♦" }); 22 SampleTest(("straight", new[] { "K", "Q", "J", "10", "9" }), new[] { "Q♠", "2♦" }, new[] { "J♣", "10♥", "9♥", "K♥", "3♦" }); 23 SampleTest(("flush", new[] { "Q", "J", "10", "5", "3" }), new[] { "A♠", "K♦" }, new[] { "J♥", "5♥", "10♥", "Q♥", "3♥" }); 24 SampleTest(("full house", new[] { "A", "K" }), new[] { "A♠", "A♦" }, new[] { "K♣", "K♥", "A♥", "Q♥", "3♦" }); 25 SampleTest(("four-of-a-kind", new[] { "2", "3" }), new[] { "2♠", "3♦" }, new[] { "2♣", "2♥", "3♠", "3♥", "2♦" }); 26 SampleTest(("straight-flush", new[] { "J", "10", "9", "8", "7" }), new[] { "8♠", "6♠" }, new[] { "7♠", "5♠", "9♠", "J♠", "10♠" }); 27 } 28 29 private static void SampleTest((string type, string[] ranks) expected, string[] holeCards, string[] communityCards) 30 { 31 var actual = Act(holeCards, communityCards); 32 Verify(expected, actual, holeCards, communityCards); 33 } 34 35