質問

I'm writing a little card game in which two players have 4 cards each in their hands and they must take as much cards on the table as they can by sum. It's using the classic poker cards, so same seeds and same values.

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

Numbered cards can take by sum, so 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 values are. But the problem is that I need to calculate unique combinations. So, for example I only want one of those combos as seeds and values are the same:

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

Is there any premade function to do this? I tried looking around with Google and Wikipedia but probably I don't know the name of the algorithm/problem in english. Aw... I was forgetting... solution can be whatever you want (plain, recursive, linq).

役に立ちましたか?

解決

The combinations you are trying to compute are called integer partitions. Correspondingly you are after an integer partition algorithm.

A solution in python might look like:

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 P in fascicle 3B.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top