体育竞技预测

一.体育竞技分析的IPO模式:

  输入I(input):两个球员的能力值,模拟比赛的次数(其中,运动员的能力值,可以通过发球方赢得本回合的概率来表示,

                一个能力值为0.8的球员,在他发球时,有80%的可能性赢得1分)

  处理P(process):模拟比赛过程

  输出O(output):两个球员获胜的概率

二.模拟排球的比赛:

前4局比赛采用25分制,每个队只有赢得至少25分,并同时超过对方2分时 ,才胜1局。正式比赛采用5局3胜制,决胜局的比赛采用15分制,一队先得8分后,两队交换场区,按原位置顺序继续比赛到结束。在决胜局(第五局)的比赛,先获15分并领先对队2分为胜。

我的代码如下:

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
from random import random
def printIntro():
    print("这个程序模拟两个选手A和B的某种竞技比赛")
    print("this xin ji 05 pipishe")
    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 simNGames(n, probA, probB):
    winsA, winsB = 0, 0
    for i in range(n):
        if wuju(probA,probB):
            winsA += 1
        else:
            winsB += 1
    return winsA, winsB
def wuju(probA,probB):
    a=0
    b=0
    for i in range(4):
        scoreA, scoreB = simOneGame(4,probA, probB)
        if scoreA > scoreB:
            a+=1
        else:
            b+=1
    if a>b:
        return 1
    elif b>a:
        return 0
    else:
        scoreA, scoreB = simOneGame(5,probA, probB)
        if scoreA > scoreB:
            return 1
        else:
            return 0
def gameOver(n,a,b):
        if a>=25 and b>=25:
            if a-b>2 or b-a>2:
                return 1
        if n==5:
            if a>=25 and b>=25:
                if a-b>2 or b-a>2:
                    return 1
def simOneGame(n,probA, probB):
    scoreA, scoreB = 0, 0
    serving = "A"
    while not gameOver(n,scoreA, scoreB):
        if serving == "A":
            if random() < probA:
                scoreA += 1
            else:
                serving="B"
        else:
            if random() < probB:
                scoreB += 1
            else:
                serving="A"
    return scoreA, scoreB
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))
def main():
    printIntro()
    probA, probB, n = getInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(winsA, winsB)
main()

结果如下:

猜你喜欢

转载自www.cnblogs.com/0330lgs/p/10868767.html