我一直在修改骰子游戏猪的代码。我将它修改为滚动2个骰子而不是一个骰子。当你滚动一个时,它会转到下一个玩家转弯,你的任何一个点都不会与你之前的得分保持一致。好吧当你滚动两个我需要它将分数重置为0并打印出我的消息" Snake Eyes !!你的分数设置为0"。问题是两条信息都打印出来用于蛇眼,当你滚动一条信息时,两条信息都被滚动。分数也不会重置。
编辑*我已使用冲突的打印消息修复了代码,但仍然无法在分割蛇形时将分数重置为0
from random import randint
playercount = 2
maxscore = 100
safescore = [0] * playercount
player = 0
score=0
while max(safescore) < maxscore:
if player == 0:
rolling = 0
if score < 17 and score + safescore[player] < maxscore:
rolling = 1
else:
rolling = input("Player %i: (%i, %i) Rolling? (Y) "
% (player, safescore[player], score)).strip().lower() in {'yes', 'y', ''}
if rolling:
rolled = randint(1, 6)
rolled2 = randint(1, 6)
print(' Rolled %i' % rolled)
print(' Rolled %i' % rolled2)
if rolled ==1 and rolled2 ==1:
print(' Snake Eyes!! your score is set to 0')
score=0
elif rolled == 1:
print(' Bust! you lose %i but still keep your previous %i'
% (score, safescore[player]))
score, player = 0, (player + 1) % playercount
elif rolled2 == 1:
print(' Bust! you lose %i but still keep your previous %i'
% (score, safescore[player]))
score, player = 0, (player + 1) % playercount
else:
score += rolled + rolled2
else:
safescore[player] += score
if safescore[player] >= maxscore:
break
print(' Sticking with %i' % safescore[player])
score, player = 0, (player + 1) % playercount
print('\nPlayer %i wins with a score of %i' %(player, safescore[player]))
答案 0 :(得分:2)
您需要更正支票的顺序。现在你的&#34;滚动了一个&#34;检查发生在&#34;滚动两个&#34;检查,因此当两个滚动时它将执行。如果你把&#34;两个&#34;首先检查并使用else-if结构使两者独占,您将获得所需的行为:
if rolled==1 and rolled2==1:
# snake eyes
elif rolled==1:
# a single one
else:
# all other cases
另外,请注意,您目前只检查两个骰子是否出现一个,或者第一个骰子是否出现一个。你可能还想检查第二个骰子是否出现了。