# Three of a kind numbers

This topic is 1745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

It will pass the check for five of a kind, because you don't check if the three's are different from the two's. That may not be a problem if the rules allow for five of a kind to also be used for a full house, but keep that in mind.

You can also merge the two statements by checking if the first two are equal, the last two are equal, and finally if the middle one is equal to either the second of the other values. This allows for an easy check that the three's and the two's are different.

if(
dice[0] == dice[1] && // first pair
dice[3] == dice[4] && // last pair
(dice[2] == dice[1] || dice[2] == dice[3]) && // middle must be equal to either pair
dice[2] != dice[4] // exclude five of a kind where first and last pair are equal
) {
....
}


But I would try to code some general pattern matching method so that you don't have to hand code this at all.

Edited by Brother Bob

##### Share on other sites

AHHHHHHHHHHHHHHHHHHHHHHHH

##### Share on other sites

It will pass the check for five of a kind, because you don't check if the three's are different from the two's. That may not be a problem if the rules allow for five of a kind to also be used for a full house, but keep that in mind.

You can also merge the two statements by checking if the first two are equal, the last two are equal, and finally if the middle one is equal to either the second of the other values. This allows for an easy check that the three's and the two's are different.

if(
dice[0] == dice[1] && // first pair
dice[3] == dice[4] && // last pair
(dice[2] == dice[1] || dice[2] == dice[3]) && // middle must be equal to either pair
dice[2] != dice[4] // exclude five of a kind where first and last pair are equal
) {
....
}


But I would try to code some general pattern matching method so that you don't have to hand code this at all.

oh, I didn't even realize that, thanks!
fixed it :D

##### Share on other sites

Well, the array/histogram solution was already brought up several times. A minor variation for maximum laziness, because performance really isn't an issue here uses a map.

for (each card)
{
map[card value]++;
}

if (map.size() == 2 && map.containsValue(3))
fullHouse;

size=1 => 5 of a kind
size=2 && contains(4) => 4 of a kind
size=3 && contains(2) => 2 pairs
size=3 && contains(3) => 3 of a kind
size=4 => 1 pair
size=5 => check for straight



##### Share on other sites

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.

Nerd cred upvote for using popcount intrinsic. (Also for using single-core parallelism.)

##### Share on other sites

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.

##### Share on other sites

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. Edited by Álvaro

##### Share on other sites

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.

##### Share on other sites

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;
}

Edited by Azaral

##### Share on other sites
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.