2013-01-22 31 views
3

我正在寫一個小型的紙牌遊戲,其中兩名玩家手中各有4張紙牌,他們必須在桌上採取儘可能多的紙牌。它使用經典的撲克牌,所以種子和價值相同。獨特組合的紙牌遊戲算法

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

編號卡可以通過總和取,因此,例如:

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

種子是不相關的...只有值。但問題是我需要計算獨特的組合。因此,舉例來說,我只希望這些連擊的一個種子和值是相同的:

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

是否有任何預製功能做到這一點?我試着用谷歌和維基百科四處尋找,但可能我不知道英文中的算法/問題的名稱。唉...我忘記了......解決方案可以是任何你想要的(簡單,遞歸,linq)。

回答

2

您嘗試計算的組合稱爲integer partitions。相應地,你在integer partition algorithm之後。

Python中的解決方案可能是這樣的:

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) 

這在概念上類似於Knuth的algorithm P in fascicle 3B

+0

看起來像是,但沒有maxCount限制。 –