/// <summary> /// Returns less than one if _this_ hand is the losing hand. /// Returns greater than one if _this_ hand is the winning hand. /// Returns zero if the hands are a tie. /// </summary> /// <param name="other"></param> /// <returns></returns> public int CompareTo(PokerHand other) { List<Card> rankedAndOrderedCards; List<Card> otherRankedAndOrderedCards; var thisRank = ParseRank(out rankedAndOrderedCards); var otherRank = other.ParseRank(out otherRankedAndOrderedCards); if (thisRank != otherRank) { return thisRank - otherRank; } Debug.WriteLine("Comparing ({0})({1})", ShortNotationFromCardList(rankedAndOrderedCards), ShortNotationFromCardList(otherRankedAndOrderedCards)); Card highCard; Card otherHighCard; int idx = 0; do { highCard = rankedAndOrderedCards[idx]; otherHighCard = otherRankedAndOrderedCards[idx]; idx++; } while (highCard.Value == otherHighCard.Value && idx < rankedAndOrderedCards.Count); return (int)highCard.Value - (int)otherHighCard.Value; }
public void WhenHandIsRanked(string shortNotation, PokerHand.Rank expectedResult, string expectedOrderedList) { var hand = new PokerHand(shortNotation); var remainingCards = new List<Card>(); var actualResult = hand.ParseRank(out remainingCards); Assert.That(actualResult, Is.EqualTo(expectedResult)); Assert.That(remainingCards, Is.EqualTo(PokerHand.CardListFromShortNotation(expectedOrderedList))); }
static void Main(string[] args) { Console.WriteLine("Beginning long-running tests with machine-learning test cases..."); using (var tr = new StreamReader(@"poker-hand-testing.data")) { int testNumber = 0; int failedTests = 0; string testcase; while (null != (testcase = tr.ReadLine())) { if (!string.IsNullOrWhiteSpace(testcase)) { ++testNumber; var testcaseArray = testcase.Split(','); var testcaseData = new int[10]; for (int i = 0; i < 10; i++) testcaseData[i] = int.Parse(testcaseArray[i]); var hand = BuildPokerHandFromData(testcaseData); List<Card> orderedCards; if ((int)hand.ParseRank(out orderedCards) != int.Parse(testcaseArray[10])) { ++failedTests; Console.WriteLine("Test case failed: {0}", hand.ToString()); } } } Console.WriteLine("{0} tests executed, {1} tests failed", testNumber, failedTests); } Console.WriteLine("Beginning tests from project Euler..."); using (var tr = new StreamReader(@"poker-hand-testing-project-euler.data")) { int iWonByPlayer1 = 0; string testcase; while (null != (testcase = tr.ReadLine())) { if (!string.IsNullOrWhiteSpace(testcase)) { var testcaseArray = testcase.Split(' '); var hand1 = new PokerHand(string.Join(" ", testcaseArray.Take(5))); var hand2 = new PokerHand(string.Join(" ", testcaseArray.Skip(5).Take(5))); if (hand1.CompareTo(hand2) > 0) ++iWonByPlayer1; } } Console.WriteLine(iWonByPlayer1 == 376 ? "Passed.": "Failed."); } }
[TestCase("QH QC 2C 2D 2H", "4S 5D 3H 3S 3D", false)] // Rule: Full-house (FH) beats Taok // TODO: Four-of-a-Kind (Foak) // TODO: Straight Flush public void WhenHandIsCompared(string win, string lose, bool expectTie) { var winningHand = new PokerHand(win); var losingHand = new PokerHand(lose); if (expectTie) { Assert.That(winningHand.CompareTo(losingHand), Is.EqualTo(0)); Assert.That(losingHand.CompareTo(winningHand), Is.EqualTo(0)); } else { Assert.That(winningHand.CompareTo(losingHand), Is.GreaterThan(0)); Assert.That(losingHand.CompareTo(winningHand), Is.LessThan(0)); } }