from random import * class Info: def __init__(self): print('2019310143114') print('这个程序是模拟两支队伍A和B的排球比赛') print('程序运行需要A和B的能力值(以0到1之间的小数表示)') def GetInput(self): A = eval(input('请输入队伍A的能力值(0-1):')) B = eval(input('请输入队伍B的能力值(0-1):')) N = eval(input('模拟比赛场次:')) return A, B, N class OneGame: def simOneGame(self, probA, probB): A_win, B_win, count = 0, 0, 1 while not (A_win == 3 or B_win == 3): serving = choice(['A', 'B']) scoreA, scoreB = 0, 0 while not OneGame.Gameover(count, scoreA, scoreB): if serving == 'A': if random() < probA: scoreA += 1 else: serving = 'B' else: if random() < probB: scoreB += 1 else: serving = 'A' if scoreA > scoreB : A_win += 1 else: B_win += 1 if count == 5: count = 1 count += 1 return A_win, B_win @classmethod def Gameover(self, count, scoreA, scoreB): if count < 5: return (scoreA >= 25 or scoreB >= 25) and abs(scoreA - scoreB) >= 2 else: return (scoreA >= 15 or scoreB >= 15) and abs(scoreA - scoreB) >= 2 class NGame(OneGame): def simNGames(self, n, A, B): winsA, winsB = 0, 0 for i in range(n): win_numA, win_numB = self.simOneGame(A, B) if win_numA > win_numB: winsA += 1 else: winsB += 1 return winsA, winsB class PrintSummary: def __init__(self, N, winA, winB): print('竞技分析开始,共模拟{}场比赛'.format(N)) print('队伍A获胜{}场比赛,占比{:.2f}%'.format(winA, winA/N * 100)) print('队伍B获胜{}场比赛,占比{:.2f}%'.format(winB, winB/N * 100)) match_info = Info() A, B, N = match_info.GetInput() match = NGame() A_win, B_win = match.simNGames(N, A, B) PrintSummary(N, A_win, B_win) input("please input any key to exit!")
比赛结果
用pyinstall库将程序封装
在控制台输入 —> pyinstaller -F D:桌面pytestGAME.py