Three of a kind numbers

Started by
33 comments, last by alvaro 11 years ago

I am creating a method isThreeKind(int[] fiveNumbers) and it should return a boolean if 3 of the 5 are the same. I don't understand how I should go about doing this. I also need to make a similar method but instead do 4 of a kind. If someone could just help me out with understanding the logic that would be great.

Thanks

Advertisement

Sort the array and count the largest number of consecutive equal values in the sorted array.

If it's for poker you need to check for 2 pairs as well and full houses so you need to keep the longest and 2nd longest run counts around.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
One way would be look at the first number and then check the rest of the array for two or more of that number. If there aren't, look at the second number and check the rest of the array, and so on. Another way would be to sort the array first and then see if there or three or more in a row.

Well, you need to test if 3 of 5 numbers are the same.

What you could do is use a nested for loop to iterate through all the numbers and test which is the same with which then have an integer keep track of how many numbers are the same, then return true if it's greater than three. That's just one possible solution. There are probably others.

As you loop through the array, you don't have to go back wards, for example test the first one against the ones after it, then test the second one against the ones after it, but you don't have to retest the first one against the second since you already know the first one failed the test. If you make it to the second to last one, ( only 2 left ) and you haven't found 3 matches yet, you don't have to test the last two since there is only 2 left there can't be 3 matches in 2 objects.

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

If your numbers are smaller than 21 (as they are in poker), you can use 64-bit arithmetic to do this really fast:
bool is_three_of_a_kind(int *a) {
  unsigned long long x = 0ull;
  for (int i=0; i<5; ++i)
    x += 1ull << (a*3);
  return (x + 0111111111111111111111ull) & 0444444444444444444444ull;
  // return x & 0444444444444444444444ull; <-- four of a kind
}

ok i got it!
I sorted the array and simply checked for the number of consecutive numbers that are the same.


thanks for the help guys

Alvaro, please use capital L's instead of lower case l's when doing that ;) At least there is a u inbetween the digits and the l's though, nothing worse than seeing stuff like


long ll = 1l;
long l1 = 11;
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Why not just use a Hash? Something like this (might be missing some corner cases):

int pair_count = 0;
bool three_of_a_kind = false;
bool four_of_a_kind = false;
for (int i=0; i<5; ++i) {
  hash[cards]++;
  if (hash[cards] == 2) pair_count++;
  three_of_a_kind ||= hash[cards] == 3;
  four_of_a_kind ||= hash[cards] == 4;
}
 
// now you can see if it has a two pair, a three of a kind, a four of a kind, or a full house.
 

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

here is my code just so you guys could see what I did


Arrays.sort(player1.diceValues);		
for(int i = 0; i < 3; i++){
	if(player1.diceValues == player1.diceValues[i + 1] && player1.diceValues == player1.diceValues[i + 2]){
		player1.score =+ player1.diceValues * 3;
	}
}

This topic is closed to new replies.

Advertisement