模拟排球比赛

  1 # -*- coding: utf-8 -*-
  2 """
  3 Created on Wed May 15 15:43:44 2019
  4 
  5 @author: h2446
  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()

 # -*- coding: utf-8 -*-
"""
Created on Tue Apr 14 15:07:24 2020

@author: HP
"""

from random import random
from time import time
def printInfo():
    '''
    function: 打印程序的介绍信息
    '''
    print("{:*^70}".format("产品简介"))
    print("产品名称: 排球竞技模拟分析器")
    print("产品概述: 通过输入2个队伍A和B的能力值(0到1之间的小数表示),能够模拟多次2个队伍A和B的排球竞技比赛,从而得出各自的胜率!")
    print("产品作者: 张星星 - 2019310143025")
    print("{:*^70}".format("模拟开始"))
 
def getInputs():
    '''
    function: 获得用户输入的参数
    '''
    probA = eval(input("请输入队伍A的能力值(0~1):"))
    probB = eval(input("请输入队伍B的能力值(0~1):"))
    n = eval(input("请输入需要模拟比赛的场次数:"))
    return probA, probB, n
 
def printResult(n, winsA, winsB):
    '''
    function: 输出模拟比赛的结果
    '''
    print("{:*^70}".format("模拟结束"))
    print("竞技分析开始,共模拟{}场比赛。".format(n))
    print(">>>队伍A获胜{}场比赛,占比{:0.1%}".format(winsA,winsA/n))
    print(">>>队伍B获胜{}场比赛,占比{:0.1%}".format(winsB,winsB/n))
 
def simNGames(n, probA, probB):
    '''
    function: 模拟n场比赛
    n: 模拟n场比赛
    probA, probB: 分别为队伍A和B的能力值
    winA, winB: 队伍A和B在一场比赛中获胜的局数
    winsA, winsB: 队伍A和B赢得比赛的场数,总共n场
    '''
    winsA, winsB = 0, 0
    for _ in range(n):
        winA, winB = simOneGame(probA, probB)
        if winA > winB:
            winsA += 1
        else:
            winsB += 1
    return winsA, winsB
 
def simOneGame(probA, probB):
    '''
    function: 模拟一场比赛,包括五局,采取五局三胜制
    probA, probB: 分别为队伍A和B的能力值
    return: 返回队伍A和B在本场比赛中获胜的局数
    scoreA, scoreB: 分别为队伍A和B一局比赛获得的分数
    winA, winB: 分别为队伍A和B一场比赛获胜的局数
    N: 代表本次比赛的局次
    '''
    winA, winB = 0, 0
    for N in range(5):
        scoreA, scoreB = simAGame(N, probA, probB)
        if scoreA > scoreB:
            winA += 1
        else:
            winB += 1
        if winA == 3 or winB == 3:
            break
    return winA, winB
 
def simAGame(N, probA, probB):
    '''
    function: 模拟一局比赛
    N: 代表本次比赛的局次
    probA, probB: 分别为队伍A和B的能力值
    return: 返回队伍A和B在本局比赛中获得的分数
    '''
    scoreA, scoreB = 0, 0    # 分别为队伍A和B一局比赛获得的分数
    serving = 'A'            # 发球方
    while not GameOver(N, scoreA, scoreB):
        if serving == 'A':
            if random() > probA:
                scoreB += 1
                serving = 'B'
            else:
                scoreA += 1
        else:
            if random() > probB:
                scoreA += 1
                serving = 'A'
            else:
                scoreB += 1
    return scoreA, scoreB
 
def GameOver(N, scoreA, scoreB):
    '''
    function: 定义一局比赛的结束条件
    N: 代表当前局次(第五局为决胜局)
    return: 若比赛结束的条件成立返回真,否则为假
    '''
    if N <= 4:
        return (scoreA>=25 and scoreB>=25 and abs(scoreA-scoreB)>=2)
    else:
        return (scoreA>=15 and abs(scoreA-scoreB)>=2) or (scoreB>=15 and abs(scoreA-scoreB)>=2)
 
if __name__ == "__main__":
    printInfo()
    probA, probB, n = getInputs()
    Time = time()
    winsA, winsB = simNGames(n, probA, probB)
    print("模拟用时: {:.1f}s".format(time()-Time))
    printResult(n, winsA, winsB)

posted @ 2020-04-22 14:55  星星星星星仔  阅读(193)  评论(0)    收藏  举报