任务目标(06号同学)
模拟排球竞技比赛,并且得出模拟结果
比赛规则如下
前4局比赛采用25分制,每个队只有至少赢得25分,并且同时超过对方2分时,才胜一局
正式比赛采用5局3胜制,决胜局的比赛采用15分制,1队先得8分,两队交换场地,按原位置顺序继续进行比赛到结束
在决胜局(第5局)的比赛,先获15分并领先对方2分为胜
输入如下代码
1 # -*- coding: utf-8 -*- 2 """ 3 Spyder Editor 4 5 This is a temporary script file. 6 """ 7 8 from random import random 9 def printIntro(): 10 print("这个程序模拟两个选手A和B的排球竞技比赛") 11 print("程序需要A和B的能力值(0到1之间的小数表示)") 12 def getInputs(): 13 a=eval(input("请输入选手A的能力值(0-1):")) 14 b=eval(input("请输入选手B的能力值(0-1):")) 15 n=eval(input("模拟比赛场次:")) 16 return a,b,n 17 def simNGames(n,probA,probB): 18 winsA,winsB=0,0 19 for i in range(n): 20 scoreA,scoreB=simOneGame(probA,probB) 21 if scoreA>scoreB: 22 winsA+=1 23 else: 24 winsB+=1 25 return winsA,winsB 26 def gameOver(a,b): 27 if (a>=25 and abs(a-b)>=2 )or(b>=25 and abs(a-b)>=2): 28 return True 29 else: 30 return False 31 def simOneGame(probA,probB): 32 scoreA,scoreB=0,0 33 serving="A" 34 while not gameOver(scoreA,scoreB): 35 if serving=="A": 36 if random() < probA: 37 scoreA += 1 38 else: 39 serving="B" 40 else: 41 if random() < probB: 42 scoreB += 1 43 else: 44 serving="A" 45 return scoreA,scoreB 46 def final(probA,probB): 47 winsA,winsB=simNGames1(4,probA,probB) 48 printSummary(winsA,winsB) 49 if not winsA==3 or winsB==3: 50 if winsA==winsB==2: 51 winsA1,winsB1=simOneGame1(probA,probB) 52 finalprintSummary(winsA,winsB) 53 else: 54 finalprintSummary(winsA,winsB) 55 def simNGames1(n,probA,probB): 56 winsA,winsB=0,0 57 for i in range(n): 58 scoreA,scoreB=simOneGame2(probA,probB) 59 if winsA==3 or winsB==3: 60 break 61 if scoreA>scoreB: 62 winsA+=1 63 else: 64 winsB+=1 65 return winsA,winsB 66 def simOneGame2(probA,probB): 67 scoreA,scoreB=0,0 68 serving="A" 69 while not GG(scoreA,scoreB): 70 if serving=="A": 71 if random() < probA: 72 scoreA += 1 73 else: 74 serving="B" 75 else: 76 if random() < probB: 77 scoreB += 1 78 else: 79 serving="A" 80 return scoreA,scoreB 81 def simOneGame1(probA,probB): 82 scoreA,scoreB=0,0 83 serving="A" 84 while not finalGameOver(scoreA,scoreB): 85 if serving=="A": 86 if random() < probA: 87 scoreA += 1 88 else: 89 serving="B" 90 else: 91 if random() < probB: 92 scoreB += 1 93 else: 94 serving="A" 95 return scoreA,scoreB 96 def GG(a,b): 97 return a==3 or b==3 98 def finalGameOver(a,b): 99 if (a==8 or b==8): 100 if a>b: 101 print("A队获得8分,双方交换场地") 102 else: 103 print("B队获得8分,双方交换场地") 104 if (a>=15 and abs(a-b)>=2 )or(b>=15 and abs(a-b)>=2): 105 return True 106 else: 107 return False 108 def finalprintSummary(winsA,winsB): 109 n=winsA+winsB 110 if n>=4: 111 print("进行最终决赛") 112 if winsA>winsB: 113 print("最终决赛由A获胜") 114 else: 115 print("最终决赛由B获胜") 116 else: 117 if winsA>winsB: 118 print("最终决赛由A获胜") 119 else: 120 print("最终决赛由B获胜") 121 def printSummary(winsA,winsB): 122 n=winsA+winsB 123 print("竞技分析开始,共模拟{}场比赛".format(n)) 124 print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA,winsA/n)) 125 print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB,winsB/n)) 126 def main(): 127 printIntro() 128 probA,probB,n=getInputs() 129 winsA,winsB=simNGames(n,probA,probB) 130 printSummary(winsA,winsB) 131 final(probA,probB) 132 main()
输出结果如右下角
使用pyinstaller,将.py文件转换为exe文件