如何使这个功能倒数

时间:2016-07-20 12:35:53

标签: python artificial-intelligence

这个getNextPlayer()功能可以向前发展,但我想让它适用于偶尔需要向后计数的纸牌游戏。

def GetNextPlayer(self, p):
    """ Return the player to the left of the specified player, skipping players who have been knocked out
    """
    next = (p % self.numberOfPlayers) + 1
    # Skip any knocked-out players
    while next != p and self.knockedOut[next]:
        next = (next % self.numberOfPlayers) + 1
    return next

它来自http://www.aifactory.co.uk/newsletter/ISMCTS.txt发现的gard游戏脚本,是更大的蒙特卡罗树搜索算法的一部分。我尝试了next=(p%self.numberOfPlayers)-1,但它产生了无效值

2 个答案:

答案 0 :(得分:1)

仅将+1更改为-1会产生无效值,因为modulo运算符会忽略您执行0 - 1 % self.numberOfPlayers的情况下的符号。例如。 -1 % 4 == 3

更新,感谢@pwnsauce ,这应该可以满足您的需求:

p - 1 if p >= 1 else self.numberOfPlayers - 1

这假设玩家指数从0开始并转到self.numberOfPlayers-1

答案 1 :(得分:0)

您可以使用以下内容:

def GetNextPlayer(self, p, forward=True):
    """ Return the player to the left of the specified player, skipping players who have been knocked out
    """
    def get_next():
        ref = p if forward else p + self.numberOfPlayers - 1
        return p + 1

    next = get_next()
    # Skip any knocked-out players
    while next != p and self.knockedOut[next]:
        next = get_next()
    return next

我已将引用(从0开始计数)和periodic属性(模运算)分开。并且它也使其成为后向和前向的通用