Jump to content

Problem getting possible combinations of 2 cards in a card deck

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
AzaraT

AzaraT

    Newbie

  • Members
  • PipPip
  • 13 posts
Hello,

I am currently trying to develop an application where I need to find all combinations of 2 cards in a standard card deck of 52 cards. These combinations will be generated in four nested for loops.

When you calculate the possible combinations using simple math you will get:

Quote

52! / ( 2!(52-2)! ) = 1326

now my code currently looks like this:


        for (int c3_1 = 2; c3_1 <= 14; c3_1++){ // first cards value


            for (int c3_2 = 1; c3_2 <= 4; c3_2++) { // first card suit


                for (int c4_1 = 2; c4_1 <=14; c4_1++){ // second card value


                    for (int c4_2 = 1; c4_2 <=4; c4_2++){ // second card suit

                        


                        // check if the cards are the same

                        if (c3_1 == c4_1 && c3_2  == c4_2){


                            break;

                        }

                        else {

                           card_count++;

                       }

                    }


                }


            }


        }



however when I print out the card count I get 2574, which is way to many combinations. As I've shown you it should only be 1326.

I allready done so the generated cards cant be the same.

So how do I do this? anyone know what the problem is here?

Thanks alot!
Jacob

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Your break condition is wrong. You don't want to check if they're the same, you want to check if the first card is "greater than or equal to" the second card.

You're listing all possible permutations.
                        if (c3_1 >= c4_1 || (c3_1 == c4_2 && c3_2  >= c4_2){

                            break;
                        }
                        else {
                           card_count++;
                       }

Of course, you could make this much easier by simply doing:
for (card1 = 1; card1 <= 52; card1++)
  for (card2 = card1+1; card2 <= 52; card2++)
    count++;

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
AzaraT

AzaraT

    Newbie

  • Members
  • PipPip
  • 13 posts
Thank you for helping :)

I am, however, now getting a value of 1086, and it should be, as explained in the original post, 1326.

So while it is closer to the target, there is still a small problem.

Any idea why?

I need to know the cards exact suit and value in differnt variables so it is not easyier to do it on the other way.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
If you do card in 0-51, card/13 +2 gives you the rank, and card % 13 gives you the suit.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'll have to test later, but is there any other code that may be interfering?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts

WingedPanther said:

If you do card in 0-51, card/13 +2 gives you the rank, and card % 13 gives you the suit.

Surely it's the other way around?

card%13 will give results from 0 - 12 and will therefore be the rank while card/13 will give 0 - 3 and will therefore be the suit.

I used the following loop to produce the correct result.

[highlight="C++"]
int combi_count = 0;

for (int card1 = 0; card1 < 13; card1++)
for (int card2 = 0; card2 < 13; card2++)
if (card1 < card2)
combi_count++;
[/highlight]

The if statement ensures that every combination is considered with producing duplicates (ie you don't end up with As,Ac as well as Ac,As).
If there's a new way, I'll be the first in line.

But, it better work this time.

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
That's what I get for typing faster than I think.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
AzaraT

AzaraT

    Newbie

  • Members
  • PipPip
  • 13 posts

Hignar said:

Surely it's the other way around?

card%13 will give results from 0 - 12 and will therefore be the rank while card/13 will give 0 - 3 and will therefore be the suit.

I used the following loop to produce the correct result.

[highlight="C++"]
int combi_count = 0;

for (int card1 = 0; card1 < 52; card1++)
for (int card2 = 0; card2 < 52; card2++)
if (card1 < card2)
combi_count++;
[/highlight]

The if statement ensures that every combination is considered with producing duplicates (ie you don't end up with As,Ac as well as Ac,As).

Thank you, this did the work for me.

One thing however, I didn't quite get how I can get the suit. Fx. 51/13 = 3.9231, and 0/13 = 0.076923

If I round these numbers up/down (how do i even do that?) i have values from 0-4 ? five values?

#9
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts

AzaraT said:

Thank you, this did the work for me.

One thing however, I didn't quite get how I can get the suit. Fx. 51/13 = 3.9231, and 0/13 = 0.076923

If I round these numbers up/down (how do i even do that?) i have values from 0-4 ? five values?

If the value of the card is defined as an integer then the rounding is done automatically in most languages so values between 0 and 12 will return 0, 13 to 25 will return 1 etc all you need to do then is assign the values 0 -3 to the different suits.

Note that you'll never get 4 as a result from this as the card values are 0 to 51 rather than 1 to 52.
If there's a new way, I'll be the first in line.

But, it better work this time.