我编写了以下python脚本来实现我的游戏nims / stones
版本def nims_stones(pile, max_stones):
while pile != 0:
move = 0
while move < 1 or move > max_stones:
move = int(raw_input("Player 1 How Many Stones"))
pile -= move
if pile == 0:
print "Player 1 wins"
else:
print "There are %s stones left." %(pile)
move = 0
while move < 1 or move > max_stones:
move = int(raw_input("Player 2 How Many Stones"))
pile -= move
if pile == 0:
print "Player 2 wins"
else:
print "There are %s stones left." %(pile)
print "Game Over"
当我调用函数nims_stones(10,5)它似乎工作但是在玩家一或玩家二赢之后它没有退出循环它不打印&#34;游戏结束&#34 ;它只是要求下一步行动
我不知道为什么在玩家获胜后它不会退出循环。任何帮助将不胜感激。
答案 0 :(得分:2)
当玩家1清空了堆时,你应该停止循环。由于第二个播放器的代码几乎相同,请考虑重用代码。然后你还会在循环结束时进行空桩检查:
def nims_stones(pile, max_stones):
player = 2
while pile != 0:
player = 3 - player
move = 0
while move < 1 or move > max_stones or move > pile:
move = int(raw_input("Player %i. How Many Stones" % (player)))
pile -= move
print ("There are %s stones left." %(pile))
print ("Player %i wins" % (player))
print ("Game Over")
NB /我还添加了条件move > pile
,以避免玩家获得超过可用的数量。
答案 1 :(得分:0)
在每位玩家获胜后添加休息语句将解决您的问题。您还应该考虑在桩达到负值时添加逻辑。
[EstMdl,EstParamCov,logL,info] = estimate(Mdl,TimeSeries_i);