• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
chosenkill6

Three of a kind numbers

34 posts in this topic

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
1

Share this post


Link to post
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

0

Share this post


Link to post
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
 
0

Share this post


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

0

Share this post


Link to post
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.

0

Share this post


Link to post
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
0

Share this post


Link to post
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;
  }
};
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.

0

Share this post


Link to post
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[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
0

Share this post


Link to post
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[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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0