有一个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游戏脚本,是更大的montecarlo树搜索算法的一部分。我尝试了next=(p%self.numberOfPlayers)-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
你可以用这样的方法:
我已经分离了引用(从0开始的计数)和周期性属性(模运算)。也使它通用于向后和向前
相关问题 更多 >
编程相关推荐