模拟体育竞技分析--羽毛球

发布时间 2023-11-21 08:11:16作者: 企鹅不耐热

羽毛球比赛规则:

1. 21 分制,3局2胜为佳 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬

2. 每球得分制 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬

3. 每回合中,取胜的一方加 1 分 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬

4. 当双方均为 20 分时,领先对方 2 分的一方赢得该局比赛 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬

5. 当双方均为 29 分时,先取得 30 分的一方赢得该局比赛 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬

6. 一局比赛的获胜方在下一局率先发球

from random import random
def printIntro():
print("该程序模拟两个选手A和B的羽毛球比赛")
print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
print("规则:三局两胜--21分制")
print('学号38')
def getInputs():
a=eval(input("A的能力值:"))
b=eval(input("B的能力值:"))
n=eval(input("模拟比赛的场次:"))
return a,b,n
def simNGames(n,probA,probB):
winsA, winsB = 0, 0
for i in range(n):
scoreA, scoreB = simOneGame(probA, probB)
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
return winsA,winsB
def gameOver(a,b):
if(a>=20 or b>=20):
if(abs(a-b)==2 and a<=29 and b<=29):
return True
else:
return a==30 or b==30
else:
return False
def simOneGame(probA,probB):
scoreA, scoreB=0, 0
serving="A"
while not gameOver(scoreA,scoreB):
if serving=="A":
if random()<probA:
scoreA += 1
else:
serving="B"
else:
if random()<probB:
scoreB += 1
else:
serving="A"
return scoreA,scoreB
def printSummary(winsA,winsB):
n=winsA+winsB
print("竞技分析开始,共模拟{}场比赛".format(n))
print("A获胜{}场比赛,共占比{:0.1%}".format(winsA,winsA/n))
print("B获胜{}场比赛,共占比{:0.1%}".format(winsB,winsB/n))

def main():
printIntro()
probA,probB,n=getInputs()
winsA,winsB=simNGames(n,probA,probB)
printSummary(winsA,winsB)
main()

代码运行结果如下: