采用排球比赛规则
点击查看代码
from random import random
def printInfo(): # 打印程序介绍信息
print('这个程序模拟两个选手A和B的某种竞技比赛')
print('程序运行需要A和B的能力值(以0到1之间的小数表示)')
def getInputs(): # 获得程序运行参数
a = eval(input('请输入选手A的能力值(0-1):'))
b = eval(input('请输入选手B的能力值(0-1):'))
n = eval(input('模拟比赛场次:'))
return a, b, n
def simOneGame(probA, probB, final): # 进行一场比赛
scoreA, scoreB = 0, 0 # 初始化AB的得分
serving = 'A' # 首先由A发球
while not gameOver(scoreA, scoreB, final): # 用while循环来执行比赛
if serving == 'A':
if random() < probA: # random() 方法返回随机生成的一个实数,它在[0,1)范围内。
scoreA += 1 # 用随机数来和能力值比较从而分出胜负
else:
serving = 'B'
else:
if random() < probB:
scoreB += 1
else:
serving = 'A'
return scoreA, scoreB
def simNGames(n, probA, probB): # 进行N场比赛
winsA, winsB = 0, 0 # 初始化AB的胜场数
for i in range(n):
winsOneA, winsOneB = 0, 0
for j in range(4): #前四局
scoreA, scoreB = simOneGame(probA, probB, False)
if scoreA > scoreB:
winsOneA += 1
else:
winsOneB += 1
scoreA, scoreB = simOneGame(probA, probB, True) #决胜局
if scoreA > scoreB:
winsOneA += 1
else:
winsOneB += 1
if winsOneA > winsOneB: #判断一大局谁获胜
winsA += 1
else:
winsB += 1
return winsA, winsB
def gameOver(scoreA, scoreB, final): # 比赛结束
if final is False: #前四局
return scoreA == 25 or scoreB == 25
elif final is True: #决胜局
if (scoreA >= 15 and abs(scoreA - scoreB) >= 2) or (scoreB >= 15 and abs(scoreB - scoreA) >= 2):
return True
def printSummary(n, winA, winB): # 打印比赛结果
print('竞技分析开始,共模拟{}场比赛'.format(n))
print('选手A获胜{}场比赛,占比{:.2f}%'.format(winA, winA / n * 100))
print('选手B获胜{}场比赛,占比{:.2f}%'.format(winB, winB / n * 100))
def main():
printInfo()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(n, winsA, winsB)
main()
采用篮球规则模拟比赛
点击查看代码
from random import random
def printIntro():
print("这个程序模拟两个队伍A和B的篮球比赛")
print("程序运行需要A和B队伍的平均能力值(以0到1之间的小数表示)")
def getInput():
probA=eval(input("请输入队伍A的平均能力值(0-1):"))
probB=eval(input("请输入队伍B的平均能力值(0-1):"))
n=eval(input("模拟比赛场次:"))
return probA,probB,n
def Games(n,probA,probB): #全部比赛场次的各胜方总数分析
A,B=0,0
for i in range(n):
a=fourGames(probA,probB)
if a=="A":
A+=1
else:
B+=1
return A,B
def fourGames(probA,probB): #一场比赛的胜利方判断
scoreA,scoreB=0,0
for i in range(4):
scoreA,scoreB=onejieGame(probA,probB)
if scoreA>scoreB:
return 'A'
return 'B'
def onejieGame(probA,probB): #一小节比赛的比分总结
scoreA,scoreB=0,0
serving="A"
while not gameOver(scoreA,scoreB):
a=random()
if serving=="A":
if a<probA:
scoreA+=2
else:
serving="B"
else:
if a<probB:
scoreB+=2
else:
serving="A"
return scoreA,scoreB
def gameOver(a,b):
return a==16 or b==16
def printsummary(A,B):
n=A+B
print("竞技开始分析,共模拟了{}场比赛".format(n))
print("选手A获胜{}场比赛,占比{:0.1%}".format(A,A/n))
print("选手B获胜{}场比赛,占比{:0.1%}".format(B,B/n))
print("分析人:Yong No.10")
def main():
printIntro()
probA,probB,n=getInput()
A,B=Games(n,probA,probB)
printsummary(A,B)
main()
input()
采用乒乓球
点击查看代码
from random import random
def printIntro():
print("这个程序模拟两个选手A和B的兵乓球比赛")
print("程序运行需要A和B选手的能力值(以0到1之间的小数表示)")
def getInput():
probA=eval(input("请输入选手A的能力值(0-1):"))
probB=eval(input("请输入选手B的能力值(0-1):"))
n=eval(input("模拟比赛场次:"))
return probA,probB,n
def simGames(n,probA,probB): #比赛双方各自胜利的场次数
winsA,winsB=0,0
for i in range (n):
c=simGame(probA,probB)
if c=='A':
winsA+=1
else:
winsB+=1
return winsA,winsB
def simGame(probA,probB): #判断一场比赛的胜方
winsA1,winsB1=0,0
for i in range(7):
scoreA,scoreB=simoneGame(probA,probB)
if scoreA>scoreB:
winsA1+=1
else:
winsB1+=1
if winsA1>=4:
return 'A'
return 'B'
def simoneGame(probA,probB): #统计一场比赛下来的各自得分数
scoreA,scoreB=0,0
serving="A"
while not gameOver(scoreA,scoreB):
x=random()
if serving=="A":
if x < probA:
scoreA+=1
else:
serving="B"
else:
if x < probB:
scoreB+=1
else:
serving="A"
return scoreA,scoreB
def gameOver(a,b):
if (a>=11 and b<11) or (b>=11 and a<11) or (a>9 and b>9 and ((a-b)>=2 or (b-a)>=2)):
return True
return False
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))
print("分析人:Yong No.10")
def main():
printIntro()
probA,probB,n=getInput()
winsA,winsB=simGames(n,probA,probB)
printsummary(winsA,winsB)
main()