Three of a kind numbers

Started by
33 comments, last by alvaro 11 years ago

This should be blazing fast:

struct Classification {
bool is_three_of_a_kind;
bool is_four_of_a_kind;
bool is_full_house;
bool is_small_straight;
bool is_large_straight;
bool is_yahtzee;
bool is_chance;

Classification(int *dice) {
int sum = 0;
int types = 0;

for (int i=0; i<5; ++i) {
sum += 1 << ((dice[i]-1)*3);
types |= 1 << (dice[i]-1);
}
int n_types = __builtin_popcount(types); // This only works in gcc. You can find other ways of counting set bits for other compilers.

is_three_of_a_kind = (((sum + 0111111) & 0444444) != 0);
is_four_of_a_kind = ((sum & 0444444) != 0);
is_full_house = ((n_types == 2) && !is_four_of_a_kind);
is_small_straight = ((types & (types>>1) & (types>>2) & (types>>3)) != 0);
is_large_straight = (types == 31 || types == 62);
is_yahtzee = ((types & (types-1)) == 0);
is_chance = true;
}
};

I thought this was interesting because I couldn't really understand it. So I ran it and one problem is that it will set is_three_kind to true when you have a four-of-a-kind or a five-of-a kind. Usually in these classification routines, you would want to pick out only the strongest classification, right?

Anyway, I still think this code is cool.

The Four Horsemen of Happiness have left.

Advertisement

I thought this was interesting because I couldn't really understand it. So I ran it and one problem is that it will set is_three_kind to true when you have a four-of-a-kind or a five-of-a kind. Usually in these classification routines, you would want to pick out only the strongest classification, right?

No, my understanding of the rules (I have never played the game) is that you may want to score a particular hand as a lower rank than the maximum. Actually, my code might be wrong in the other direction, because it doesn't recognize five-of-a-kind as a form of full house. In any case, if you understand how the code works, it is trivial to change that type of thing.

This should be blazing fast:


struct Classification {
  bool is_three_of_a_kind;
  bool is_four_of_a_kind;
  bool is_full_house;
  bool is_small_straight;
  bool is_large_straight;
  bool is_yahtzee;
  bool is_chance;

  Classification(int *dice) {
    int sum = 0;
    int types = 0;

    for (int i=0; i<5; ++i) {
      sum += 1 << ((dice-1)*3);
      types |= 1 << (dice-1);
    }
    int n_types = __builtin_popcount(types); // This only works in gcc. You can find other ways of counting set bits for other compilers.

    is_three_of_a_kind = (((sum + 0111111) & 0444444) != 0);
    is_four_of_a_kind = ((sum & 0444444) != 0);
    is_full_house = ((n_types == 2) && !is_four_of_a_kind);
    is_small_straight = ((types & (types>>1) & (types>>2) & (types>>3)) != 0);
    is_large_straight = (types == 31 || types == 62);
    is_yahtzee = ((types & (types-1)) == 0);
    is_chance = true;
  }
};
Another alternative is to encode all the bools in a single byte and make a table that maps a 5-digit base-6 number to its classification. That table is smaller than 8KB.

I am sure this should be maybe in another topic of its own but for those of us who aren't has advanced mind explaining some quick theory behind this? While I can read the code it's pretty had for me to generalize what's actually happening which I don't understand the theory. Yea this may be for a topic all on it's own.

Use an unordered_map to store your numbers. The key would be number, the value would be how many you have of that number.

Example:


std::unordered_map< unsigned short int, unsigned short int > numbersInHand;

std::vector<unsigned short int> numbersDealt;

for(unsigned short int i = 0; i < numbersDealth.size(); i++)
{
      numbersInHand[ numbersDealt ]++;
}

 

Then it's just a matter of iterating through the map and getting the numbers from each value in it and seeing how big it is.


std::unordered_map<unsigned short int, unsigned short int>::iterator currentGroup = numbersInHand.begin();

enum HandCombinations {SinglesOnly, Pair, TwoPair, ThreeOfAKind, FullHouse, FourOfAKind} handStrength = SinglesOnly;

while( currentGroup != numbersInHand.end() )
{
      switch( currentGroup->second() )

      case 4:
               handStrength = FourOfAKind;
               break;

      case 3:

               if(handStrength == Pair)
               {
                      handStrength = FullHouse;
               }
               else
               {
                      handStrength = ThreeOfAKind;
               }
               break;

      case 2:
              
              if(handStrength == ThreeOfAKind)
              {
                   handStrength == FullHouse;
              }
              else
              {
                   handStrenght == Pair;
              }
              break;

      currentGroup++;
}

 

Also, now that I think about it, you could use a map rather than an unordered map because of it keeping things in sequence.

This would make searching for straights easy.


//pseudocode
bool straightFound = false;
bool keepsearching = true;

iterator = begin;

int lowestNumber = iterator->second;
int numbersInARow = 1;
while(keepsearching)
{

     if( hand.find(lowestNumber + 1) != hand.end() )
     {
          lowestNumber++;
          numbersInARow++;
     }

     else
     {
         keepSearching = false;
     }

}

if( numbersInARow == 5)
{
     handStrength = straight;
}
 
I added some comments to the code. There are some prerequisites to understanding it:
* How integers are represented in binary
* Bitwise operations
* Constants that start with a `0' in C/C++ are in octal
// This struct computes what categories match the dice in the constructor
struct Classification {
  bool is_three_of_a_kind;
  bool is_four_of_a_kind;
  bool is_full_house;
  bool is_small_straight;
  bool is_large_straight;
  bool is_yahtzee;
  bool is_chance;
  
  // Pass a pointer to an array of 5 dice, with values in 1..6
  Classification(int *dice) {
    // sum consists of 6 groups of 3 bits, which are counters for the values in the dice
    int sum = 0;
    // types marks which values appear at all, one bit per value
    int types = 0;

    for (int i=0; i<5; ++i) {
      sum += 1 << ((dice-1)*3); // Increment the appropriate counter within sum
      types |= 1 << (dice-1); // Mark the value
    }
    int n_types = __builtin_popcount(types); // This only works in gcc. You can find other ways of counting set bits for other compilers.
    /*
    // Alternative code to count bits:
    static int const bitcount_table[64] = {
      0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 
      1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 
      1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 
      2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6
    };
    int n_types = bitcount_table[types];
    */

    // I sorted the checks from easy to hard (somewhat subjectively)
    
    // Chance: Every hand qualifies
    is_chance = true;
    
    // Yahtzee: Only one type
    is_yahtzee = (n_types == 1);
    
    // Large straight: Either 1-2-3-4-5 or 2-3-4-5-6, for which types
    // is 31 or 62 respectively
    is_large_straight = (types == 31 || types == 62);
    
    // Small straight: types must contain four consecutive bits
    is_small_straight = ((types & (types>>1) & (types>>2) & (types>>3)) != 0);
    
    // Full house: Either only two values and none of them appear 4
    // times (it can only be a pair and a trio), or is yahtzee
    is_full_house = ((n_types == 2 && !is_four_of_a_kind) || n_types == 1);
    
    // Four of a kind: check if any counter got to 4, by checking the
    // bit whose value is 4
    is_four_of_a_kind = ((sum & 0444444) != 0);
    
    // To detect three of a kind, add 1 to each 3-bit counter and see
    // if any of them trigger the bit whose value is 4
    is_three_of_a_kind = (((sum + 0111111) & 0444444) != 0);
  }
};
If you are a beginner and still don't understand it, it's OK. Maybe you can come back to this code after you have gained some experience with the things I listed at the beginning of this post.

This topic is closed to new replies.

Advertisement