首页 > 其他 > 详细

科学设计方法论

时间:2019-05-13 16:45:10      阅读:125      评论:0      收藏:0      [点我收藏+]

一:羽毛球比赛设计

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()

技术分享图片

二:足球比赛设计

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()

技术分享图片

 四:打包

技术分享图片

科学设计方法论

原文:https://www.cnblogs.com/wjaihui/p/10855455.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!