from random import random
def bsxx():
string1="模拟体育竞技分析模拟 "
string2="模拟乒乓球竞技分析"
string3="乒乓球比赛规则如下:"
string4="一局比赛:"
string5="先得11分的一方为胜方;10平后,先多得2分的一方为胜方。"
string6="一场比赛:"
string7="单打的淘汰赛采用七局四胜制,双打淘汰赛和团体赛采用五局三胜制。"
string8="注意:团体赛由四局单打,一局双打组成"
string9="顺序:一,二,四,五为单打,第三局为双打"
print (string1)
print ()
print (string2)
print (string3)
print (string4)
print (string5)
print (string6)
print (string7)
print (string8)
print (string9)
print ()
def getinputs1():
print ()
print ("您选择的比赛类型是:单打淘汰赛")
print ("为了更好的模拟比赛数据,请输入两位运动员的能力值(0-1之间)")
athlete1= eval ( input ("请输入第一位运动员能力值"))
athlete2= eval ( input ("请输入第二位运动员能力值"))
n = eval ( input ("请输人模拟的场次:"))
return athlete1,athlete2, n
def getinputs2():
print ()
print ("您选择的比赛类型是:双打淘汰赛")
print ("为了更好的模拟比赛数据,请输入四位运动员的能力值(0-1之间)")
print ("请输入团队 A 的运动员能力值")
athlete1= eval ( input ("请输入第一位运动员能力值:"))
athlete2= eval ( input ("请输入第二位运动员能力值:"))
print ("请输入团队 B 的运动员能力值")
athlete3= eval ( input ("请输入第三位运动员能力值:"))
athlete4= eval ( input ("请输入第四位运动员能力值:"))
n = eval ( input ("请输入模拟的场次:"))
aver1=(athlete1+athlete2)/2
aver2=(athlete3+athlete4)/2
return aver1,aver2, n
def getinputs3():
print()
print ("您选择的比赛类型是:团体赛")
print ("为了更好的模拟比赛数据,请输入五局运动员的能力值(0-1之间)")
print ("请按照出场顺序,分别输出运动员的能力值")
print ("请输入团队 A 的运动员能力值")
athlete1= eval ( input ("请输入第一局运动员能力值:"))
athlete2= eval ( input ("请输入第二局运动员能力值:"))
athlete3= eval ( input ("请输入第三局运动员能力值:"))
athlete4= eval ( input ("请输入第四局运动员能力值:"))
athlete5= eval ( input ("请输入第五局运动员能力值:"))
print ("请输入团队 B 的运动员能力值")
athlete6= eval ( input ("请输入第一局运动员能力值:"))
athlete7= eval ( input ("请输入第二局运动员能力值:"))
athlete8= eval ( input ("请输入第三局运动员能力值:"))
athlete9= eval ( input ("请输入第四局运动员能力值:"))
athlete10= eval ( input ("请输入第五局运动员能力值:"))
n = eval ( input ("请输入模拟的场次:"))
return athlete1,athlete2,athlete3,athlete4, athlete5,athlete6,athlete7,athlete8,athlete9,athlete10,n
def oneGame ( N ,ablity1,ablity2):
score1,score2=0,0
i =0
while not gameover (score1,score2):
if i ==0:
for k in range (2):
if random ()>ablity1:
score2+=1
else :
score1+=1
i+=1
if i ==1:
for k in range (2):
if random ()>ablity2:
score1+=1
else :
score2+=1
i-=1
return score1,score2
def allGame (ablity1,ablity2):
N =1
win1,win2=0,0
for i in range (7):
score1,score2= oneGame ( N ,ablity1,ablity2)
if score1>score2:
win1+=1
else :
win2+=1
N +=1
if win14 or win24:
break
return win1,win2
def Allgame ( n , ablity1,ablity2):
wins1,wins2=0,0
for i in range ( n ):
score1,score2= allGame (ablity1,ablity2)
if score1>score2:
wins1+=1
else :
wins2+=1
return wins1,wins2
def TTgame ( n ,A1,A2,A3,A4,A5,B1,B2,B3,B4,B5):
teamA , teamB =0,0
A , B =0,0
for i in range ( n ):
a1,b1= Allgame (1000,A1,B1)
if a1>b1:
A +=1
else :
B +=1
a2,b2= Allgame (1000,A2,B2)
if a2>b2:
A +=1
else :
B +=1
a3,b3= Allgame (1000,A3,B3)
if a3>b3:
A +=1
else :
B +=1
if A >=3:
teamA +=1
A , B =0,0
elif B >=3:
teamB +=1
A , B =0,0
else :
a4,b4= Allgame (1000,A4,B4)
if a4>b4:
A +=1
else :
B +=1
if A >=3:
teamA +=1
A,B =0,0
elif B >=3:
teamB +=1
A , B =0,0
else :
a5,b5= Allgame (1000,A5,B5)
if a5>b5:
A +=1
else :
B +=1
if A >=3:
teamA +=1
A , B =0,0
elif B >=3:
teamB +=1
A , B =0,0
return teamA , teamB
def gameover ( a , b ):
if a >=11 and ( a - b )>=2:
return a
if b >=11 and ( b - a )>=2:
return b
def alysis ( n ,win1,win2):
print ("模拟竞赛分析开始,共模拟{}场比赛". format(n))
print ("athlete1单打获得{}场胜利,共占比{:.2f}%". format(win1,win1/n100))
print("athlete2单打获得{}场胜利,共占比{:.2f}%". format(win2,win2/n100))
print("分析完毕")
def alysis2( n ,win1,win2):
print ("模拟竞赛分析开始,共模拟{}场比赛". format(n))
print ("团队 A 双打获得{}场胜利,共占比:{:.2f}%" . format(win1,win1/n100))
print ("团队 B 双打获得{}场胜利,共占比{:.2f}%". format(win2,win2/n100))
print ("分析完毕")
def alysis1( n , teamA , teamB ):
print ("模拟竞赛分析开始,共模拟{}场比赛". format(n))
print ("团队 A 获得{}场胜利,共占比{:.2f}%". format(teamA,teamA/n100))
print ("团队 B 获得{}场胜利,共占比{:.2f}%". format(teamB,teamB/n100))
print ("分析完毕")
def danda ():
ablity1,ablity2, n =getinputs1()
win1,win2= Allgame ( n ,ablity1,ablity2)
print ("模拟单打:")
alysis ( n ,win1,win2)
def shuangda ():
averablity1,averablity2, n =getinputs2()
win1,win2= Allgame ( n ,averablity1,averablity2)
print ("模拟双打:")
alysis2( n ,win1,win2)
def tuanti():
A1,A2,A3,A4,A5,B1,B2,B3,B4,B5, n = getinputs3()
teamA , teamB = TTgame ( n ,A1,A2,A3,A4,A5,B1,B2,B3,B4,B5)
alysis1( n , teamA , teamB )
def mian ():
bsxx()
x =1
while x 1:
print ("请选择模拟比赛类型")
print ("一、单打淘汰赛")
print ("二、双打淘汰赛")
print ("三、团体赛")
print ("四,结束模拟分析")
choice1= eval ( input ("请输入对应比赛类型的阿拉伯数字编号:"))
if choice11:
danda ()
print ()
elif choice12:
shuangda ()
print ()
elif choice13:
tuanti ()
print ()
elif choice1==4:
print ("模拟程序结束")
break
else :
print ("输入错误,请重新输入")
print ()
mian ()