public HoldemHand(Card c1, Card c2) { cards = new Cards(4); cards.Add(c1); cards.Add(c2); cards.Sort(); //Evaluate hand int handRanks = 1; handFlushRankBits = new int[4] { 0, 0, 0, 0 }; strengthDictionary = new Dictionary <int, short>(); for (int n = 0; n < cards.Count; n++) { handRanks *= Consts.primeNumbers[cards.GetCard(n).Rank]; handFlushRankBits[cards.GetCard(n).Suit] |= Consts.rankMasks[cards.GetCard(n).Rank]; } String s = (String)Consts.Deserialize("HoldemDictionaries\\strengthDict" + handRanks + ".dat"); String[] tokens = s.Split(new char[] { ':', ',' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < tokens.Length; i += 2) { strengthDictionary[int.Parse(tokens[i])] = short.Parse(tokens[i + 1]); } }
private bool IsKicker(int boardRankBits, int cardsNeeded, int kicker) { if ((boardRankBits & Consts.rankMasks[kicker]) > 0) { return(false); } int totalRankBits = boardRankBits | Consts.rankMasks[kicker]; if ((totalRankBits % 2) == 1) { totalRankBits--; } int bitsToRemove = Consts.GetBitCount(totalRankBits) - cardsNeeded; for (int n = 0; n < 13; n++) { if ((totalRankBits & Consts.rankMasks[n]) > 0) { bitsToRemove--; totalRankBits -= Consts.rankMasks[n]; } if (bitsToRemove == 0) { break; } } totalRankBits &= Consts.rankMasks[kicker]; if (totalRankBits > 0) { return(true); } return(false); }
public void EvaluateHand() { handRanks = 1; handFlushRankBits = new int[4] { 0, 0, 0, 0 }; suitCounts = 26214; highestOfSuit = new int[4] { 0, 0, 0, 0 }; strengthDictionary = new Dictionary <int, int>(); for (int n = 0; n < cards.Count; n++) { suitCounts += 1 << (GetCard(n).Suit * 4); if (highestOfSuit[GetCard(n).Suit] < GetCard(n).Rank) { highestOfSuit[GetCard(n).Suit] = GetCard(n).Rank; } handRanks *= Consts.primeNumbers[GetCard(n).Rank]; handFlushRankBits[GetCard(n).Suit] |= Consts.rankMasks[GetCard(n).Rank]; } String s = File.ReadAllText("OmahaDictionaries\\strengthDict" + handRanks + ".txt"); String[] tokens = s.Split(new char[] { ':', ',' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < tokens.Length; i += 2) { strengthDictionary[int.Parse(tokens[i])] = int.Parse(tokens[i + 1]); } //optimization for straight flushes straightFlushesToCheck = new ArrayList[4]; for (int m = 0; m < 4; m++) { if ((suitCounts & Consts.suitMasks[m]) == Consts.suitMasks[m]) //if (Consts.GetBitCount(handFlushRankBits[m]) > 1) { straightFlushesToCheck[m] = new ArrayList(8); for (int n = 9; n >= 0; n--) { int bits = (((int)handFlushRankBits[m]) >> n) % 32; if (Consts.GetBitCount(bits) == 2) { straightFlushesToCheck[m].Add(n); } } } else { straightFlushesToCheck[m] = new ArrayList(0); } } }
public int CheckHighCards(int boardRankBits, int cardsNeeded) { int highCards = 0; int totalRankBits = boardRankBits | handRankBits; if ((totalRankBits % 2) == 1) { totalRankBits--; } int bitsToRemove = Consts.GetBitCount(totalRankBits) - cardsNeeded; bool highestBit = true; for (int n = 0; n < 13; n++) { if ((totalRankBits & Consts.rankMasks[n]) > 0) { bitsToRemove--; totalRankBits -= Consts.rankMasks[n]; } if (bitsToRemove == 0) { break; } } totalRankBits &= handRankBits; int bits; if (cardsNeeded == 5) { totalRankBits = totalRankBits >> 4; bits = 9; } else { totalRankBits = totalRankBits >> 2; bits = 11; } for (int n = bits; n >= 0; n--) { if ((totalRankBits & (1 << n)) > 0) { if (highestBit) { highCards += n * bits; highestBit = false; } else { highCards += n; } } } return(highCards); }
private void EvaluateShowdown(Board board, int suitCounts) { double highHandValue = 1.0; //get ranks Dictionary <int, int> rankCounts = new Dictionary <int, int>(); int boardRankBits = 0; for (int n = 0; n < board.NumCards; n++) { if (rankCounts.ContainsKey(board.GetCard(n).Rank)) { rankCounts[board.GetCard(n).Rank] = rankCounts[board.GetCard(n).Rank] + 1; } else { rankCounts.Add(board.GetCard(n).Rank, 1); } boardRankBits |= Consts.rankMasks[board.GetCard(n).Rank]; } if (Consts.GetBitCount(boardRankBits % 256) > 2) //if more than 2 low cards are on board { //find best low hand ArrayList lowWinners = new ArrayList(hands.Count); int maxLowStrength = 0; for (int n = 0; n < hands.Count; n++) { int currentLowStrength = ((HandSlow)hands[n]).CheckLow(boardRankBits); if (currentLowStrength > maxLowStrength) { lowWinners.Clear(); maxLowStrength = currentLowStrength; lowWinners.Add(n); } else if (currentLowStrength == maxLowStrength) { lowWinners.Add(n); } } if (lowWinners.Count > 0) { highHandValue = highHandValue / 2; foreach (int n in lowWinners) { values[n] = (double)((double)values[n]) + (highHandValue / lowWinners.Count); } } } //determine high winner ArrayList highWinners = new ArrayList(hands.Count); //check for flush possibility int flushSuit = -1; //arbitrary assignment for (int n = 0; n < 4; n++) { if ((suitCounts & Consts.suitMasks[n]) == Consts.suitMasks[n]) { flushSuit = n; break; } } bool canStraight = false; if (Consts.GetBitCount(boardRankBits) > 2) { canStraight = true; } //check straightflush possiblity if (canStraight && flushSuit >= 0) { //get RankBits of suited cards on board int boardFlushRankBits = 0; for (int n = 0; n < board.NumCards; n++) { if (board.GetCard(n).Suit == flushSuit) { boardFlushRankBits |= Consts.rankMasks[board.GetCard(n).Rank]; } } //check for straight flush if (Consts.GetBitCount(boardFlushRankBits) > 2) { int maxStraightFlush = 0; for (int n = 0; n < hands.Count; n++) { int currentStraightFlush = ((HandSlow)hands[n]).CheckStraightFlush(flushSuit, boardFlushRankBits); if (currentStraightFlush > maxStraightFlush) { highWinners.Clear(); maxStraightFlush = currentStraightFlush; highWinners.Add(n); } } if (highWinners.Count > 0) { foreach (int n in highWinners) { values[n] = (double)((double)values[n]) + (highHandValue / (double)highWinners.Count); } return; } } } //check for full house or quads, this must be done as it helps check trips, 2 pair, pair, high card int maxQuadsBoat = 0; for (int n = 0; n < hands.Count; n++) { int currentQuadsBoat = ((HandSlow)hands[n]).CheckQuadsBoat(rankCounts); if (currentQuadsBoat > maxQuadsBoat) { highWinners.Clear(); maxQuadsBoat = currentQuadsBoat; highWinners.Add(n); } else if (currentQuadsBoat == maxQuadsBoat) { highWinners.Add(n); } } if (highWinners.Count > 0) { foreach (int n in highWinners) { values[n] = (double)((double)values[n] + (highHandValue / highWinners.Count)); } return; } //check for flush if (flushSuit >= 0) { int maxFlush = 0; for (int n = 0; n < hands.Count; n++) { int currentFlush = ((HandSlow)hands[n]).CheckFlush(flushSuit); if (currentFlush > maxFlush) { highWinners.Clear(); maxFlush = currentFlush; highWinners.Add(n); } } if (highWinners.Count > 0) { foreach (int n in highWinners) { values[n] = (double)((double)values[n]) + (highHandValue / highWinners.Count); } return; } } //check for straight if (canStraight) { int maxStraight = 0; for (int n = 0; n < hands.Count; n++) { int currentStraight = ((HandSlow)hands[n]).CheckStraight(boardRankBits); if (currentStraight > maxStraight) { highWinners.Clear(); maxStraight = currentStraight; highWinners.Add(n); } else if (currentStraight == maxStraight) { highWinners.Add(n); } } if (highWinners.Count > 0) { foreach (int n in highWinners) { values[n] = (double)((double)values[n]) + (highHandValue / highWinners.Count); } return; } } //check for trips or set int maxSetTrips = 0; for (int n = 0; n < hands.Count; n++) { int currentSetTrips = ((HandSlow)hands[n]).CheckSetTrips(); if (currentSetTrips > maxSetTrips) { highWinners.Clear(); maxSetTrips = currentSetTrips; highWinners.Add(n); } else if (currentSetTrips == maxSetTrips) { highWinners.Add(n); } } if (highWinners.Count > 0) { foreach (int n in highWinners) { values[n] = (double)((double)values[n]) + (highHandValue / highWinners.Count); } return; } //check for two pair int maxTwoPair = 0; for (int n = 0; n < hands.Count; n++) { int currentTwoPair = ((HandSlow)hands[n]).CheckTwoPair(rankCounts); if (currentTwoPair > maxTwoPair) { highWinners.Clear(); maxTwoPair = currentTwoPair; highWinners.Add(n); } else if (currentTwoPair == maxTwoPair) { highWinners.Add(n); } } if (highWinners.Count > 0) { foreach (int n in highWinners) { values[n] = (double)((double)values[n]) + (highHandValue / highWinners.Count); } return; } //check for one pair int maxPair = 0; for (int n = 0; n < hands.Count; n++) { int currentPair = ((HandSlow)hands[n]).CheckPair(rankCounts); if (currentPair > maxPair) { highWinners.Clear(); maxPair = currentPair; highWinners.Add(n); } else if (currentPair == maxPair) { highWinners.Add(n); } } if (highWinners.Count > 0) { foreach (int n in highWinners) { values[n] = (double)((double)values[n]) + (highHandValue / highWinners.Count); } return; } //check for high card int maxHighCards = 0; for (int n = 0; n < hands.Count; n++) { int currentHighCards = ((HandSlow)hands[n]).CheckHighCards(); if (currentHighCards > maxHighCards) { highWinners.Clear(); maxHighCards = currentHighCards; highWinners.Add(n); } else if (currentHighCards == maxHighCards) { highWinners.Add(n); } } foreach (int n in highWinners) { values[n] = (double)((double)values[n]) + (highHandValue / highWinners.Count); } }
public OmahaHand(Card c1, Card c2, Card c3, Card c4) { cards = new Cards(4); cards.Add(c1); cards.Add(c2); cards.Add(c3); cards.Add(c4); cards.Sort(); //evaluate hand int handRanks = 1; handFlushRankBits = new int[4] { 0, 0, 0, 0 }; suitCounts = 26214; highestOfSuit = new int[4] { 0, 0, 0, 0 }; strengthDictionary = new Dictionary <int, int>(); for (int n = 0; n < cards.Count; n++) { suitCounts += 1 << (cards.GetCard(n).Suit * 4); if (highestOfSuit[cards.GetCard(n).Suit] < cards.GetCard(n).Rank) { highestOfSuit[cards.GetCard(n).Suit] = cards.GetCard(n).Rank; } handRanks *= Consts.primeNumbers[cards.GetCard(n).Rank]; handFlushRankBits[cards.GetCard(n).Suit] |= Consts.rankMasks[cards.GetCard(n).Rank]; } String s = (String)Consts.Deserialize("OmahaDictionaries\\strengthDict" + handRanks + ".dat"); String[] tokens = s.Split(new char[] { ':', ',' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < tokens.Length; i += 2) { strengthDictionary[int.Parse(tokens[i])] = int.Parse(tokens[i + 1]); } //optimization for straight flushes straightFlushesToCheck = new ArrayList[4]; for (int m = 0; m < 4; m++) { if ((suitCounts & Consts.suitMasks[m]) == Consts.suitMasks[m]) //if (Consts.GetBitCount(handFlushRankBits[m]) > 1) { straightFlushesToCheck[m] = new ArrayList(8); for (int n = 9; n >= 0; n--) { int bits = (((int)handFlushRankBits[m]) >> n) % 32; if (Consts.GetBitCount(bits) == 2) { straightFlushesToCheck[m].Add(n); } } } else { straightFlushesToCheck[m] = new ArrayList(0); } } }