我编辑了我之前的问题,因为我想出了我认为正确的代码。这背后的逻辑应该是:当这盘比赛还没有结束,也不是平局时 10:10:选手 A 开始发球,不管他是否赢分,发球两次,然后选手 B 接发球,也发球两次。它一直持续到该盘结束,除了当服务器改变每个得分时,10:10 平局。
任何人都可以检查代码是否完美无缺?谢谢你。
def simOneSet(probA, probB):
serving = "A"
scoreA = scoreB = 0
while not setOver(scoreA, scoreB):
if scoreA != 10 and scoreB != 10:
if serving == "A":
for i in range(2):
if random() < probA:
scoreA += 1
else:
scoreB += 1
serving = "B"
else:
for i in range(2):
if random() < probB:
scoreB +=1
else:
scoreA += 1
serving = "A"
# when there is a tie 10:10
else:
if serving == "A":
if random() < probA:
scoreA += 1
serving = "B"
else:
scoreB += 1
serving = "B"
else:
if random() < probB:
scoreB += 1
serving = "B"
else:
scoreA += 1
serving = "A"
return scoreA, scoreB