c# - 独特组合的纸牌游戏算法

标签 c# algorithm combinations game-engine playing-cards

我正在编写一个小型纸牌游戏,其中两名玩家手中各有 4 张牌,他们必须按总和尽可能多地拿走 table 上的牌。它使用经典的扑克牌,所以相同的种子和相同的值。

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)。

最佳答案

您尝试计算的组合称为 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 .

关于c# - 独特组合的纸牌游戏算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14457575/

相关文章:

javascript - 列表排列

一组随机整数的组合 C 代码

c# - 当应用于大量数据时,SQL CLR 聚合无法正确终止

c# - 什么时候应该使用 TaskCompletionSource<T>?

c++ - AVL树实现c++

java - 统计超大文本文件的词频

ruby - 欧拉计划 1 :Find the sum of all the multiples of 3 or 5 below 1000

algorithm - 选择具有最小总体差异的数字对

c# - 如何在 Unity 中启用 MSAA?

c# - 获取一个 COM 对象以在 C# 中对除 S_OK (0) 以外的任何结果抛出异常