c# - Determine a playing-card flush hand -


in card application use 0-51 represent 5 card hand.

the suit card / 13
rank card %
there 4 possible suit (spade, heart, club diamond)

if 5 suit same flush. flush of equal value. spades same diamonds.

i know going pre-optimization evil running simulations doing million of times , expensive step. card byte calculation seem faster int. don't check range put in know there range.

is there more efficient way?

public bool isflush(int[] quick) {     hashset<int> suit = new hashset<int>();     suit.add(quick[0] / 13);     int thisquick;     (int = 1; < quick.length; ++)     {         thisquick = quick[i];         if (thisquick < 0 || thisquick > 51)             throw new indexoutofrangeexception();         if (suit.add(thisquick / 13))             return false;     }     return true; } 

eliminating hashset should speed things bit:

public static bool isflush(int[] hand) {     int firstsuit = hand[0] / 13;     (int = 1; < hand.length; i++)     {         int card = hand[i];         if (card < 0 || card > 51)             throw new indexoutofrangeexception();         if (firstsuit != (card / 13))             return false;     }     return true; } 

my (admittedly meager) testing shows 20% performance improvement.


Comments