3 of a kind

Hi guys,

I'm creating a little game just as a side project to help me further my learning with c++ and I am stuck on what i feel like is a simple logic problem.

At this stage in the game i have 5 numbers and i want to check for 3 of a kind (3 of the 5 numbers being the same). I'm not really sure how to address this. I only got as far as having my method which is given the 5 numbers and returns true if there is three of a kind or false otherwise. I also started with creating a vector and an iterator with the idea of looping through the numbers in order to check them but this is where i get stuck. I don't know what the loop needs to do, any help would be greatly appreciated.
This a good time to learn recursion. Google "recursion c++". :)
If you need to know that there are exactly three of kind then you can write the code the following way


1
2
3
4
5
6
bool three_of_kind( int a, int b, int c, int d, int e )
{
   return ( ( ( a == b ) + ( a == c ) + ( a == d ) + ( a == e ) ) == 2 ||
            ( ( b == a ) + ( b == c ) + ( b == d ) + ( b == e ) ) == 2 ||
            ( ( c == a ) + ( c == b ) + ( c == d ) + ( c == e ) ) == 2 );
}

Last edited on
It will be even shorter to write the function the following way

1
2
3
4
5
6
bool three_of_kind( int a, int b, int c, int d, int e )
{
   return ( ( ( a == b ) + ( a == c ) + ( a == d ) + ( a == e ) ) == 2 ||
            ( ( b == c ) + ( b == d ) + ( b == e ) ) == 2 ||
            ( ( c == d ) + ( c == e ) ) == 2 );
}


Sorry, I am wrong. This code is not equivalent to the previous one.:)
Last edited on
Topic archived. No new replies allowed.