Card game algorithm for unique combinations

I am writing a small card game in which two players each have 4 cards in their hands and must take as many cards to the table as they can. He uses classic poker cards, the same seeds and the same values.

King can only take another King
Queen can only take another Queen 
Jack can only take another Jack

      

Numeric cards can be accepted by amount, for example, for example:

10H takes 6C + 4S
5S takes 3C + 2D or 3S + 2D
7S takes 4S + 3S or 5C + 2D
And so on...

      

Seeds are not relevant ... only meanings. But the problem is I need to calculate unique combinations. So, for example, I only want one of these combos since the seeds and values ​​are the same:

10H -> 6C + 4S
10H -> 4S + 6C

      

Is there any ready-made function for this? I tried looking around Google and Wikipedia, but I probably don't know the name of the algorithm / problem in English. Aw ... I forgot ... the solution could be whatever you want (simple, recursive, linq).

+3


source to share


1 answer


The combinations you are trying to calculate are called whole sections . Accordingly, you are following the integer partitioning algorithm .

A python solution might look like this:



def bubble(part, i=0): #add one to the list whilst keeping lexicographic order
    if i == (len(part)-1) or part[i] < part[i+1]:
        part[i] += 1
        return part
    else:
        return bubble(part, i+1)

def partitions(n):
    if n == 1:
        yield [1]
        return
    for p in partitions(n-1):
        yield [1] + p
        yield bubble(p)

      

This is conceptually similar to Knuth's algorithm in bundle 3B .

+2


source







All Articles