科学设计方法论

一:羽毛球比赛设计

a.函数介绍

函数名称 函数说明
printInfo()
打印程序的介绍信息
getInputs()
获得用户输入的参数
printResult(n, winsA, winsB)
输出模拟比赛的结果
simNGames(n, probA, probB)
模拟n场比赛
simOneGame(probA, probB)
模拟一场比赛,包括五局,采取五局三胜制
simAGame(N, probA, probB)
模拟一局比赛
GameOver(N, scoreA, scoreB)
定义一局比赛的结束条件

b.羽毛球比赛规则 计分

  1. 21 分制,3局2胜为佳2. 每球得分制3. 每回合中,取胜的一方加 1 分

  2. 当双方均为 20 分时,领先对方 2 分的一方赢得该局比赛

  3. 当双方均为 29 分时,先取得 30 分的一方赢得该局比赛

  4. 一局比赛的获胜方在下一局率先发球

from random import random
def printIntro():
    print("27号程序模拟选手A,B,C,D进行左右边羽毛球比赛(27)")
    print("27号程序运行需要A,B,C,D的能力值(0-1)")
def getInputs():
    z=eval(input("请输入左场次选手的能力值(0-1):"))
    y=eval(input("请输入右场次选手的能力值(0-1):"))
    n=eval(input("模拟比赛的场次:"))
    return z,y,n
def simNGames(n,probZ,probY):
    winsZ,winsY=0,0
    for i in range(n):
        scoreZ,scoreY=simOneGame(probZ,probY)
        if scoreZ>scoreY:
            winsZ+=1
        else:
            winsY+=1
    return winsZ,winsY
def gameOver(z,y):
    if(z-y==2 and z==20 and y==20):
        return True
    elif(y-z==2 and z==20 and y==20):
        return True
    elif(z==29 and y==29):
        if(z-y==1 or y-z==1):
            return True
    else:
        return False
def simOneGames(probZ,probY):
    scoreZ,scoreY=0,0
    serving="Z"
    while not gameOver(scoreZ,scoreY):
        if serving=="Z":
            if random()<probZ:
                scoreZ+=1
            else:
                serving="Y"
        else:
            if random()<probY:
                scoreY+=1
            else:
                serving="Z"
    return scoreZ,scoreY
def printSummary(winsZ,winsY):
    n=winsZ+winsY
    print("竞技分析开始,共模拟()场比赛".format(n))
    print("选手C,D获胜{}场比赛,占比{:0.1%}".format(winsZ,winsZ/n))
    print("选手A,B获胜{}场比赛,占比{:0.1%}".format(winsY,winsY/n))
def main():
    printIntro()
    probZ,probY,n=getInputs()
    winsZ,winsY=simNGames(n,probZ,probY)
    printSummary(winsZ,winsY)
main()

二:足球比赛设计

1. 简介: 模拟不同的两个队伍进行足球的模拟比赛。

2. 模拟原理: 通过输入各自的能力值(Ⅰ),模拟比赛的进行( P ),最后输出模拟的结果( O )。

P 简介:通过产生随机数得到半场比赛的回合数,再通过产生随机数得到每回合比赛的难度,若小于能力值则表示赢得本局比赛,反之输掉本场比赛。

3. 规则简介:

① 比赛分为两半场,每场为45分钟。

  上半场: 一方挑选进攻的球门,另一方获得开球权;

  下半场: 互换攻守方向,上半场没获得开球权的一方获得开球权。

②在进球后开球时,开球方为失球一方。

③ 比赛结束时得分多的球队获胜,如果两队得分相同或均未得分,比赛为平局。

from random import random
def printIntro():
    print("27号程序模拟选手A,B进行足球比赛(27)")
    print("27号程序运行需要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):
        scoreA,scoreB=simOneGame(probA,probB)
        if scoreA>scoreB:
            winsA+=1
        else:
            winsB+=1
    return winsA,winsB
def simOneGame(probA,probB):
    scoreA,scoreB=0,0
    serving="A"
    while not gameOver(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(scoreA,scoreB):
    return scoreA==15 or scoreB==15
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()

三:晋级模拟

from random import random
def printIntro():
    print("27号程序模拟选手A,B进行足球比赛(27)")
    print("27号程序运行需要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):
        scoreA,scoreB=simOneGame(probA,probB)
        if scoreA>scoreB:
            winsA+=1
        else:
            winsB+=1
        if winsA >=4 or winsB >= 4:
                break
    return winsA,winsB
def simOneGame(probA,probB):
    scoreA,scoreB=0,0
    serving="A"
    while not gameOver(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(scoreA,scoreB):
    return (scoreA >= 11 and abs(scoreA-scoreB)>=2) or (scoreB >= 11 and abs(scoreA-scoreB)>=2)
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()

 四:打包

posted @ 2019-05-13 16:07  简笺  阅读(329)  评论(0)    收藏  举报