HoldemHand.Hand.GapCount C# (CSharp) Method

GapCount() public static method

Counts the number of empty space between adjacent cards. 0 means connected, 1 means a gap of one, 2 means a gap of two and 3 means a gap of three. This method calculates the results. Because of that equivelent call in PocketHands is preferred because it uses a lookup table and is faster. This function remains to allow for automated testing.
public static GapCount ( ulong mask ) : int
mask ulong two card mask mask
return int
        public static int GapCount(ulong mask)
        {
            int start, end;

            if (BitCount(mask) != 2) return -1;

            uint bf = CardMask(mask, Clubs) |
                        CardMask(mask, Diamonds) |
                        CardMask(mask, Hearts) |
                        CardMask(mask, Spades);

            if (BitCount(bf) != 2) return -1;

            for (start = 12; start >= 0; start--)
            {
                if ((bf & (1UL << start)) != 0)
                    break;
            }

            for (end = start - 1; end >= 0; end--)
            {
                if ((bf & (1UL << end)) != 0)
                    break;
            }

            // Handle wrap
            if (start == 12 && end == 0) return 0;
            if (start == 12 && end == 1) return 1;
            if (start == 12 && end == 2) return 2;
            if (start == 12 && end == 3) return 3;

            return (start-end-1 > 3 ? -1 : start-end-1);
        }