当前位置: 移动技术网 > IT编程>脚本编程>Python > 预测球队比赛成绩

预测球队比赛成绩

2020年04月19日  | 移动技术网IT编程  | 我要评论

反叛的鲁路修之萨拉的降临,名门纪事,生活大爆炸第十季

本文尝试采用自顶向下的设计方法进行体育竞技分析。自顶向下将一个复杂问题分解成几个问题,再细分成一个个具体的小问题,从而来解决复杂问题。

一、采用乒乓球比赛规则(学号尾号为3必做题)

比赛规则:(1)一局比赛:在一局比赛中,先得11分的一方为胜方:10平后,先多得2分的一方为胜方。

(2)一场比赛:单打的淘汰赛为七局四胜制,双打淘汰赛或团体赛为五局三胜制。

1、将体育竞技分析分解为以下几个小步骤

1.1打印程序的介绍性信息式

1.2获得程序运行参数:proba(a的能力值),probb(b的能力值),n(比赛场次)

1.3利用球员ab的能力值,模拟n场比赛

1.4输出球员ab获胜的场次及概率

1.5用pyinstaller打包可执行文件

 

2、将各个步骤定义成函数来实现

 

2.1 主体函数:

def main():
    printintro()
    proba, probb, n = printinputs()
    winsa, winsb = simngames(n, proba, probb)
    printsummary(winsa, winsb)

2.2 自定义函数:

#打印程序介绍信息
def printintro():
    print("19信计2班23号邓若言")
    print("这个程序模拟两个选手a和b的乒乓球比赛")
    print("程序运行需要a和b的能力值(以0到1之间的小数表示)")

#获得程序运行参数
def printinputs():
    a = eval(input("请输入选手a的能力值(0-1): "))
    b = eval(input("请输入选手b的能力值(0-1): "))
    n = eval(input("模拟比赛的场次: "))
    return a, b, n

# 进行n场比赛
def simngames(n, proba, probb):
    winsa, winsb = 0, 0
    for i in range(n):
        for j in range(7):           #进行7局4胜的比赛
            scorea, scoreb = simonegame(proba, probb)
            if scorea > scoreb:
                winsa += 1
            else:
                winsb += 1
    return winsa,winsb


 #进行一场比赛
def simonegame(proba, probb):
    scorea, scoreb = 0, 0           #初始化ab的得分
    serving = "a"                 
    while not gameover(scorea, scoreb):     #用while循环来执行比赛
        if scorea==10 and scoreb==10:
            return(simonegame2(proba,probb))
        if serving == "a":
            if random() < proba:            ##用随机数生成胜负
                scorea += 1
            else:
                serving="b"
        else:
            if random() < probb:
                scoreb += 1
            else:
                serving="a"
    return scorea, scoreb

def simonegame2(proba,probb):
    scorea,scoreb=10,10
    serving = "a"
    while not gameover2(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 gameover(a,b):               #正常比赛结束
    return a==11 or b==11
def gameover2(a,b):              #进行抢12比赛结束
   if abs((a-b))>=2:
       return a,b


#输出数据
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))

  

2.3 合并

from random import random

#打印程序介绍信息
def printintro():
    print("19信计2班23号邓若言")
    print("这个程序模拟两个选手a和b的乒乓球比赛")
    print("程序运行需要a和b的能力值(以0到1之间的小数表示)")

#获得程序运行参数
def printinputs():
    a = eval(input("请输入选手a的能力值(0-1): "))
    b = eval(input("请输入选手b的能力值(0-1): "))
    n = eval(input("模拟比赛的场次: "))
    return a, b, n

# 进行n场比赛
def simngames(n, proba, probb):
    winsa, winsb = 0, 0
    for i in range(n):
        for j in range(7):           #进行7局4胜的比赛
            scorea, scoreb = simonegame(proba, probb)
            if scorea > scoreb:
                winsa += 1
            else:
                winsb += 1
    return winsa,winsb


 #进行一场比赛
def simonegame(proba, probb):
    scorea, scoreb = 0, 0           #初始化ab的得分
    serving = "a"                 
    while not gameover(scorea, scoreb):     #用while循环来执行比赛
        if scorea==10 and scoreb==10:
            return(simonegame2(proba,probb))
        if serving == "a":
            if random() < proba:            ##用随机数生成胜负
                scorea += 1
            else:
                serving="b"
        else:
            if random() < probb:
                scoreb += 1
            else:
                serving="a"
    return scorea, scoreb

def simonegame2(proba,probb):
    scorea,scoreb=10,10
    serving = "a"
    while not gameover2(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 gameover(a,b):               #正常比赛结束
    return a==11 or b==11
def gameover2(a,b):              #进行抢12比赛结束
   if abs((a-b))>=2:
       return a,b


#输出数据
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 = printinputs()
    winsa, winsb = simngames(n, proba, probb)
    printsummary(winsa, winsb)

main()

  

单人赛预测结果如下: 

 

 

 当该比赛是双人赛或者团体赛时,将 自定义函数simngames(n,proa,prob)  稍作修改,改动如下:

# 进行n场比赛
def simngames(n, proba, probb):
    winsa, winsb = 0, 0
    for i in range(n):
        for j in range(5):           #进行5局3胜的比赛
            scorea, scoreb = simonegame(proba, probb)
            if scorea > scoreb:
                winsa += 1
            else:
                winsb += 1
    return winsa,winsb

结果如下:

2.4 打包可执行文件

在控制台输入  pyinstaller  -f  xxx(pyw文件路径,我的是 d://我爱学习/python3/作业/moni.py)

可在对应路径下找到exe文件:

 二、采用篮球比赛规则

比赛规则:

(1)篮球比赛由两个队参加,每队出场5名队员。每队目标是在对方球篮得分,并阻止对方队在本方球篮得分。

(2)篮球比赛由裁判员、记录台人员和技术代表(如到场)管理。

(3)被某队进攻的球篮是对方的球篮,由某队防守的球篮是本方的球篮。

(4)在比赛时间结束时得分较多的队,将是比赛的胜者。

 分析步骤基本同上,仅模拟比赛的自定义函数 simngames(n,proa,prob) ,simonegame(proa,prob)和gameover(a,b) 不同。 改动如下:

# 进行n场比赛
def simngames(n, proba, probb):
    winsa, winsb = 0, 0
    for i in range(n):
        scorea, scoreb = simonegame(proba, probb)
        if scorea > scoreb:
            winsa += 1
        else:
            winsb += 1
    return winsa,winsb


 #进行一场比赛
def simonegame(proba, probb):
    scorea, scoreb = 0, 0           #初始化ab的得分
    serving = "a"                 
    while not gameover(scorea, scoreb):     #用while循环来执行比赛
        if serving == "a":
            if random() < proba:            ##用随机数生成胜负
                scorea += 1
            else:
                serving="b"
        else:
            if random() < probb:
                scoreb += 1
            else:
                serving="a"
    return scorea, scoreb


#比赛结束
def gameover(a,b):
    return a>b or b>a

 合并后效果如下:

 

 

 

 

 

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网