Three of a kind numbers

Started by
33 comments, last by alvaro 11 years, 1 month ago

i dont really understand what a hash is and I would like to keep this game simple. By the way this is for a game of yahtzee not poker

Hashtable.

In java, that'd be in java.util.Hashtable

Advertisement

With Yahtzee you might be better having an array of size 6 and store a count of how many dice are rolled of each number. That makes checking the combinations and scoring them really easy.

Can't you still have a full house and 2 pairs in Yahtzee too?

Ten Thousand/Farkle/Dix Mille is a much better dice game though I reckon, especially with the 4 or more 2's = disaster lose all your banked points rule.

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

I think this is the easiest way if you have no need to sort:

int same=0;

for(int i=0;i<4;++i)

for(int j=i+1;j<5;++j)

if(a == a[j])

same++;

same = 1 is a pair

same = 3 is a three of a kind

same = 6 is a quad

same = 10 is 5 of a kind

added:

same = 2 is two pair

same = 4 is a full house

The Four Horsemen of Happiness have left.

You could also use histograms as they allow you to check all of the other constraints on placing a roll really quickly. 3 of a kind one histogram bar is at least 3, with 4 it goes to 4, with a large straight all of them are 1, small straight at least four connecting number bars are 1 or more.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Yeah that's what I was suggesting with the array of 6 entries, use it as a histogram.

I thought a small straight was 1-2-3-4-5 and a large straight was 2-3-4-5-6?

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

Just for fun, a (beginner) Haskell implementation:


unique :: (Eq a) => [a] -> [a]
unique []     = []
unique (x:xs) = x : unique (filter (\y -> not (x==y)) xs)

nOcurrences :: (Eq a) => a -> [a] -> Int
nOcurrences elem list = length (filter (==elem) list)

isThreeKind :: (Eq a) => [a] -> Bool
isThreeKind list = elem 3 [nOcurrences i list | i <- (unique list)]

You could parametrize the 3 number in "isThreeKind" function and have a "fourKind", a "fiveKind", or whatever...

Cheers.

Yeah that's what I was suggesting with the array of 6 entries, use it as a histogram.

I thought a small straight was 1-2-3-4-5 and a large straight was 2-3-4-5-6?

Not according to wikipedia: http://en.wikipedia.org/wiki/Yahtzee

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Looks like I've been playing a weird Yacht/Yahtzee hybrid then ;) I also thought Yacht was the same game as Yahtzee, you learn something new every day ;)

Since I mentioned 10,000 someone should do a physics based simulation of the remarkably similar game "Pass the Pigs" http://en.wikipedia.org/wiki/Pass_the_pigs

EDIT: And I've never heard of the Kissin' Bacon (both snouts touching) rule before. It's not in my ruleset!

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
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.

*reads Alvaro's post* *checks name of forum*

OK, it's "For Beginners" ;) Probably a tad too advanced for a simple Yahtzee game...

I suggested a sort and count consecutive approach (since it is scalable for more complex problems) until I found out it was Yahtzee where I feel a histogram based approach is going to be simplest to implement and understand.

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

This topic is closed to new replies.

Advertisement