HoldemHand.Hand.HandDistance C# (CSharp) Method

HandDistance() public static method

This method returns the mask distance from the best possible mask given this board (no draws are considered). The value 0 is the best possible mask. The value 1 is the next best mask and so on.
public static HandDistance ( ulong pocket, ulong board ) : long
pocket ulong The players pocket mask
board ulong The board mask
return long
        public static long HandDistance(ulong pocket, ulong board)
        {
            #if DEBUG
            if (BitCount(pocket) != 2) throw new ArgumentException("player must have exactly 2 cards");
            if (BitCount(board) != 3 && BitCount(board) != 4) throw new ArgumentException("board must contain 3 or 4 cards");
            #endif
            uint hv = 0U;
            SortedDictionary<uint, long> dict = new SortedDictionary<uint, long>();

            // Current Hand
            uint pocketHandVal = Hand.Evaluate(pocket | board);

            // Build a List of unique hands
            foreach (ulong p in Hand.Hands(0UL, board, 2))
            {
                hv = Hand.Evaluate(p | board);
                if (!dict.ContainsKey(hv))
                    dict.Add(hv, 1);
            }

            // Find current mask in the sorted list
            long count = dict.Count-1;
            foreach (uint handval in dict.Keys)
            {
                if (handval == pocketHandVal)
                    return count;
                count--;
            }
            return -1;
        }