1 采用5局3胜制
2. 前四局采用25分制,每个队只有在赢得至少25分,且同时超过对方2分时才胜一局
3. 决胜局(第五局)采用15分制,先获得15分,且同时超过对方2分为胜
28 probB = eval(input("请输入队伍B的能力值(0~1):"))
29 n = eval(input("请输入需要模拟比赛的场次数:"))
30 return probA, probB, n
31
32 def printResult(n, winsA, winsB):
33 '''
34 function: 输出模拟比赛的结果
35 '''
36 print("{:*^70}".format("模拟结束"))
37 print("竞技分析开始,共模拟{}场比赛。".format(n))
38 print(">>>队伍A获胜{}场比赛,占比{:0.1%}".format(winsA,winsA/n))
39 print(">>>队伍B获胜{}场比赛,占比{:0.1%}".format(winsB,winsB/n))
40
41 def simNGames(n, probA, probB):
42 '''
43 function: 模拟n场比赛
44 n: 模拟n场比赛
45 probA, probB: 分别为队伍A和B的能力值
46 winA, winB: 队伍A和B在一场比赛中获胜的局数
47 winsA, winsB: 队伍A和B赢得比赛的场数,总共n场
48 '''
49 winsA, winsB = 0, 0
50 for _ in range(n):
51 winA, winB = simOneGame(probA, probB)
52 if winA > winB:
53 winsA += 1
54 else:
55 winsB += 1
56 return winsA, winsB
57
58 def simOneGame(probA, probB):
59 '''
60 function: 模拟一场比赛,包括五局,采取五局三胜制
61 probA, probB: 分别为队伍A和B的能力值
62 return: 返回队伍A和B在本场比赛中获胜的局数
63 scoreA, scoreB: 分别为队伍A和B一局比赛获得的分数
64 winA, winB: 分别为队伍A和B一场比赛获胜的局数
65 N: 代表本次比赛的局次
66 '''
67 winA, winB = 0, 0
68 for N in range(5):
69 scoreA, scoreB = simAGame(N, probA, probB)
70 if scoreA > scoreB:
71 winA += 1
72 else:
73 winB += 1
74 if winA == 3 or winB == 3:
75 break
76 return winA, winB
77
78 def simAGame(N, probA, probB):
79 '''
80 function: 模拟一局比赛
81 N: 代表本次比赛的局次
82 probA, probB: 分别为队伍A和B的能力值
83 return: 返回队伍A和B在本局比赛中获得的分数
84 '''
85 scoreA, scoreB = 0, 0 # 分别为队伍A和B一局比赛获得的分数
86 serving = 'A' # 发球方
87 while not GameOver(N, scoreA, scoreB):
88 if serving == 'A':
89 if random() > probA:
90 scoreB += 1
91 serving = 'B'
92 else:
93 scoreA += 1
94 else:
95 if random() > probB:
96 scoreA += 1
97 serving = 'A'
98 else:
99 scoreB += 1
100 return scoreA, scoreB
101
102 def GameOver(N, scoreA, scoreB):
103 '''
104 function: 定义一局比赛的结束条件
105 N: 代表当前局次(第五局为决胜局)
106 return: 若比赛结束的条件成立返回真,否则为假
107 '''
108 if N <= 4:
109 return (scoreA>=25 and abs(scoreA-scoreB)>=2) or (scoreB>=25 and abs(scoreA-scoreB)>=2)
110 else:
111 return (scoreA>=15 and abs(scoreA-scoreB)>=2) or (scoreB>=15 and abs(scoreA-scoreB)>=2)
112
113 if __name__ == "__main__":
114 printInfo()
115 probA, probB, n = getInputs()
116 Time = time()
117 winsA, winsB = simNGames(n, probA, probB)
118 print("模拟用时: {:.1f}s".format(time()-Time))
119 printResult(n, winsA, winsB)