Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Three of a kind numbers


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
34 replies to this topic

#21 Álvaro   Crossbones+   -  Reputation: 13649

Like
2Likes
Like

Posted 01 April 2013 - 05:31 PM

*reads Alvaro's post* *checks name of forum*
 
OK, it's "For Beginners" ;) Probably a tad too advanced for a simple Yahtzee game...

Oooops! smile.png

I have to agree: A beginner should use an array of counters (a.k.a. histogram) instead of the crazy octal sum I posted. I thought the problem was interesting, I spent some time thinking about it and then I completely forgot what forum this was on. Sorry about that...

Edited by Álvaro, 01 April 2013 - 05:32 PM.


Sponsor:

#22 Paradigm Shifter   Crossbones+   -  Reputation: 5407

Like
0Likes
Like

Posted 01 April 2013 - 05:42 PM

I agree, it is interesting, and worth it if for thinking about AI for more complex games.

 

If the Earth ever gets invaded by aliens who will destroy the Earth unless we can utilise the power of all of our computers in a rapid fire Yahtzee tournament, we know who they can turn to ;)


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#23 LorenzoGatti   Crossbones+   -  Reputation: 2735

Like
0Likes
Like

Posted 02 April 2013 - 04:19 AM

I would simply count the number of occurrences of each value, compiling what other posts rightly call an histogram, without sorting the values; then you can scan the occurrence counts to find values with the appropriate number of occurrences. With few possible values, like in your dice game, the histogram can be represented as a dense array of counts, indexed by values; with many possible values an array would have a lot of wasted slots with a count of 0, if not too many slots to fit into memory. For large sets of allowed values you should therefore implement the same histogram abstract data type (initialize to 0 occurrences for all values; given a value, increment the corresponding occurrence count by 1; produce all value/occurrence count pairs with at least 1 occurrence) using a hash table, a search tree, or another efficient data structure that uses memory proportional to the number of different values in the actual input rather than proportional to the number of possible values.
Produci, consuma, crepa

#24 Paradigm Shifter   Crossbones+   -  Reputation: 5407

Like
0Likes
Like

Posted 02 April 2013 - 08:13 AM

I was only suggesting an array of size 6 (number of 1s rolled, number of 2s rolled, etc.).

 

I wasn't suggesting a 6x6x6x6x6 array with all possible values (size would be 7776 entries).

 

That's because in Yahtzee you don't have to score the best possible roll (and you can only score a result once). You could do it by storing a bitfield of which combinations are allowed for each roll (so 5 of a kind is also 4 of a kind and a full house, whatever), but then you still have to work out the legal combinations anyway (which is the purpose of the question in the OP)...


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#25 nitishk   Members   -  Reputation: 161

Like
0Likes
Like

Posted 02 April 2013 - 04:09 PM

WOW
thanks for so much feedback and help guys

 

I kind of ended up using a combination of what you guys said. Sorted it and then loop through it but I think my way was a bit inefficient but I do not mind for now. Any feedback on how I did my full house code?

 

Arrays.sort(player1.diceValues);
				
if(player1.diceValues[0] == player1.diceValues[1] && player1.diceValues[0] == player1.diceValues[2]){
	if(player1.diceValues[3] == player1.diceValues[4]){
		player1.score = player1.score + 25;
	}
}else if(player1.diceValues[2] == player1.diceValues[3] && player1.diceValues[2] == player1.diceValues[4]){
	if(player1.diceValues[0] == player1.diceValues[1]){
	player1.score = player1.score + 25;
	}
}


#26 Brother Bob   Moderators   -  Reputation: 8429

Like
1Likes
Like

Posted 02 April 2013 - 05:52 PM

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, 02 April 2013 - 05:52 PM.


#27 radioteeth   Prime Members   -  Reputation: 1103

Like
0Likes
Like

Posted 02 April 2013 - 05:58 PM

AHHHHHHHHHHHHHHHHHHHHHHHH



#28 nitishk   Members   -  Reputation: 161

Like
0Likes
Like

Posted 02 April 2013 - 08:20 PM

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



#29 Trienco   Crossbones+   -  Reputation: 2208

Like
0Likes
Like

Posted 02 April 2013 - 09:05 PM

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
 

f@dzhttp://festini.device-zero.de

#30 Khatharr   Crossbones+   -  Reputation: 3030

Like
0Likes
Like

Posted 03 April 2013 - 02:46 AM

 

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


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#31 jms bc   Members   -  Reputation: 439

Like
0Likes
Like

Posted 11 April 2013 - 09:05 AM

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.


#32 Álvaro   Crossbones+   -  Reputation: 13649

Like
0Likes
Like

Posted 11 April 2013 - 09:41 AM

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, 11 April 2013 - 09:42 AM.


#33 Chad Smith   Members   -  Reputation: 1139

Like
0Likes
Like

Posted 11 April 2013 - 02:53 PM

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



#34 Azaral   Members   -  Reputation: 463

Like
0Likes
Like

Posted 11 April 2013 - 03:41 PM

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[i] ]++;
}

 

 

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, 11 April 2013 - 04:12 PM.


#35 Álvaro   Crossbones+   -  Reputation: 13649

Like
0Likes
Like

Posted 11 April 2013 - 08:35 PM

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[i]-1)*3); // Increment the appropriate counter within sum
      types |= 1 << (dice[i]-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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS