public void Beats_should_use_first_unlike_highest_card(string bestHand, string worstHand) { var hand1 = new PokerHand(bestHand); var hand2 = new PokerHand(worstHand); hand1.Beats(hand2).ShouldBeTrue(); hand2.Beats(hand1).ShouldBeFalse(); }
/// <summary>Constructor</summary> public Result(bool best, BitArray bestHandBits, PokerHand bestHand, PokerHand selectedHand) { _best = best; _bestHandBits = bestHandBits; _bestHand = bestHand; _selectedHand = selectedHand; }
public void Pair_should_beat_higher_pair() { var hand1 = new PokerHand("AC 2D 2C"); var hand2 = new PokerHand("2H 3A 3H"); hand1.Beats(hand2).ShouldBeFalse(); hand2.Beats(hand1).ShouldBeTrue(); }
public static void Main(string[] args) { int simCount = 5000; if (args.Length == 1) simCount = int.Parse(args[0]); Deck d = new Deck(); PokerHand hand = new PokerHand(d); Stats stats = new Stats(); stats.simCount = simCount; for (int i=0;i<simCount;i++) { // worry counter if ((i%1000)==0) Console.Write("*"); d.shuffle(); hand.pullCards(); hand.Sort(); POKERSCORE ps = PokerLogic.score(hand); stats.Append(ps); } Console.WriteLine(); stats.Report(); }
public void Pair_should_beat_high_card_only() { var hand1 = new PokerHand("2D 2C"); var hand2 = new PokerHand("AA 2H"); hand1.Beats(hand2).ShouldBeTrue(); hand2.Beats(hand1).ShouldBeFalse(); }
public void Equal_pairs_should_use_high_card_to_determine_winner() { var hand1 = new PokerHand("2D 2C AC KC TD"); var hand2 = new PokerHand("2H 2S AD KC 9C"); hand1.Beats(hand2).ShouldBeTrue(); hand2.Beats(hand1).ShouldBeFalse(); }
static void Main(string[] args) { bool debug = (args.Length > 0); int maxPokerHandSize = 5; // Two Hands PokerHand black = new PokerHand(maxPokerHandSize); PokerHand white = new PokerHand(maxPokerHandSize); string input; while ((input = Console.ReadLine()) != null) { // Debug - Some Test Cases and Expected Results //input = "2H 3D 5S 9C KD 2C 3H 4S AD AH"; // High Card, Pair //input = "2H 3D 4H 5D 6H 3C 4C 5C 6C 7C"; // Straight, Straight Flush //input = "2H 2D 3H 3D 4C AH AD TC TD TH"; // Two Pair, Full House (Correctly does 10) //input = "2H 2D 2C 4H 5H AH AD AC AS KD"; // 3Kind, 4Kind //input = "2H 4H 6H 8H TH 2D 4D 6D 8D TD"; // Flush (Tie)s //input = "2H 4D 6H 8D AH 3H 4C 6D 7C AD"; // Both Ace High, Black has Better Kickers //input = "2H 2D 4H 4D AH 2S 2C 4S 4C AC"; // Two Pair Real Tie if (debug) { Console.WriteLine(input); } // Clear the Hands black.Clear(); white.Clear(); // Parse and load Hands try { string[] cardStrings = input.Split(' '); for (int i = 0; i < black.MaxHandSize; ++i) { black.Add(new PlayingCard(cardStrings[i])); } for (int i = 0; i < white.MaxHandSize; ++i) { white.Add(new PlayingCard(cardStrings[i + black.MaxHandSize])); } } catch (Exception e) { Console.WriteLine("Bad Card in the Mix"); if (debug) { Console.WriteLine(e.StackTrace); Console.WriteLine(e.Message); } continue; } // Debug - Output the Scores if (debug) { Console.WriteLine("black score: " + black.ScoreHand().ToString()); Console.WriteLine("white score: " + white.ScoreHand().ToString()); } // Compare Hands - Output the Winner int compare = black.CompareTo(white); if (compare == 0) { Console.WriteLine("Tie."); } else if (compare < 0) { Console.WriteLine("White wins."); } else { Console.WriteLine("Black wins."); } } }
public void AddPokerHand(PokerHand ph) { if (!AllHands.ContainsKey(ph.ToLong())) { AnalyzePokerHand(ph); AllHands.Add(ph.ToLong(), ph); } }
public PokerHand GetPokerHand(PokerHand ph) { if (!AllHands.ContainsKey(ph.ToLong())) { AddPokerHand(ph); } return AllHands[ph.ToLong()] as PokerHand; }
public override bool IsValidScoreForHand(PokerHand hand) { if (HasThreeOfKind(hand.Cards)) { return FindPairs(hand.Cards).Count == 1; } return false; }
public HeartsGameGump( HeartsGame game, PokerHand hand ) : base( 10, 10 ) { _game = game; _hand = hand; AddPage( 1 ); AddBackground( 0, 0, 640, 480, 9250 ); }
public override bool IsValidScoreForHand(PokerHand hand) { foreach (var card in hand.Cards) { if (hand.Cards.Count(c => c.CardFace.Equals(card.CardFace)) == 4) return true; } return false; }
public void AddPokerHand(long hand) { if (!AllHands.ContainsKey(hand)) { var ph = new PokerHand(hand); AnalyzePokerHand(ph); AllHands.Add(ph.ToLong(), ph); } }
public bool UpdatePokerHand(Guid id, PokerHand item) { var pokerHand = this.PokerHands.Where(a => a.PokerHandID == id).SingleOrDefault(); if(pokerHand != null) { pokerHand.PokerHandName = item.PokerHandName; return true; } return false; }
private static void AnalyzePokerHand(PokerHand ph) { foreach (var pht in PokerHandsTypes) { if (pht.Parse(ph)) { break; } } }
public override bool Parse(PokerHand pokerHand) { var result = false; if (CardsAnalyzer.IsStraight(pokerHand.ToLong())) { result = true; pokerHand.Strength = Strength; pokerHand.Kickers.Add(new Card(CardsAnalyzer.Kickers[0])); } return result; }
public PokerHand CreateNewPokerHand(string pokerHandsName) { var hand = new PokerHand(ref _deckOfCards) { PokerHandName = pokerHandsName, PokerHandID = Guid.NewGuid() }; this.PokerHands.Add(hand); return hand; }
public void PokerHandAnalyzer_TwoPairs() { var pha = new PokerHandAnalyzer(); var ph1 = new PokerHand(new HoleCards(new Card("Ah"), new Card("4s")), new CardsCollection() {new Card("As"), new Card("3c"), new Card("8h"), new Card("4h"), new Card("6s")}); pha.AddPokerHand(ph1); var ph2 = new PokerHand(new HoleCards(new Card("Qs"), new Card("6c")), new CardsCollection() { new Card("As"), new Card("3c"), new Card("8h"), new Card("4h"), new Card("6s") }); pha.AddPokerHand(ph2); Assert.Equal(PokerHandAnalyzer.Strength.TwoPairs, pha.GetPokerHand(ph1.ToLong()).Strength); Assert.Equal(PokerHandAnalyzer.Strength.OnePair, pha.GetPokerHand(ph2.ToLong()).Strength); // TODO test de kickers, kickers of TwoPairs are not properly sets }
public void BothHandsHaveStraighFlushes() { PokerHand winningHand = new PokerHand() { PlayerName = "Winning hand." }; PokerHand losingHand = new PokerHand() { PlayerName = "Losing hand." }; Card queenOfClubs = new Card() { Rank = CardRank.Queen, Suit = CardSuit.Clubs }; Card nineOfHearts = new Card() { Rank = CardRank.Nine, Suit = CardSuit.Hearts }; winningHand.Cards.Add(new Card() { Rank = CardRank.Nine, Suit = CardSuit.Clubs }); winningHand.Cards.Add(queenOfClubs); winningHand.Cards.Add(new Card() { Rank = CardRank.Jack, Suit = CardSuit.Clubs }); winningHand.Cards.Add(new Card() { Rank = CardRank.Eight, Suit = CardSuit.Clubs}); winningHand.Cards.Add(new Card() { Rank = CardRank.Ten, Suit = CardSuit.Clubs }); losingHand.Cards.Add(nineOfHearts); losingHand.Cards.Add(new Card() { Rank = CardRank.Five, Suit = CardSuit.Hearts }); losingHand.Cards.Add(new Card() { Rank = CardRank.Eight, Suit = CardSuit.Hearts }); losingHand.Cards.Add(new Card() { Rank = CardRank.Seven, Suit = CardSuit.Hearts }); losingHand.Cards.Add(new Card() { Rank = CardRank.Six, Suit = CardSuit.Hearts }); PokerHandVerdict verdict = testJudge.Judge(winningHand, losingHand); Assert.AreEqual( PokerHandVerdictType.WinByInTypeOrdering, verdict.VerdictType, "Incorrect verdict type detected."); Assert.AreEqual( new KeyValuePair<Card, Card>(queenOfClubs, nineOfHearts), verdict.DecidingCardPair, "Incorrect deciding pair detected."); Assert.AreEqual( winningHand, verdict.WinningHand, "Incorrect winner detected."); // Sets up hands to tie and retests losingHand.Cards[0].Rank = CardRank.Queen; losingHand.Cards[1].Rank = CardRank.Jack; losingHand.Cards[2].Rank = CardRank.Ten; losingHand.Cards[3].Rank = CardRank.Nine; losingHand.Cards[4].Rank = CardRank.Eight; verdict = testJudge.Judge(winningHand, losingHand); Assert.AreEqual( PokerHandVerdictType.Tie, verdict.VerdictType, "Tie not detected correctly."); }
public void HighCardUnitTests_1() { var highCardsHands = new Dictionary<long, string>() { //{0x0008004002001000, string.Empty}, // 0000 0000 0000/1000 0000.0000 0/100.0000 0000.00/10 0000.0000 000/1.0000 0000.0000 {0x0000000000445111L, string.Empty} // 0000 0000 0000/0000 0000.0000 0/000.0000 0000.00/00 0100.0100 010/1.0001 0001.0001 }; var pht = new HighCard(); foreach (var hand in highCardsHands) { var ph = new PokerHand(hand.Key); Assert.True(pht.Parse(ph), hand.Value); Assert.Equal(PokerHandAnalyzer.Strength.HighCard, ph.Strength); } }
public void PokerHandAnalyzerUnitTest_1() { var pha = new PokerHandAnalyzer(); // 0000 0000 0000/0000 0000.0000 0/000.0000 0000.01/00 0000.0000 001/0.0000 0001.1111 pha.AddPokerHand(0x400201FL); Assert.Equal(PokerHandAnalyzer.Strength.StraightFlush, pha.GetPokerHand(0x400201FL).Strength); // 0000 0000 0000/0000 0000.0000 0/000.0000 0000.00/00 0000.0000 000/0.0000 0111.1111 var ph = new PokerHand(new HoleCards(new Card("2c"), new Card("3c")), new CardsCollection() {new Card("4c"), new Card("5c"), new Card("6c"), new Card("7c"), new Card("8c")}); pha.AddPokerHand(ph); Assert.Equal(PokerHandAnalyzer.Strength.StraightFlush, pha.GetPokerHand(0x7FL).Strength); // 0000 0000 0000/0000 0000.0000 0/000.0000 0000.00/00 0000.1111 111/0.0000 0000.0000 Assert.Equal(PokerHandAnalyzer.Strength.StraightFlush, pha.GetPokerHand(0xFE000L).Strength); // 0000 0000 0000/0000 0000.0000 0/000.0000 0000.00/00 0100.0100 010/1.0001 0001.0001 Assert.Equal(PokerHandAnalyzer.Strength.HighCard, pha.GetPokerHand(0x445111L).Strength); }
public void StraightFlush_1() { var straightFlushHands = new Dictionary<long, string>() { {0x000000000040201F, string.Empty}, // 00000000 0000//0000 0000.0000 0/000.0000 0000.00/00 0100.0000 001/0.0000 0001.1111 {0x00000000004020F8, string.Empty}, {0x00000F8000000000, string.Empty}, {0x000F800000000000, string.Empty}, {0x000000000040300F, "Hand got a `petite` straight flush i.e. 12345 "}, // 00000000 0000//0000 0000.0000 0/000.0000 0000.00/00 0100.0000 001/1.0000 0000.1111 }; var pht = new StraightFlush(); foreach (var hand in straightFlushHands) { var ph = new PokerHand(hand.Key); Assert.True(pht.Parse(ph), hand.Value); Assert.Equal(PokerHandAnalyzer.Strength.StraightFlush, ph.Strength); } }
private static bool isStraightFlush(PokerHand h) { if (isStraight(h) && isFlush(h)) return true; return false; }
// must be flush and straight and // be certain cards. No wonder I have private static bool isRoyalFlush(PokerHand h) { if (isStraight(h) && isFlush(h) && h[0].rank == RANK.Ace && h[1].rank == RANK.Ten && h[2].rank == RANK.Jack && h[3].rank == RANK.Queen && h[4].rank == RANK.King) return true; return false; }
/* * Two choices here, the first four cards * must match in rank, or the second four * must match in rank. Only because the hand * is sorted */ private static bool isFourOfAKind(PokerHand h) { if (h[0].rank == h[1].rank && h[1].rank == h[2].rank && h[2].rank == h[3].rank) return true; if (h[1].rank == h[2].rank && h[2].rank == h[3].rank && h[3].rank == h[4].rank) return true; return false; }
public void testGetHighestCardValue(string hand, int expected) { PokerHand _hand = new PokerHand(hand); Assert.AreEqual(expected, _hand.getHighestCardValue()); }
public void testWeightCombination(string hand, int expectedWeight) { PokerHand _hand = new PokerHand(hand); Assert.AreEqual(expectedWeight, _hand.getWeight()); }
public void CPUJudgeCard() { CPUJudgeHand = PokerHand.CardHand(m_cpuHand); CPUHightCardNumber = PokerHand.HighCard; }
public FullHouse(PokerHand pokerHand) : base(pokerHand) { base.Weighting = 7; }
public void HasStraightFlushTest() { PokerHand target = new PokerHand(); int minimumNumberOfCards = 0; Rank startingWithRank = Rank.SEVEN; Suit suit = Suit.HEARTS; PokerHand hand = new PokerHand(); PokerHand handExpected = new PokerHand(); bool expected = false; bool actual; actual = target.HasStraightFlush(minimumNumberOfCards, startingWithRank, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); target = new PokerHand(new Card(Suit.SPADES, Rank.ACE), new Card(Suit.SPADES, Rank.TWO), new Card(Suit.SPADES, Rank.THREE), new Card(Suit.SPADES, Rank.FOUR), new Card(Suit.SPADES, Rank.FIVE)); handExpected = new PokerHand(new Card(Suit.SPADES, Rank.ACE), new Card(Suit.SPADES, Rank.TWO), new Card(Suit.SPADES, Rank.THREE), new Card(Suit.SPADES, Rank.FOUR), new Card(Suit.SPADES, Rank.FIVE)); startingWithRank = Rank.ACE; suit = Suit.SPADES; minimumNumberOfCards = 5; expected = true; actual = target.HasStraightFlush(minimumNumberOfCards, startingWithRank, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); target = new PokerHand(new Card(Suit.SPADES, Rank.ACE), new Card(Suit.SPADES, Rank.TWO), new Card(Suit.SPADES, Rank.THREE), new Card(Suit.SPADES, Rank.FOUR), new Card(Suit.SPADES, Rank.FIVE)); handExpected = new PokerHand(); startingWithRank = Rank.ACE; suit = Suit.HEARTS; minimumNumberOfCards = 5; expected = false; actual = target.HasStraightFlush(minimumNumberOfCards, startingWithRank, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); handExpected = new PokerHand(); startingWithRank = Rank.ACE; suit = Suit.SPADES; minimumNumberOfCards = 6; expected = false; actual = target.HasStraightFlush(minimumNumberOfCards, startingWithRank, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); target = new PokerHand(new Card(Suit.SPADES, Rank.ACE), new Card(Suit.SPADES, Rank.TWO), new Card(Suit.SPADES, Rank.THREE), new Card(Suit.SPADES, Rank.FOUR), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.SPADES, Rank.FIVE)); handExpected = new PokerHand(new Card(Suit.SPADES, Rank.ACE), new Card(Suit.SPADES, Rank.TWO), new Card(Suit.SPADES, Rank.THREE), new Card(Suit.SPADES, Rank.FOUR), new Card(Suit.SPADES, Rank.FIVE), new Card(Suit.UNKNOWN, Rank.UNKNOWN)); startingWithRank = Rank.ACE; suit = Suit.SPADES; minimumNumberOfCards = 6; expected = true; actual = target.HasStraightFlush(minimumNumberOfCards, startingWithRank, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); target = new PokerHand(new Card(Suit.SPADES, Rank.ACE), new Card(Suit.SPADES, Rank.TWO), new Card(Suit.SPADES, Rank.THREE), new Card(Suit.SPADES, Rank.FOUR), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.SPADES, Rank.FIVE)); handExpected = new PokerHand(); startingWithRank = Rank.ACE; suit = Suit.SPADES; minimumNumberOfCards = 7; expected = false; actual = target.HasStraightFlush(minimumNumberOfCards, startingWithRank, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); target = new PokerHand(new Card(Suit.SPADES, Rank.ACE), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.SPADES, Rank.TWO), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.SPADES, Rank.THREE), new Card(Suit.SPADES, Rank.FOUR), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.SPADES, Rank.FIVE)); handExpected = new PokerHand(new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN)); startingWithRank = Rank.ACE; suit = Suit.CLUBS; minimumNumberOfCards = 4; expected = true; actual = target.HasStraightFlush(minimumNumberOfCards, startingWithRank, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); target = new PokerHand(new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.SPADES, Rank.TWO), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.SPADES, Rank.THREE), new Card(Suit.SPADES, Rank.FOUR), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.SPADES, Rank.FIVE)); handExpected = new PokerHand(new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.CLUBS, Rank.ACE)); startingWithRank = Rank.JACK; suit = Suit.CLUBS; minimumNumberOfCards = 4; expected = true; actual = target.HasStraightFlush(minimumNumberOfCards, startingWithRank, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); }
public void HasRoyalFlushTest() { PokerHand target = new PokerHand(); int minimumNumberOfCards = 0; Suit suit = Suit.DIAMONDS; PokerHand hand = new PokerHand(); PokerHand handExpected = new PokerHand(); bool expected = false; bool actual; actual = target.HasRoyalFlush(minimumNumberOfCards, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); minimumNumberOfCards = 1; suit = Suit.CLUBS; target = new PokerHand(new Card(Suit.CLUBS, Rank.TWO), new Card(Suit.CLUBS, Rank.THREE), new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.CLUBS, Rank.JACK), new Card(Suit.CLUBS, Rank.FIVE)); expected = true; handExpected = new PokerHand(new Card(Suit.CLUBS, Rank.ACE)); actual = target.HasRoyalFlush(minimumNumberOfCards, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); minimumNumberOfCards = 5; suit = Suit.CLUBS; target = new PokerHand(new Card(Suit.CLUBS, Rank.TWO), new Card(Suit.CLUBS, Rank.THREE), new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.CLUBS, Rank.JACK), new Card(Suit.DIAMONDS, Rank.FIVE)); expected = false; handExpected = new PokerHand(); actual = target.HasRoyalFlush(minimumNumberOfCards, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); minimumNumberOfCards = 5; suit = Suit.CLUBS; target = new PokerHand(new Card(Suit.CLUBS, Rank.TWO), new Card(Suit.UNKNOWN, Rank.THREE), new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.CLUBS, Rank.JACK), new Card(Suit.CLUBS, Rank.FIVE)); expected = false; handExpected = new PokerHand(); actual = target.HasRoyalFlush(minimumNumberOfCards, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); minimumNumberOfCards = 5; suit = Suit.CLUBS; target = new PokerHand(new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.CLUBS, Rank.JACK), new Card(Suit.UNKNOWN, Rank.UNKNOWN)); expected = true; handExpected = new PokerHand(new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.CLUBS, Rank.JACK), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN)); actual = target.HasRoyalFlush(minimumNumberOfCards, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); minimumNumberOfCards = 6; suit = Suit.CLUBS; target = new PokerHand(new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.CLUBS, Rank.JACK), new Card(Suit.UNKNOWN, Rank.UNKNOWN)); expected = false; handExpected = new PokerHand(); actual = target.HasRoyalFlush(minimumNumberOfCards, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); minimumNumberOfCards = 8; suit = Suit.CLUBS; target = new PokerHand(new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.CLUBS, Rank.JACK), new Card(Suit.UNKNOWN, Rank.UNKNOWN)); expected = true; handExpected = new PokerHand(new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.CLUBS, Rank.JACK), new Card(Suit.UNKNOWN, Rank.UNKNOWN)); actual = target.HasRoyalFlush(minimumNumberOfCards, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); minimumNumberOfCards = 4; suit = Suit.CLUBS; target = new PokerHand(new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.CLUBS, Rank.JACK), new Card(Suit.UNKNOWN, Rank.UNKNOWN)); expected = true; handExpected = new PokerHand(new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.UNKNOWN, Rank.UNKNOWN), new Card(Suit.CLUBS, Rank.JACK)); actual = target.HasRoyalFlush(minimumNumberOfCards, suit, ref hand); Assert.AreEqual(handExpected, hand); Assert.AreEqual(expected, actual); }
public void HasStraightTest1() { PokerHand target = new PokerHand(); int minimumNumberOfCards = 0; Rank beginRank = Rank.UNKNOWN; bool expected = false; bool actual; actual = target.HasStraight(minimumNumberOfCards, beginRank); Assert.AreEqual(expected, actual); target = new PokerHand(new Card(Suit.CLUBS, Rank.JACK), new Card(Suit.DIAMONDS, Rank.TWO), new Card(Suit.HEARTS, Rank.FOUR), new Card(Suit.SPADES, Rank.FIVE), new Card(Suit.CLUBS, Rank.SIX), new Card(Suit.CLUBS, Rank.SEVEN), new Card(Suit.HEARTS, Rank.UNKNOWN)); expected = false; minimumNumberOfCards = 7; beginRank = Rank.TWO; actual = target.HasStraight(minimumNumberOfCards, beginRank); Assert.AreEqual(expected, actual); expected = true; minimumNumberOfCards = 6; beginRank = Rank.TWO; actual = target.HasStraight(minimumNumberOfCards, beginRank); Assert.AreEqual(expected, actual); expected = true; minimumNumberOfCards = 4; beginRank = Rank.THREE; actual = target.HasStraight(minimumNumberOfCards, beginRank); Assert.AreEqual(expected, actual); expected = true; minimumNumberOfCards = 3; beginRank = Rank.FOUR; actual = target.HasStraight(minimumNumberOfCards, beginRank); Assert.AreEqual(expected, actual); expected = false; minimumNumberOfCards = 3; beginRank = Rank.JACK; actual = target.HasStraight(minimumNumberOfCards, beginRank); Assert.AreEqual(expected, actual); expected = true; minimumNumberOfCards = 2; beginRank = Rank.JACK; actual = target.HasStraight(minimumNumberOfCards, beginRank); Assert.AreEqual(expected, actual); target = new PokerHand(new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.DIAMONDS, Rank.TWO), new Card(Suit.HEARTS, Rank.FOUR), new Card(Suit.SPADES, Rank.FIVE), new Card(Suit.CLUBS, Rank.SIX), new Card(Suit.CLUBS, Rank.SEVEN), new Card(Suit.HEARTS, Rank.UNKNOWN)); expected = true; minimumNumberOfCards = 7; beginRank = Rank.ACE; actual = target.HasStraight(minimumNumberOfCards, beginRank); Assert.AreEqual(expected, actual); target = new PokerHand(new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.DIAMONDS, Rank.TWO), new Card(Suit.HEARTS, Rank.FOUR), new Card(Suit.SPADES, Rank.FIVE), new Card(Suit.CLUBS, Rank.SIX), new Card(Suit.CLUBS, Rank.SEVEN), new Card(Suit.HEARTS, Rank.UNKNOWN), new Card(Suit.DIAMONDS, Rank.TEN), new Card(Suit.SPADES, Rank.JACK), new Card(Suit.HEARTS, Rank.KING)); expected = true; minimumNumberOfCards = 5; beginRank = Rank.TEN; actual = target.HasStraight(minimumNumberOfCards, beginRank); Assert.AreEqual(expected, actual); target = new PokerHand(new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.DIAMONDS, Rank.TWO), new Card(Suit.HEARTS, Rank.FOUR), new Card(Suit.SPADES, Rank.FIVE), new Card(Suit.CLUBS, Rank.SIX), new Card(Suit.CLUBS, Rank.SEVEN), new Card(Suit.HEARTS, Rank.UNKNOWN), new Card(Suit.DIAMONDS, Rank.TEN), new Card(Suit.SPADES, Rank.JACK), new Card(Suit.HEARTS, Rank.KING)); expected = false; minimumNumberOfCards = 5; beginRank = Rank.NINE; actual = target.HasStraight(minimumNumberOfCards, beginRank); Assert.AreEqual(expected, actual); target = new PokerHand(new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.DIAMONDS, Rank.TEN), new Card(Suit.SPADES, Rank.JACK), new Card(Suit.HEARTS, Rank.KING), new Card(Suit.UNKNOWN, Rank.QUEEN)); expected = true; minimumNumberOfCards = 5; beginRank = Rank.TEN; actual = target.HasStraight(minimumNumberOfCards, beginRank); Assert.AreEqual(expected, actual); target = new PokerHand(new Card(Suit.CLUBS, Rank.ACE), new Card(Suit.DIAMONDS, Rank.TWO), new Card(Suit.SPADES, Rank.THREE), new Card(Suit.HEARTS, Rank.FIVE), new Card(Suit.UNKNOWN, Rank.FOUR)); expected = true; minimumNumberOfCards = 5; beginRank = Rank.ACE; actual = target.HasStraight(minimumNumberOfCards, beginRank); Assert.AreEqual(expected, actual); }
//Note that lines is only passed for efficiency, it could be obtained by just splitting handText private PokerHand parseHand(List <string> lines, string handText) { PokerHand hand = new PokerHand(); #region setup variables int start; int end; int curLine; char currencySymbol; List <Round> rounds = new List <Round>(); #endregion // Edited #region Make sure it's a PokerStars hand if (!handText.StartsWith("PokerStars")) { throw new InvalidHandFormatException(handText); } #endregion #region Skip partial hands if (lines[0].EndsWith("(partial)")) { return(null); } #endregion // Edited hand.Context.Online = true; hand.Context.Site = "PokerStars"; #if DEBUG Console.WriteLine("Hand Number"); #endif // Edited #region Get the hand number start = handText.IndexOf('#') + 1; end = handText.IndexOf(':'); hand.Context.ID = handText.Substring(start, end - start); //Console.WriteLine("ID: {0}", hand.Context.ID); if (printed < 2) { Console.WriteLine("Hand Text: {0}", handText); printed++; } #endregion #if DEBUG Console.WriteLine("Table Name"); #endif // Edited #region Get the table name start = lines[1].IndexOf('\''); end = lines[1].IndexOf('\'', 7); hand.Context.Table = (lines[1].Substring(start + 1, end - start - 1)); #endregion #if DEBUG Console.WriteLine("Blinds"); #endif // Edited #region Get the blinds, antes and currency start = handText.IndexOf('('); end = handText.IndexOf('/'); string smallBlind = handText.Substring(start + 1, end - start - 1); start = end; end = handText.IndexOf(' ', start); string bigBlind = handText.Substring(start + 1, end - start - 1); if (smallBlind[0].Equals('$') || smallBlind[0].Equals('€')) { if (smallBlind[0].Equals('$')) { hand.Context.Currency = "USD"; currencySymbol = '$'; } else { hand.Context.Currency = "EUR"; currencySymbol = '€'; } hand.Context.SmallBlind = Decimal.Parse(smallBlind.Substring(1).Trim()); hand.Context.BigBlind = Decimal.Parse(bigBlind.Substring(1).Trim()); } else { currencySymbol = 'T'; hand.Context.SmallBlind = Decimal.Parse(smallBlind.Trim()); hand.Context.BigBlind = Decimal.Parse(bigBlind.Substring(0, bigBlind.Length - 1).Trim()); } // Antes are not written on the first line in PokerStars hand histories // Ante amount will have to be extracted from post blind lines. // Smallest possible post blind line index is 4 curLine = 4; while (!lines[curLine].Equals("*** HOLE CARDS ***")) { if (lines[curLine].Contains(" posts the ante ")) { start = lines[curLine].IndexOf("ante"); string anteText = lines[curLine].Substring(start + 5); if (anteText[0].Equals(currencySymbol)) { anteText = anteText.Substring(1); } hand.Context.Ante = Decimal.Parse(anteText.Trim()); break; } curLine++; } #endregion #if DEBUG Console.WriteLine("Game Format"); #endif #region Get the game format // Stars does not have different notations for Sit&Go's and MTTs. // All tournament hand histories are of the same format. if (currencySymbol.Equals('T')) { if (lines[0].Contains(": Tournament #")) { hand.Context.Format = GameFormat.Tournament; } else { hand.Context.Format = GameFormat.PlayMoney; } } else { hand.Context.Format = GameFormat.CashGame; } #endregion #if DEBUG Console.WriteLine("Poker Variant and Betting Type"); #endif #region Get the betting type and poker variant if (hand.Context.Format == GameFormat.CashGame && handText.Contains(" Cap - ")) { start = handText.IndexOf(" - ") + 3; end = handText.IndexOf(" Cap "); string capAmountText = handText.Substring(start, end - start); hand.Context.CapAmount = Decimal.Parse(capAmountText.Substring(1).Trim()); hand.Context.CapAmountSpecified = true; hand.Context.Capped = true; } else { hand.Context.CapAmountSpecified = false; hand.Context.Capped = false; } string typeAndVariant; if (hand.Context.Format == GameFormat.CashGame) { start = handText.IndexOf(": ") + 3; end = handText.IndexOf(" (", start); typeAndVariant = handText.Substring(start, end - start); } else { start = handText.IndexOf(hand.Context.Currency) + 4; end = handText.IndexOf(" - ", start); typeAndVariant = handText.Substring(start, end - start); } if (typeAndVariant.Contains("Pot Limit")) { hand.Context.BettingType = BettingType.PotLimit; } else if (typeAndVariant.Contains("No Limit")) { hand.Context.BettingType = BettingType.NoLimit; } else { hand.Context.BettingType = BettingType.FixedLimit; } if (typeAndVariant.Contains("Hold'em")) { hand.Context.PokerVariant = PokerVariant.TexasHoldEm; } else if (typeAndVariant.Contains("Omaha Hi")) { hand.Context.PokerVariant = PokerVariant.OmahaHi; } else { hand.Context.PokerVariant = PokerVariant.OmahaHiLo; } #endregion #if DEBUG Console.WriteLine("Time Stamp"); #endif #region Get the date and time start = lines[0].LastIndexOf(" - ") + 3; end = lines[0].IndexOf(' ', start); string dateText = lines[0].Substring(start, end - start); string[] dateTokens = dateText.Split('/'); int year = Int32.Parse(dateTokens[0]); int month = Int32.Parse(dateTokens[1]); int day = Int32.Parse(dateTokens[2]); start = end; end = lines[0].IndexOf(' ', start + 1); string timeText = lines[0].Substring(start, end - start); string[] timeTokens = timeText.Split(':'); int hour = Int32.Parse(timeTokens[0]); int minute = Int32.Parse(timeTokens[1]); int second = Int32.Parse(timeTokens[2]); hand.Context.TimeStamp = new DateTime(year, month, day, hour, minute, second); #endregion #if DEBUG Console.WriteLine("Players"); #endif #region Create the players List <Player> players = new List <Player>(); curLine = 2; for (Match m = stacksExp.Match(lines[curLine]); m.Success; m = stacksExp.Match(lines[curLine])) { if (m.Success) { GroupCollection gc = m.Groups; Player p = new Player(); p.Seat = Int32.Parse(gc[1].Value); p.Name = gc[2].Value; p.Stack = Decimal.Parse(gc[4].Value); players.Add(p); curLine++; } } hand.Players = players.ToArray(); #endregion #if DEBUG Console.WriteLine("Blinds and Antes Posted"); #endif #region Terminate parsing of unsupported poker type if ((!typeAndVariant.Contains("Hold'em") && (!typeAndVariant.Contains("Omaha")))) { return(hand); } #endregion #region Get the blinds and antes posted List <Blind> blinds = new List <Blind>(); for (; !lines[curLine].StartsWith("*** HOLE CARDS ***"); curLine++) { for (Match m = actionExp.Match(lines[curLine]); m.Success; m = m.NextMatch()) { GroupCollection gc = m.Groups; Blind blind = new Blind(); blind.Player = gc[1].Value; String a = gc[2].Value + "" + gc[3].Value + "" + gc[4].Value + "" + gc[5].Value + "" + gc[6].Value + "" + gc[7].Value + "" + gc[8].Value + "" + gc[9].Value + "" + gc[10].Value + "" + gc[11].Value + "" + gc[12].Value; if (gc[2].Value.Contains("ante")) { blind.Type = BlindType.Ante; } else if (gc[2].Value.Contains("small")) { blind.Type = BlindType.SmallBlind; } else if (gc[2].Value.Contains("big")) { blind.Type = BlindType.BigBlind; } else { throw new Exception("Unknown blind type: " + lines[curLine]); } if (lines[curLine].Contains("dead")) { for (int i = 0; i < gc.Count; i++) { Console.WriteLine("{0}: \"{1}\"", i, gc[i].Value); } Console.WriteLine("Found as {0}", blind.Type.ToString()); } blind.Amount = Decimal.Parse(gc[4].Value); blind.AllIn = !gc[12].Value.Equals(""); blinds.Add(blind); } } hand.Blinds = blinds.ToArray(); #endregion #if DEBUG Console.WriteLine("Button"); #endif #region Get the button string buttonText = lines[1].Substring(lines[1].IndexOf('#') + 1, 1); hand.Context.Button = Int32.Parse(buttonText); curLine++; #endregion #if DEBUG Console.WriteLine("Hole Cards and Hero"); #endif #region Get the hole cards and the name of the hero int tempIndex = curLine; bool heroType = true; while (!lines[curLine].StartsWith("Dealt to ")) { if (lines[curLine].Equals("*** FLOP ***") || lines[curLine].Equals("*** SUMMARY ***")) { curLine = tempIndex; heroType = false; break; } else { curLine++; } } if (heroType) { start = "Dealt to ".Length; end = lines[curLine].IndexOf(" [", start); hand.Hero = lines[curLine].Substring(start, end - start); start = end + 2; List <Card> holecards = new List <Card>(); holecards.Add(new Card(lines[curLine].Substring(start, 2))); holecards.Add(new Card(lines[curLine].Substring(start + 3, 2))); if (hand.Context.PokerVariant != PokerVariant.TexasHoldEm) { holecards.Add(new Card(lines[curLine].Substring(start + 6, 2))); holecards.Add(new Card(lines[curLine].Substring(start + 9, 2))); } hand.HoleCards = holecards.ToArray(); curLine++; } #endregion #if DEBUG Console.WriteLine("Preflop Actions"); #endif #region Preflop Actions rounds.Add(new Round()); rounds[0].Actions = getRoundActions(lines, ref curLine); #endregion #if DEBUG Console.WriteLine("Flop Actions"); #endif #region Flop Actions and Community Cards if (lines[curLine].StartsWith("*** FLOP ***")) { rounds.Add(new Round()); start = lines[curLine].IndexOf('[') + 1; Card[] flop = new Card[3]; flop[0] = new Card(lines[curLine].Substring(start, 2)); flop[1] = new Card(lines[curLine].Substring(start + 3, 2)); flop[2] = new Card(lines[curLine].Substring(start + 6, 2)); rounds[1].CommunityCards = flop; curLine++; rounds[1].Actions = getRoundActions(lines, ref curLine); } #endregion #if DEBUG Console.WriteLine("Turn Actions"); #endif #region Turn Actions and Community Card if (lines[curLine].StartsWith("*** TURN ***")) { rounds.Add(new Round()); start = lines[curLine].LastIndexOf('[') + 1; Card[] turn = new Card[1]; turn[0] = new Card(lines[curLine].Substring(start, 2)); rounds[2].CommunityCards = turn; curLine++; rounds[2].Actions = getRoundActions(lines, ref curLine); } #endregion #if DEBUG Console.WriteLine("River Actions"); #endif #region River Actions and Community Card if (lines[curLine].StartsWith("*** RIVER ***")) { rounds.Add(new Round()); start = lines[curLine].LastIndexOf('[') + 1; Card[] river = new Card[1]; river[0] = new Card(lines[curLine].Substring(start, 2)); rounds[3].CommunityCards = river; curLine++; rounds[3].Actions = getRoundActions(lines, ref curLine); } #endregion #region Set rounds hand.Rounds = rounds.ToArray(); #endregion #if DEBUG Console.WriteLine("Results"); #endif #region Get pots won List <HandResult> results = new List <HandResult>(); List <List <Pot> > pots = new List <List <Pot> >(); for (; !lines[curLine].StartsWith("*** SUMMARY"); curLine++) { Match m = potsExp.Match(lines[curLine]); if (m.Success) { GroupCollection gc = m.Groups; Pot p = new Pot(); p.Amount = Decimal.Parse(gc[3].Value); if (gc[7].Value.Length > 0) { p.Number = Int32.Parse(gc[7].Value.Substring(1)); } else if ((gc[5].Value.Length == 0 && gc[3].Value == "the pot") || gc[5].Value == "main ") { p.Number = 0; } else if (gc[5].Length > 0) { p.Number = 1; } HandResult result = null; List <Pot> potList = null; for (int i = 0; i < results.Count; i++) { if (results[i].Player == gc[1].Value) { result = results[i]; potList = pots[i]; break; } } if (result == null) { result = new HandResult(gc[1].Value); potList = new List <Pot>(); results.Add(result); pots.Add(potList); } potList.Add(p); } } //add the pots to the model for (int i = 0; i < results.Count; i++) { results[i].WonPots = pots[i].ToArray(); } //get the rake, if any if (hand.Context.Format == GameFormat.CashGame) { for (; !lines[curLine].StartsWith("Total pot") || lines[curLine].Contains(":") || !lines[curLine].Contains("| Rake "); curLine++) { } int rakeStart = lines[curLine].LastIndexOf("| Rake ") + "| Rake ".Length; string rakeText = lines[curLine].Substring(rakeStart).Replace(currencySymbol + "", ""); hand.Rake = Decimal.Parse(rakeText.Trim()); } #endregion #if DEBUG Console.WriteLine("Shown Hands"); #endif #region Get the shown down hands for (; curLine < lines.Count; curLine++) { Match m = shownHandsExp.Match(lines[curLine]); if (m.Success) { GroupCollection gc = m.Groups; List <Card> shownCards = new List <Card>(); shownCards.Add(new Card(gc[5].Value)); shownCards.Add(new Card(gc[6].Value)); if (hand.Context.PokerVariant != PokerVariant.TexasHoldEm) { shownCards.Add(new Card(gc[8].Value)); shownCards.Add(new Card(gc[9].Value)); } string player = gc[2].Value; HandResult hr = null; foreach (HandResult curResult in results) { if (curResult.Player == player) { hr = curResult; break; } } if (hr == null) { hr = new HandResult(player); results.Add(hr); } hr.HoleCards = shownCards.ToArray(); } } #endregion #region Set the results hand.Results = results.ToArray(); #endregion #if DEBUG Console.WriteLine("Done"); #endif return(hand); }
public PokerHand ToXmlHand() { PokerHand hand = new PokerHand() { Hero = players[Hero].Name }; #region Convert blinds var blindPostings = predealActions.Where(s => s.ActionType == Action.ActionTypes.PostSmallBlind || s.ActionType == Action.ActionTypes.PostBigBlind || s.ActionType == Action.ActionTypes.PostAnte); hand.Blinds = new Blind[blindPostings.Count()]; for (int i = 0; i < hand.Blinds.Length; i++) { var blindPosting = blindPostings.ElementAt(i); hand.Blinds[i] = new Blind() { AllIn = blindPosting.AllIn, Amount = (decimal)blindPosting.Amount, Player = blindPosting.Name, Type = blindPosting.ActionType == Action.ActionTypes.PostBigBlind ? BlindType.BigBlind : blindPosting.ActionType == Action.ActionTypes.PostSmallBlind ? BlindType.SmallBlind : blindPosting.ActionType == Action.ActionTypes.PostAnte ? BlindType.Ante : BlindType.None }; } #endregion #region Context hand.Context = new Context(); hand.Context.Ante = (decimal)Ante; #region Betting Structure if (BettingStructure == holdem_engine.BettingStructure.None) { throw new Exception("Unknown betting structure"); } hand.Context.BettingType = this.BettingStructure == holdem_engine.BettingStructure.Limit ? BettingType.FixedLimit : BettingStructure == holdem_engine.BettingStructure.NoLimit ? BettingType.NoLimit : BettingType.PotLimit; #endregion hand.Context.BigBlind = (decimal)BigBlind; hand.Context.Button = (int)Button; hand.Context.CapAmount = 0; // no caps supported hand.Context.CapAmountSpecified = false; hand.Context.Capped = false; hand.Context.Currency = "$"; hand.Context.Format = GameFormat.CashGame; hand.Context.ID = HandNumber.ToString(); hand.Context.Online = false; hand.Context.PokerVariant = PokerVariant.TexasHoldEm; hand.Context.Site = "SimulatedPokerSite"; hand.Context.SmallBlind = (decimal)SmallBlind; hand.Context.Table = TableName; hand.Context.TimeStamp = DateTime.Now; #endregion #region HoleCards hand.HoleCards = HoldemHand.Hand.Cards(HoleCards[Hero]) .Select(c => new PokerHandHistory.Card(c)) .ToArray(); #endregion hand.Rake = 0m; hand.Players = this.Players.Select((s, i) => new Player() { Name = s.Name, Seat = s.SeatNumber, Stack = (decimal)StartingChips[i] }).ToArray(); #region Rounds (actions and community cards) hand.Rounds = new PokerHandHistory.Round[Math.Min(4, (int)this.CurrentRound)]; if (CurrentRound >= Round.Preflop) { hand.Rounds[0] = new PokerHandHistory.Round(); hand.Rounds[0].Actions = PreflopActions.Select(a => convertActionToXml(a)).ToArray(); if (CurrentRound >= Round.Flop) { hand.Rounds[1] = new PokerHandHistory.Round(); hand.Rounds[1].Actions = FlopActions.Select(a => convertActionToXml(a)).ToArray(); hand.Rounds[1].CommunityCards = HoldemHand.Hand.Cards(Flop) .Select(c => new PokerHandHistory.Card(c)) .ToArray(); if (CurrentRound >= Round.Turn) { hand.Rounds[2] = new PokerHandHistory.Round(); hand.Rounds[2].Actions = TurnActions.Select(a => convertActionToXml(a)).ToArray(); hand.Rounds[2].CommunityCards = HoldemHand.Hand.Cards(Turn) .Select(c => new PokerHandHistory.Card(c)) .ToArray(); if (CurrentRound >= Round.River) { hand.Rounds[3] = new PokerHandHistory.Round(); hand.Rounds[3].Actions = RiverActions.Select(a => convertActionToXml(a)).ToArray(); hand.Rounds[3].CommunityCards = HoldemHand.Hand.Cards(River) .Select(c => new PokerHandHistory.Card(c)) .ToArray(); } } } } #endregion #region Results if (Winners != null && Winners.Count() > 0) { hand.Results = new HandResult[players.Length]; //int potNum = 0; for (int i = 0; i < hand.Results.Length; i++) { HandResult hr = new HandResult(players[i].Name); hr.HoleCards = HoldemHand.Hand.Cards(HoleCards[i]) .Select(c => new PokerHandHistory.Card(c)) .ToArray(); var wins = winners.Where(w => w.Player == hr.Player); if (wins != null) { hr.WonPots = wins.Select(w => new PokerHandHistory.Pot() { Amount = (decimal)w.Amount, //Number = potNum++ // not able to handle this properly currently. }).ToArray(); } hand.Results[i] = hr; } } #endregion return(hand); }
public FourOfAKind(PokerHand pokerHand) : base(pokerHand) { base.Weighting = 8; }
/// <summary> /// So sánh 2 chi /// </summary> public static Winner CmpHands(CardCollection hand1, CardCollection hand2, PokerHand hand, CompareSuitType type) { return(Winner.NoWin); }
public override bool IsValidScoreForHand(PokerHand hand) { return IsStraight(hand.Cards); }
protected bool ContainsXofSameKind(PokerHand pokerHand, int num) { var groups = pokerHand.GroupBy(c => c.Value); return(groups.Any(g => g.Count() == num)); }
public void Initialize() { shuffledDeck = new Deck().Shuffle <Deck, Card>(); highCardHand = new PokerHand { new Card(Rank.Two, Suit.Hearts), new Card(Rank.Three, Suit.Spades), new Card(Rank.Four, Suit.Spades), new Card(Rank.Six, Suit.Clubs), new Card(Rank.Eight, Suit.Diamonds), }; pairHand = new PokerHand { new Card(Rank.Two, Suit.Hearts), new Card(Rank.Two, Suit.Spades), new Card(Rank.Four, Suit.Spades), new Card(Rank.Six, Suit.Clubs), new Card(Rank.Eight, Suit.Diamonds), }; twoPairHand = new PokerHand { new Card(Rank.Two, Suit.Hearts), new Card(Rank.Two, Suit.Spades), new Card(Rank.Three, Suit.Hearts), new Card(Rank.Three, Suit.Spades), new Card(Rank.Eight, Suit.Diamonds), }; threeOfAKindHand = new PokerHand { new Card(Rank.Two, Suit.Hearts), new Card(Rank.Two, Suit.Spades), new Card(Rank.Two, Suit.Clubs), new Card(Rank.Six, Suit.Clubs), new Card(Rank.Eight, Suit.Diamonds), }; straightHand = new PokerHand { new Card(Rank.Two, Suit.Hearts), new Card(Rank.Three, Suit.Hearts), new Card(Rank.Four, Suit.Hearts), new Card(Rank.Five, Suit.Hearts), new Card(Rank.Six, Suit.Spades), }; flushHand = new PokerHand { new Card(Rank.Two, Suit.Hearts), new Card(Rank.Three, Suit.Hearts), new Card(Rank.Four, Suit.Hearts), new Card(Rank.Five, Suit.Hearts), new Card(Rank.Seven, Suit.Hearts), }; fullHouseHand = new PokerHand { new Card(Rank.Three, Suit.Hearts), new Card(Rank.Three, Suit.Spades), new Card(Rank.Two, Suit.Hearts), new Card(Rank.Two, Suit.Spades), new Card(Rank.Two, Suit.Diamonds), }; fourOfAKindHand = new PokerHand { new Card(Rank.Two, Suit.Hearts), new Card(Rank.Two, Suit.Spades), new Card(Rank.Two, Suit.Diamonds), new Card(Rank.Two, Suit.Clubs), new Card(Rank.Eight, Suit.Diamonds), }; straightFlushHand = new PokerHand { new Card(Rank.Two, Suit.Hearts), new Card(Rank.Three, Suit.Hearts), new Card(Rank.Four, Suit.Hearts), new Card(Rank.Five, Suit.Hearts), new Card(Rank.Six, Suit.Hearts), }; }
public static void PlayPoker() { List <string> Name = new List <string>(); PlayPokerGame PokerGameFirst = new PlayPokerGame(); for (int i = 1; i < 3; i++) { Console.WriteLine("Please Enter Name of Player Number {0}", i); string name = Console.ReadLine(); Name.Add(name); } for (int i = 0; i < Name.Count; i++) { PokerPlayer player = new PokerPlayer(Name[i]); PokerGameFirst.AddPlayer(player); Console.WriteLine(""); Console.WriteLine("Card No for {0}", Name[i]); Console.WriteLine("----------------------------------------------------------------------"); Console.WriteLine("Ranktype: Please Enter Rank Type for: {0} ", Name[i]); Console.WriteLine("For Rank Type Please Choose Five from the Following List"); Console.WriteLine("List: Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten"); Console.WriteLine("List: Jack, Queen, King, Ace, None"); Console.WriteLine("EXAMPLE TO TYPE IN THE CONSOLE: *** Jack, Queen, King, Ace, Five *** "); Console.WriteLine("-----------------------------------------------------------------------"); List <int> RankType = new List <int>(); List <int> SuitsType = new List <int>(); string valueRank = Console.ReadLine(); string[] testValueRank = UtilityClass.SplitString(valueRank); for (int k = 0; k < testValueRank.Length; k++) { UtilityClass.ParseRank(RankType, testValueRank, k); } Console.WriteLine(""); Console.WriteLine("-------------------------------------------------------------------------------"); Console.WriteLine("Suit Card: Please Enter Suit Card for: {0}", Name[i]); Console.WriteLine("For Suit Card Please Choose FIVE From the Following List"); Console.WriteLine("List: Diamond, Heart, Spades, Clubs, None"); Console.WriteLine("EXAMPLE TO TYPE IN THE CONSOLE: **** Diamond, Spades, Heart, Heart, Clubs *** "); Console.WriteLine("---------------------------------------------------------------------------------"); Console.WriteLine(""); string valueForType = Console.ReadLine(); string[] SuitTypeValue = UtilityClass.SplitString(valueForType); UtilityClass.ParseSuit(SuitsType, SuitTypeValue); for (int m = 0; m < 4; m++) { int ranktype = RankType[m]; int suitType = SuitsType[m]; RankType rankForPlayingCard = (RankType)Enum.ToObject(typeof(RankType), ranktype); SuitCard suit = (SuitCard)Enum.ToObject(typeof(SuitCard), suitType); PokerGameFirst.GivePlayingCards(player, new PlayingCard { Rank = rankForPlayingCard, Suit = suit }); } } Console.WriteLine(""); PokerHand firstWinner = PokerGameFirst.GetWinner(); Console.WriteLine("Winner for the first game is: {0} using {1}", firstWinner.Name, firstWinner.Category.ToString()); Console.Read(); }
//Note that lines is only passed for efficiency, it could be obtained //by just splitting handText private PokerHand parseHand(List <string> lines, string handText) { PokerHand hand = new PokerHand(); #region setup variables int start; int curLine; List <Round> rounds = new List <Round>(); #endregion #region Make sure it's a Party Poker hand if (!handText.StartsWith(HAND_START_TEXT)) { throw new InvalidHandFormatException(handText); } #endregion hand.Context.Online = true; hand.Context.Site = Name; #if DEBUG Console.WriteLine("Hand Number"); #endif #region Get the hand number hand.Context.ID = gameNumExp.Match(handText).Groups[1].Value; //Console.WriteLine("ID: {0}", hand.Context.ID); if (printed < 200) { Console.WriteLine("Hand Text: {0}", handText); printed++; } #endregion #if DEBUG Console.WriteLine("Hand Number: {0}", hand.Context.ID); Console.WriteLine("Table Name"); #endif #region Get the table name hand.Context.Table = tableNameExp.Match(handText).Groups[1].Value; #endregion #if DEBUG Console.WriteLine(hand.Context.Table); Console.WriteLine("Blinds"); #endif #region Get the blinds and game format var stakesAndDate = stakesAndDateExp.Match(lines.First(line => line.StartsWith("$"))); hand.Context.SmallBlind = decimal.Parse(stakesAndDate.Groups[1].Value) / 2.0m; // All old pp stakesAndDate were sb = 0.5 * bb hand.Context.BigBlind = decimal.Parse(stakesAndDate.Groups[1].Value); // Assume no ante. #endregion #if DEBUG Console.WriteLine("Small Blind: {0}", hand.Context.SmallBlind); Console.WriteLine("Big Blind: {0}", hand.Context.BigBlind); Console.WriteLine("Game Format"); #endif #region Get the game format // TODO: Support more than cash games hand.Context.Format = GameFormat.CashGame; #endregion #if DEBUG Console.WriteLine("Poker Variant and Betting Type"); #endif #region Get the betting type and poker variant // Assume playing fixed limit Texas Hold'em hand.Context.CapAmountSpecified = false; hand.Context.Capped = false; hand.Context.BettingType = BettingType.FixedLimit; hand.Context.PokerVariant = PokerVariant.TexasHoldEm; #endregion #if DEBUG Console.WriteLine("Time Stamp"); #endif #region Get the date and time int year = int.Parse(stakesAndDate.Groups[14].Value); int month = monthToInt(stakesAndDate.Groups[8].Value); int day = int.Parse(stakesAndDate.Groups[9].Value); int hour = int.Parse(stakesAndDate.Groups[10].Value); int minute = int.Parse(stakesAndDate.Groups[11].Value); int second = int.Parse(stakesAndDate.Groups[12].Value); string timeZone = stakesAndDate.Groups[13].Value;// Note: Currently not considered. hand.Context.TimeStamp = new DateTime(year, month, day, hour, minute, second); #endregion #if DEBUG Console.WriteLine("DateTime: {0}", hand.Context.TimeStamp.ToString()); Console.WriteLine("Players"); #endif #region Create the players List <Player> players = new List <Player>(); curLine = 5; for (Match m = stacksExp.Match(lines[curLine]); m.Success; m = stacksExp.Match(lines[++curLine])) { if (m.Success) { GroupCollection gc = m.Groups; Player p = new Player(); p.Seat = Int32.Parse(gc[1].Value); p.Name = gc[2].Value; p.Stack = Decimal.Parse(gc[3].Value); players.Add(p); } } hand.Players = players.ToArray(); #endregion #if DEBUG foreach (var player in hand.Players) { Console.WriteLine("Seat: {0} Name: {1} Chips: {2}", player.Seat, player.Name, player.Stack); } Console.WriteLine("Blinds and Antes Posted"); #endif #region Get the blinds and antes posted List <Blind> blinds = new List <Blind>(); for (; lines[curLine] != "** Dealing down cards **"; curLine++) { Match m = actionExp.Match(lines[curLine]); if (!m.Success) { continue; } //throw new InvalidHandFormatException("Hand " + hand.Context.ID + ". Unknown blind or ante: " + lines[curLine]); GroupCollection gc = m.Groups; Blind blind = new Blind(); blind.Player = gc[1].Value; if (gc[2].Value == "posts the ante") { blind.Type = BlindType.Ante; } else if (gc[2].Value == "posts small blind") { blind.Type = BlindType.SmallBlind; } else if (gc[2].Value == "posts big blind") { blind.Type = BlindType.BigBlind; } else if (gc[2].Value.StartsWith("posts")) { blind.Type = BlindType.LateBlind; } else { throw new Exception("Unknown blind type: " + lines[curLine]); } //TODO: Handle dead and late blinds appropriately blind.Amount = Decimal.Parse(gc[4].Value); // TODO: Handle all-in blind posts //blind.AllIn = gc[9].Value.Length == 15; blind.AllIn = false; blinds.Add(blind); } hand.Blinds = blinds.ToArray(); #endregion #if DEBUG foreach (var blind in hand.Blinds) { Console.WriteLine("Player: {0} Amount: {1} Type: {2} All-in: {3}", blind.Player, blind.Amount, blind.Type, blind.AllIn); } Console.WriteLine("Button"); #endif #region Get the hole cards and the name of the hero Match hcMatch = holeCardsExp.Match(handText); if (hcMatch.Success) { List <Card> holecards = new List <Card>(); hand.Hero = hcMatch.Groups[1].Value; holecards.Add(new Card(hcMatch.Groups[2].Value)); holecards.Add(new Card(hcMatch.Groups[3].Value)); hand.HoleCards = holecards.ToArray(); //TODO: Handle Omaha. } #endregion #if DEBUG if (hcMatch.Success) { Console.WriteLine("Hero: {0} HoleCard1: {1} HoleCard2: {2}", hand.Hero, hand.HoleCards[0], hand.HoleCards[1]); } Console.WriteLine("Preflop Actions"); #endif #region Preflop Actions curLine = lines.IndexOf("** Dealing down cards **") + (hand.Hero == null ? 1 : 2); rounds.Add(new Round()); rounds[0].Actions = getRoundActions(hand, rounds, lines, ref curLine); #endregion #if DEBUG if (rounds[0].CommunityCards != null) { foreach (var card in rounds[0].CommunityCards) { Console.WriteLine("Preflop Card: {0}", card); } } foreach (var action in rounds[0].Actions) { Console.WriteLine("Preflop Player: {0} Action: {1} Amount: {2} All-In: {3}", action.Player, action.Type, action.Amount, action.AllIn); } Console.WriteLine("Flop Actions"); #endif #region Get the button // F**k the button statement. Hand histories are buggy with this number. // hand.Context.Button = Int32.Parse(buttonExp.Match(handText).Groups[1].Value); // curLine++; // Validate the button -- It is apparently buggy in some hand histories. var blindNames = hand.Blinds.Where(b => b.Type == BlindType.SmallBlind || b.Type == BlindType.BigBlind).Select(b => b.Player); var buttonName = rounds[0].Actions[0].Player; List <string> actedAlready = new List <string>(); actedAlready.AddRange(blindNames); actedAlready.Add(buttonName); for (int i = 1; i < rounds[0].Actions.Length; i++) { string actor = rounds[0].Actions[i].Player; if (!actedAlready.Contains(actor)) { buttonName = actor; actedAlready.Add(actor); } } hand.Context.Button = hand.Players.First(p => p.Name == buttonName).Seat; #endregion #if DEBUG Console.WriteLine("Button: {0}", hand.Context.Button); Console.WriteLine("Hole Cards and Hero"); #endif #region Flop Actions and Community Cards if (lines[curLine].StartsWith("** Dealing Flop **")) { rounds.Add(new Round()); start = lines[curLine].IndexOf('[') + 2; Card[] flop = new Card[3]; flop[0] = new Card(lines[curLine].Substring(start, 2)); flop[1] = new Card(lines[curLine].Substring(start + 4, 2)); flop[2] = new Card(lines[curLine].Substring(start + 8, 2)); rounds[1].CommunityCards = flop; curLine++; rounds[1].Actions = getRoundActions(hand, rounds, lines, ref curLine); } #endregion #if DEBUG if (rounds.Count > 1) { foreach (var card in rounds[1].CommunityCards) { Console.WriteLine("Flop Card: {0}", card); } foreach (var action in rounds[1].Actions) { Console.WriteLine("Flop Player: {0} Action: {1} Amount: {2} All-In: {3}", action.Player, action.Type, action.Amount, action.AllIn); } } Console.WriteLine("Turn Actions"); #endif #region Turn Actions and Community Card if (lines[curLine].StartsWith("** Dealing Turn **")) { rounds.Add(new Round()); start = lines[curLine].LastIndexOf('[') + 2; Card[] turn = new Card[1]; turn[0] = new Card(lines[curLine].Substring(start, 2)); rounds[2].CommunityCards = turn; curLine++; rounds[2].Actions = getRoundActions(hand, rounds, lines, ref curLine); } #endregion #if DEBUG if (rounds.Count > 2) { foreach (var card in rounds[2].CommunityCards) { Console.WriteLine("Turn Card: {0}", card); } foreach (var action in rounds[2].Actions) { Console.WriteLine("Turn Player: {0} Action: {1} Amount: {2} All-In: {3}", action.Player, action.Type, action.Amount, action.AllIn); } } Console.WriteLine("River Actions"); #endif #region River Actions and Community Card if (lines[curLine].StartsWith("** Dealing River **")) { rounds.Add(new Round()); start = lines[curLine].LastIndexOf('[') + 2; Card[] river = new Card[1]; river[0] = new Card(lines[curLine].Substring(start, 2)); rounds[3].CommunityCards = river; curLine++; rounds[3].Actions = getRoundActions(hand, rounds, lines, ref curLine); } #endregion #region Set rounds hand.Rounds = rounds.ToArray(); #endregion #if DEBUG if (rounds.Count > 3) { foreach (var card in rounds[3].CommunityCards) { Console.WriteLine("River Card: {0}", card); } foreach (var action in rounds[3].Actions) { Console.WriteLine("River Player: {0} Action: {1} Amount: {2} All-In: {3}", action.Player, action.Type, action.Amount, action.AllIn); } } Console.WriteLine("Shown Hands"); #endif List <HandResult> results = new List <HandResult>(); #region Get the shown down hands for (Match match = shownHandsExp.Match(handText); match.Success; match = match.NextMatch()) { GroupCollection gc = match.Groups; if (!hand.Players.Select(p => p.Name).Contains(gc[1].Value)) { continue; } HandResult hr = new HandResult(gc[1].Value); if (gc[2].Value != "does not show cards") { hr.HoleCards = new Card[] { new Card(gc[4].Value), new Card(gc[5].Value) } } ; results.Add(hr); } #endregion #if DEBUG foreach (var hr in results) { Console.WriteLine("Player: {0} Cards: {1}", hr.Player, hr.HoleCards == null ? "" : hr.HoleCards[0].ToString() + " " + hr.HoleCards[1].ToString()); } #endif #region Get pots won List <List <Pot> > pots = new List <List <Pot> >(); for (int i = 0; i < results.Count; i++) { pots.Add(new List <Pot>()); } for (; curLine < lines.Count; curLine++) { Match m = potsExp.Match(lines[curLine]); if (m.Success) { GroupCollection gc = m.Groups; if (!hand.Players.Select(player => player.Name).Contains(gc[1].Value)) { continue; } Pot p = new Pot(); p.Amount = Decimal.Parse(gc[2].Value); if (gc[6].Value == "" || gc[6].Value == "the main pot") { p.Number = 0; } else { p.Number = int.Parse(gc[7].Value); } HandResult result = null; List <Pot> potList = null; for (int i = 0; i < results.Count; i++) { if (results[i].Player == gc[1].Value) { result = results[i]; potList = pots[i]; break; } } //if (result == null) //{ // result = new HandResult(gc[1].Value); // potList = new List<Pot>(); // results.Add(result); // pots.Add(potList); //} potList.Add(p); } } //add the pots to the model for (int i = 0; i < results.Count; i++) { results[i].WonPots = pots[i].ToArray(); } // Set the results hand.Results = results.ToArray(); #if DEBUG foreach (var result in hand.Results) { foreach (var pot in result.WonPots) { Console.WriteLine("Pot: {0} Player: {1} Amount: {2}", pot.Number, result.Player, pot.Amount); } } Console.WriteLine("Calculating rake"); #endif //get the rake, if any if (hand.Context.Format == GameFormat.CashGame) { hand.Rake = hand.Rounds.Sum(r => r.Actions.Sum(act => act.Amount)) + hand.Blinds.Sum(b => b.Amount) - hand.Results.Sum(r => r.WonPots.Sum(w => w.Amount)); } #endregion #if DEBUG Console.WriteLine("Rake: {0}", hand.Rake); Console.WriteLine("Done"); #endif return(hand); }
public abstract bool IsSatisfiedBy(PokerHand pokerHand);
public void testGetHandCombination(string hand, int expected) { PokerHand _hand = new PokerHand(hand); Assert.AreEqual(expected, _hand.getCombination()); }
public ThreeOfAKind(PokerHand pokerHand) : base(pokerHand) { base.Weighting = 4; }
// public static PokerType GetHandType(CardCollection input, out CardCollection output, PokerHand hand) { output = input; return(PokerType.Type_Unknown); }
public int CompareTo(Hand theirs) { if (theirs == null) { return(1); } var _myRanking = GetRanking(); var _theirRanking = theirs.GetRanking(); PokerHand myRanking = _myRanking.Item1; List <Card> myRankingCards = _myRanking.Item2; PokerHand theirRanking = _theirRanking.Item1; List <Card> theirRankingCards = _theirRanking.Item2; var myRanks = myRankingCards.GroupBy(c => c.Rank); var theirRanks = theirRankingCards.GroupBy(c => c.Rank); if (myRanking > theirRanking) { return(1); } if (myRanking < theirRanking) { return(-1); } List <Card> myLeftoverCards = Cards.Except(myRankingCards).ToList(); myLeftoverCards.Sort(); List <Card> theirLeftoverCards = theirs.Cards.Except(theirRankingCards).ToList(); theirLeftoverCards.Sort(); //Both the same ranking, use tie rules. switch (myRanking) { case PokerHand.RoyalFlush: return(0); case PokerHand.StraightFlush: { int compare = myRankingCards.Last().CompareTo(theirRankingCards.Last()); if (compare != 0) { return(compare); } return(0); } case PokerHand.FourOfAKind: { int compare = myRankingCards.Last().CompareTo(theirRankingCards.Last()); if (compare != 0) { return(compare); } return(myLeftoverCards.Last().CompareTo(theirLeftoverCards.Last())); } case PokerHand.FullHouse: { // Compare Three of a Kind int compare = myRanks.Where(r => r.Count() == 3).First().First().CompareTo(theirRanks.Where(r => r.Count() == 3).First().First()); if (compare != 0) { return(compare); } // Compare Pair return(myRanks.Where(r => r.Count() == 2).First().First().CompareTo(theirRanks.Where(r => r.Count() == 2).First().First())); } case PokerHand.Flush: { int compare = myRankingCards[4].CompareTo(theirRankingCards[4]); if (compare != 0) { compare = myRankingCards[3].CompareTo(theirRankingCards[3]); if (compare != 0) { compare = myRankingCards[2].CompareTo(theirRankingCards[2]); if (compare != 0) { compare = myRankingCards[1].CompareTo(theirRankingCards[1]); if (compare != 0) { compare = myRankingCards[0].CompareTo(theirRankingCards[0]); if (compare != 0) { return(compare); } } } } } return(0); } case PokerHand.Straight: { int compare = myRankingCards.Last().CompareTo(theirRankingCards.Last()); if (compare != 0) { return(compare); } return(0); } case PokerHand.ThreeOfAKind: { int compare = myRankingCards.Last().CompareTo(theirRankingCards.Last()); if (compare != 0) { return(compare); } compare = myLeftoverCards[1].CompareTo(theirLeftoverCards[1]); if (compare != 0) { compare = myLeftoverCards[0].CompareTo(theirLeftoverCards[0]); if (compare != 0) { return(compare); } } return(0); } case PokerHand.TwoPair: { // Compare 1st highest pair int compare = myRanks.Where(r => r.Count() == 2).Last().First().CompareTo(theirRanks.Where(r => r.Count() == 2).Last().First()); if (compare != 0) { return(compare); } // Compare 2nd highest pair return(myRanks.Where(r => r.Count() == 2).First().First().CompareTo(theirRanks.Where(r => r.Count() == 2).First().First())); } case PokerHand.Pair: { int compare = myRankingCards.Last().CompareTo(theirRankingCards.Last()); if (compare != 0) { return(compare); } compare = myLeftoverCards[2].CompareTo(theirLeftoverCards[2]); if (compare != 0) { compare = myLeftoverCards[1].CompareTo(theirLeftoverCards[1]); if (compare != 0) { compare = myLeftoverCards[0].CompareTo(theirLeftoverCards[0]); if (compare != 0) { return(compare); } } } return(0); } case PokerHand.HighCard: { int compare = myRankingCards.Last().CompareTo(theirRankingCards.Last()); if (compare != 0) { return(compare); } compare = myLeftoverCards[3].CompareTo(theirLeftoverCards[3]); if (compare != 0) { compare = myLeftoverCards[2].CompareTo(theirLeftoverCards[2]); if (compare != 0) { compare = myLeftoverCards[1].CompareTo(theirLeftoverCards[1]); if (compare != 0) { compare = myLeftoverCards[0].CompareTo(theirLeftoverCards[0]); if (compare != 0) { return(compare); } } } } return(0); } } return(0); // How? Insanity. }
/* * 4 choices here */ private static bool isJacksOrBetter(PokerHand h) { if (h[0].rank == h[1].rank && h[0].isJacksOrBetter()) return true; if (h[1].rank == h[2].rank && h[1].isJacksOrBetter()) return true; if (h[2].rank == h[3].rank && h[2].isJacksOrBetter()) return true; if (h[3].rank == h[4].rank && h[3].isJacksOrBetter()) return true; return false; }
public static string TestPokerHandRanking(string hand) { return(PokerHand.PokerHandRanking(hand.Split(' '))); }
// make sure the rank differs by one // we can do this since the Hand is // sorted by this point private static bool isStraight(PokerHand h) { if (h[0].rank == h[1].rank-1 && h[1].rank == h[2].rank-1 && h[2].rank == h[3].rank-1 && h[3].rank == h[4].rank-1) return true; // special case cause ace ranks lower // than 10 or higher if (h[1].rank == RANK.Ten && h[2].rank == RANK.Jack && h[3].rank == RANK.Queen && h[4].rank == RANK.King && h[0].rank == RANK.Ace) return true; return false; }
public void OneTimeSetUp() { _cardDeck = new CardDeck(); _cardDeck.ScrambleDeck(); _newHand = _cardDeck.GetHand(5); }
/* * three choices, two pair in the front, * separated by a single card or * two pair in the back */ private static bool isTwoPair(PokerHand h) { if (h[0].rank == h[1].rank && h[2].rank == h[3].rank) return true; if (h[0].rank == h[1].rank && h[3].rank == h[4].rank) return true; if (h[1].rank == h[2].rank && h[3].rank == h[4].rank) return true; return false; }
public abstract bool Parse(PokerHand pokerHand);
public override bool IsValidScoreForHand(PokerHand hand) { return IsFlush(hand.Cards); }
public void TestThreeOfAKindTieBreaker() { //Arrange PokerBL pokerBl = new PokerBL(); PokerHandBL pokerHandBl = new PokerHandBL(); DeckBL deckBl = new DeckBL(); Deck deck = new Deck(); deckBl.Initialize(deck); PokerHand player1 = new PokerHand(deck, "Player1"); PokerHand player2 = new PokerHand(deck, "Player2"); PokerHand player3 = new PokerHand(deck, "Player3"); //Assign cards to players manually to simulate One Pair Tie Breaker var player1CardList = new List <Card>() { new Card(CardRank.Jack, CardSuit.Hearts), new Card(CardRank.Six, CardSuit.Diamonds), new Card(CardRank.Jack, CardSuit.Spades), new Card(CardRank.Jack, CardSuit.Club), new Card(CardRank.Jack, CardSuit.Diamonds) }; var player2CardList = new List <Card>() { new Card(CardRank.Two, CardSuit.Spades), new Card(CardRank.Two, CardSuit.Diamonds), new Card(CardRank.Jack, CardSuit.Spades), new Card(CardRank.King, CardSuit.Club), new Card(CardRank.Two, CardSuit.Hearts) }; var player3CardList = new List <Card>() { new Card(CardRank.King, CardSuit.Spades), new Card(CardRank.King, CardSuit.Diamonds), new Card(CardRank.Jack, CardSuit.Spades), new Card(CardRank.King, CardSuit.Club), new Card(CardRank.Ace, CardSuit.Spades) }; player1.Hand = player1CardList; player2.Hand = player2CardList; player3.Hand = player3CardList; var playerList = new List <PokerHand>() { player1, player2, player3 }; var winningHand = player3; //Act var result = pokerBl.EvaluateWinningHand(playerList); //Assert Assert.AreEqual(winningHand.PlayerName, result.PlayerName, "Player with winning three of a kind hand did not win the game."); }
//Note that lines is only passed for efficiency, it could be obtained //by just splitting handText private PokerHand parseHand(List <string> lines, string handText) { PokerHand hand = new PokerHand(); #region setup variables int start; int end; int curLine; List <Round> rounds = new List <Round>(); #endregion #region Make sure it's a Full Tilt hand if (!handText.StartsWith("Full Tilt Poker")) { throw new InvalidHandFormatException(handText); } #endregion #region Skip partial hands if (lines[0].EndsWith("(partial)")) { return(null); } #endregion hand.Context.Online = true; hand.Context.Site = "Full Tilt Poker"; #if DEBUG Console.WriteLine("Hand Number"); #endif #region Get the hand number start = handText.IndexOf('#') + 1; end = handText.IndexOf(':'); hand.Context.ID = handText.Substring(start, end - start); //Console.WriteLine("ID: {0}", hand.Context.ID); if (printed < 2) { Console.WriteLine("Hand Text: {0}", handText); printed++; } #endregion #if DEBUG Console.WriteLine("Table Name"); #endif #region Get the table name start = end + 2; end = handText.IndexOf(" - ", start) + 1; hand.Context.Table = handText.Substring(start, end - start - 1); #endregion #if DEBUG Console.WriteLine("Blinds"); #endif #region Get the blinds and game format start = end + 2; end = handText.IndexOf(" - ", start) + 1; string blindsAndAntes = handText.Substring(start, end - start); int blindSeparator = blindsAndAntes.IndexOf('/'); string smallBlindText = blindsAndAntes.Substring(0, blindSeparator); if (smallBlindText[0] == '$') { smallBlindText = smallBlindText.Substring(1); } hand.Context.SmallBlind = Decimal.Parse(smallBlindText.Trim()); int bigBlindStart = blindSeparator + 1; int bigBlindEnd = blindsAndAntes.IndexOf(' ', bigBlindStart); string bigBlindText = blindsAndAntes.Substring(bigBlindStart, bigBlindEnd - bigBlindStart); if (bigBlindText[0] == '$') { bigBlindText = bigBlindText.Substring(1); } hand.Context.BigBlind = Decimal.Parse(bigBlindText.Trim()); int anteIndex = blindsAndAntes.IndexOf("Ante"); if (anteIndex != -1) { int anteStart = anteIndex + 5; string anteText = blindsAndAntes.Substring(anteStart); if (anteText[0] == '$') { anteText = anteText.Substring(1); } hand.Context.Ante = Decimal.Parse(anteText.Trim()); } #endregion #if DEBUG Console.WriteLine("Game Format"); #endif #region Get the game format if (hand.Context.Table.Contains("Sit & Go")) { hand.Context.Format = GameFormat.SitNGo; } else if (blindsAndAntes.Contains("$")) { hand.Context.Format = GameFormat.CashGame; } else if (lines[0].Contains("), Table ")) { hand.Context.Format = GameFormat.MultiTableTournament; } else { hand.Context.Format = GameFormat.PlayMoney; } #endregion #if DEBUG Console.WriteLine("Poker Variant and Betting Type"); #endif #region Get the betting type and poker variant start = end + 2; end = handText.IndexOf(" - ", start) + 1; string typeAndVariant = handText.Substring(start, end - start); int capIndex = typeAndVariant.IndexOf(" Cap "); if (capIndex != -1) { string capAmountText = handText.Substring(start, capIndex); hand.Context.CapAmount = Decimal.Parse(capAmountText.Trim().Replace("$", "")); hand.Context.CapAmountSpecified = true; hand.Context.Capped = true; } else { hand.Context.CapAmountSpecified = false; hand.Context.Capped = false; } if (typeAndVariant.Contains("Pot Limit")) { hand.Context.BettingType = BettingType.PotLimit; } else if (typeAndVariant.Contains("No Limit")) { hand.Context.BettingType = BettingType.NoLimit; } else { hand.Context.BettingType = BettingType.FixedLimit; } if (typeAndVariant.Contains("Hold'em")) { hand.Context.PokerVariant = PokerVariant.TexasHoldEm; } else if (typeAndVariant.Contains("Omaha Hi")) { hand.Context.PokerVariant = PokerVariant.OmahaHi; } else { hand.Context.PokerVariant = PokerVariant.OmahaHiLo; } #endregion #if DEBUG Console.WriteLine("Time Stamp"); #endif #region Get the date and time start = end + 2; end = handText.IndexOf(' ', start); string timeText = handText.Substring(start, end - start); string[] timeTokens = timeText.Split(':'); int hour = Int32.Parse(timeTokens[0]); int minute = Int32.Parse(timeTokens[1]); int second = Int32.Parse(timeTokens[2]); start = handText.IndexOf('-', end) + 2; string dateText = lines[0].Substring(start, 10); string[] dateTokens = dateText.Split('/'); int year = Int32.Parse(dateTokens[0]); int month = Int32.Parse(dateTokens[1]); int day = Int32.Parse(dateTokens[2]); hand.Context.TimeStamp = new DateTime(year, month, day, hour, minute, second); #endregion #if DEBUG Console.WriteLine("Players"); #endif #region Create the players List <Player> players = new List <Player>(); curLine = 1; for (Match m = stacksExp.Match(lines[curLine]); m.Success; m = stacksExp.Match(lines[curLine])) { if (m.Success) { GroupCollection gc = m.Groups; Player p = new Player(); p.Seat = Int32.Parse(gc[1].Value); p.Name = gc[2].Value; p.Stack = Decimal.Parse(gc[3].Value); players.Add(p); curLine++; } } hand.Players = players.ToArray(); #endregion #region Terminate parsing of unsupported poker type if ((!typeAndVariant.Contains("Hold'em") && (!typeAndVariant.Contains("Omaha")))) { return(hand); } #endregion #if DEBUG Console.WriteLine("Blinds and Antes Posted"); #endif #region Get the blinds and antes posted List <Blind> blinds = new List <Blind>(); for (; !lines[curLine].StartsWith("The button is in seat #"); curLine++) { if (lines[curLine].Contains(" has been canceled")) { return(hand); } for (Match m = actionExp.Match(lines[curLine]); m.Success; m = m.NextMatch()) { GroupCollection gc = m.Groups; Blind blind = new Blind(); blind.Player = gc[1].Value; if (gc[2].Value == "antes") { blind.Type = BlindType.Ante; } else if (gc[6].Value == "small") { blind.Type = gc[5].Value.Trim() == "dead" ? BlindType.DeadBlind : BlindType.SmallBlind; } else if (gc[6].Value == "big") { blind.Type = gc[5].Value.Trim() == "dead" ? BlindType.DeadBlind : BlindType.BigBlind; } else if (gc[2].Value == "posts") { blind.Type = BlindType.LateBlind; } else { throw new Exception("Unknown blind type: " + lines[curLine]); } if (lines[curLine].Contains("dead")) { for (int i = 0; i < gc.Count; i++) { Console.WriteLine("{0}: \"{1}\"", i, gc[i].Value); } Console.WriteLine("Found as {0}", blind.Type.ToString()); } blind.Amount = Decimal.Parse(gc[7].Value); blind.AllIn = gc[9].Value.Length == 15; blinds.Add(blind); } } hand.Blinds = blinds.ToArray(); #endregion #if DEBUG Console.WriteLine("Button"); #endif #region Get the button string buttonText = lines[curLine].Substring(lines[curLine].IndexOf('#') + 1); hand.Context.Button = Int32.Parse(buttonText); curLine++; #endregion #if DEBUG Console.WriteLine("Hole Cards and Hero"); #endif #region Get the hole cards and the name of the hero int tempIndex = curLine; bool heroType = true; while (!lines[curLine].StartsWith("Dealt to ")) { if (lines[curLine].Equals("*** FLOP ***") || lines[curLine].Equals("*** SUMMARY ***")) { curLine = tempIndex; heroType = false; break; } else { curLine++; } } if (heroType) { start = "Dealt to ".Length; end = lines[curLine].IndexOf(" [", start); hand.Hero = lines[curLine].Substring(start, end - start); start = end + 2; List <Card> holecards = new List <Card>(); holecards.Add(new Card(lines[curLine].Substring(start, 2))); holecards.Add(new Card(lines[curLine].Substring(start + 3, 2))); if (hand.Context.PokerVariant != PokerVariant.TexasHoldEm) { holecards.Add(new Card(lines[curLine].Substring(start + 6, 2))); holecards.Add(new Card(lines[curLine].Substring(start + 9, 2))); } hand.HoleCards = holecards.ToArray(); curLine++; } #endregion #if DEBUG Console.WriteLine("Preflop Actions"); #endif #region Preflop Actions rounds.Add(new Round()); rounds[0].Actions = getRoundActions(lines, ref curLine); #endregion #if DEBUG Console.WriteLine("Flop Actions"); #endif #region Flop Actions and Community Cards if (lines[curLine].StartsWith("*** FLOP ***")) { rounds.Add(new Round()); start = lines[curLine].IndexOf('[') + 1; Card[] flop = new Card[3]; flop[0] = new Card(lines[curLine].Substring(start, 2)); flop[1] = new Card(lines[curLine].Substring(start + 3, 2)); flop[2] = new Card(lines[curLine].Substring(start + 6, 2)); rounds[1].CommunityCards = flop; curLine++; rounds[1].Actions = getRoundActions(lines, ref curLine); } #endregion #if DEBUG Console.WriteLine("Turn Actions"); #endif #region Turn Actions and Community Card if (lines[curLine].StartsWith("*** TURN ***")) { rounds.Add(new Round()); start = lines[curLine].LastIndexOf('[') + 1; Card[] turn = new Card[1]; turn[0] = new Card(lines[curLine].Substring(start, 2)); rounds[2].CommunityCards = turn; curLine++; rounds[2].Actions = getRoundActions(lines, ref curLine); } #endregion #if DEBUG Console.WriteLine("River Actions"); #endif #region River Actions and Community Card if (lines[curLine].StartsWith("*** RIVER ***")) { rounds.Add(new Round()); start = lines[curLine].LastIndexOf('[') + 1; Card[] river = new Card[1]; river[0] = new Card(lines[curLine].Substring(start, 2)); rounds[3].CommunityCards = river; curLine++; rounds[3].Actions = getRoundActions(lines, ref curLine); } #endregion #region Set rounds hand.Rounds = rounds.ToArray(); #endregion #if DEBUG Console.WriteLine("Results"); #endif #region Get pots won List <HandResult> results = new List <HandResult>(); List <List <Pot> > pots = new List <List <Pot> >(); for (; !lines[curLine].StartsWith("*** SUMMARY"); curLine++) { Match m = potsExp.Match(lines[curLine]); if (m.Success) { GroupCollection gc = m.Groups; Pot p = new Pot(); p.Amount = Decimal.Parse(gc[8].Value); if (gc[7].Value.Length > 0) { p.Number = Int32.Parse(gc[7].Value); } else if ((gc[5].Value.Length == 0 && gc[3].Value == "the pot") || gc[5].Value == "main ") { p.Number = 0; } else if (gc[5].Length > 0) { p.Number = 1; } HandResult result = null; List <Pot> potList = null; for (int i = 0; i < results.Count; i++) { if (results[i].Player == gc[1].Value) { result = results[i]; potList = pots[i]; break; } } if (result == null) { result = new HandResult(gc[1].Value); potList = new List <Pot>(); results.Add(result); pots.Add(potList); } potList.Add(p); } } //add the pots to the model for (int i = 0; i < results.Count; i++) { results[i].WonPots = pots[i].ToArray(); } //get the rake, if any if (hand.Context.Format == GameFormat.CashGame) { for (; !lines[curLine].StartsWith("Total pot") || lines[curLine].Contains(":") || !lines[curLine].Contains("| Rake "); curLine++) { } int rakeStart = lines[curLine].LastIndexOf("| Rake ") + "| Rake ".Length; string rakeText = lines[curLine].Substring(rakeStart).Replace("$", ""); hand.Rake = Decimal.Parse(rakeText); } #endregion #if DEBUG Console.WriteLine("Shown Hands"); #endif #region Get the shown down hands for (; curLine < lines.Count; curLine++) { Match m = shownHandsExp.Match(lines[curLine]); if (m.Success) { GroupCollection gc = m.Groups; List <Card> shownCards = new List <Card>(); shownCards.Add(new Card(gc[5].Value)); shownCards.Add(new Card(gc[6].Value)); if (hand.Context.PokerVariant != PokerVariant.TexasHoldEm) { shownCards.Add(new Card(gc[8].Value)); shownCards.Add(new Card(gc[9].Value)); } string player = gc[2].Value; HandResult hr = null; foreach (HandResult curResult in results) { if (curResult.Player == player) { hr = curResult; break; } } if (hr == null) { hr = new HandResult(player); results.Add(hr); } hr.HoleCards = shownCards.ToArray(); } } #endregion #region Set the results hand.Results = results.ToArray(); #endregion #if DEBUG Console.WriteLine("Done"); #endif return(hand); }
public PokerHandXML Parse(TextReader file) { List <PokerHand> hands = new List <PokerHand>(); string line; bool inHand = false; StringBuilder handText = new StringBuilder(); List <string> lines = new List <string>(); Console.WriteLine("Starting parse"); while ((line = file.ReadLine()) != null) { //skip blank lines if (!inHand && line.Length == 0) { continue; } // filter pp notifications if (line.StartsWith(">") || line.StartsWith(@"The Progressive Bad Beat Jackpot has been hit")) { continue; } if (inHand && (line.Length == 0 || line == @"Game #<do not remove this line!> starts.")) { Console.WriteLine("Found hand"); PokerHand hand = parseHand(lines, handText.ToString()); if (hand != null) { hands.Add(hand); } inHand = false; continue; } if (!inHand) { lines.Clear(); handText = new StringBuilder(); inHand = true; } handText.AppendLine(line); lines.Add(line); } if (inHand) { Console.WriteLine("Found hand"); PokerHand hand = parseHand(lines, handText.ToString()); if (hand != null) { hands.Add(hand); } } PokerHandXML result = new PokerHandXML(); result.Hands = hands.ToArray(); return(result); }
public override bool IsSatisfiedBy(PokerHand pokerHand) { return(base.UnbrokenSequence(pokerHand)); }
private Action[] getRoundActions(PokerHand hand, List <Round> rounds, List <string> lines, ref int curLine) { List <Action> actions = new List <Action>(); for (; !lines[curLine].StartsWith("** Dealing"); curLine++) { if (potsExp.Match(lines[curLine]).Success) { break; } Match m = actionExp.Match(lines[curLine]); if (m.Success) { GroupCollection gc = m.Groups; if (hand.Players.FirstOrDefault(p => p.Name == gc[1].Value) == null) { continue; } Action a = new Action(); a.Player = gc[1].Value; //is all-In|folds|checks|bets|calls|raises switch (gc[2].Value) { case "folds": a.Type = ActionType.Fold; break; case "checks": a.Type = ActionType.Check; break; case "calls": a.Type = ActionType.Call; a.Amount = Decimal.Parse(gc[4].Value); break; case "bets": a.Type = ActionType.Bet; a.Amount = Decimal.Parse(gc[4].Value); break; case "raises": a.Type = ActionType.Raise; a.Amount = Decimal.Parse(gc[4].Value); break; case "is all-In": { a.AllIn = true; a.Amount = hand.Players.FirstOrDefault(p => p.Name == a.Player).Stack - rounds.Sum(r => r.Actions == null ? 0 : r.Actions.Sum(act => act.Player == a.Player ? act.Amount : 0)) - actions.Sum(act => act.Player == a.Player ? act.Amount : 0); var lastRaise = actions.LastOrDefault(act => act.Type == ActionType.Raise); if (lastRaise == null) { if (rounds.Count == 1) { if (a.Amount <= hand.Blinds.First(b => b.Type == BlindType.BigBlind).Amount) { a.Type = ActionType.Call; } else { a.Type = ActionType.Raise; } } else { a.Type = ActionType.Bet; } } else if (lastRaise.Amount < a.Amount) { a.Type = ActionType.Raise; } else { a.Type = ActionType.Call; } } break; default: throw new Exception("Unknown action type: " + lines[curLine]); } actions.Add(a); continue; } // TODO: Handle returned money. Note that pp does not make this obvious. } return(actions.ToArray()); }
protected bool SameSuite(PokerHand pokerHand) { return(pokerHand.Select(c => c.Suit).Distinct().Count() == 1); }
public StraightFlush(PokerHand pokerHand) : base(pokerHand) { base.Weighting = 9; }