首页 > 其他分享 >体育模拟竞技

体育模拟竞技

时间:2023-12-28 22:34:45浏览次数:25  
标签:scoreA scoreB 体育 random else score 竞技 prob 模拟

模拟乒乓球比赛

import random #引用random库

def sportgame():
    print("Welcome to the sportgame")
    print("这个程序将模拟乒乓球比赛")
#介绍程序

def InputPlayer():
    player1 = eval(input("请输入运动员A能力值:"))
    player2 = eval(input("请输入运动员B能力值:"))
    return player1, player2
#创建InputPlayer函数:输入运动员能力值,并返回

def Gameover(scoreA,scoreB):
    if scoreA == 11 and scoreB <= 9:
        return scoreA
    elif scoreB ==11 and scoreA <= 9:
        return scoreB
    elif scoreA == scoreB ==10:
        if scoreA - scoreB == 2:
            return scoreA
        elif scoreA - scoreB == 2:
            return scoreB
#Gameover函数:单局乒乓球比赛的胜利条件

def ScoringRule(probA,probB):
    scoreA = 0
    scoreB = 0
    serving = "A"
    while not Gameover(scoreA,scoreB):#调用Gameover函数,保证比赛的进行
        if serving == "A":
            if random.random() < probA:
                scoreA += 1
            else:
                serving = 'B'
        else:
            if random.random() < probB:
                scoreB += 1
            else:
                serving = 'A'
    return scoreA,scoreB
#ScoringRule函数:记录单局乒乓球中的得分

def Singlegame(probA,probB):
    winsA = winsB = 0
    for i in range(7):
        scoreA, scoreB = ScoringRule(probA,probB)#调用ScoringRule函数,比较得分得出胜局
        if scoreA > scoreB:
            winsA = winsA + 1
            if winsA == 4 and winsB <=3:
                print('A获胜')
            else:
                pass
        else:
            winsB = winsB + 1
            if winsB == 4 and winsA <=3:
                print('B获胜')
            else:
                pass
    return winsA,winsB
#Singlegame函数:乒乓球单打比赛,采用七局四胜

def printSummary(winsA, winsB):
    n = winsA + winsB
    print("竞技分析开始,共模拟{}场比赛".format(7))
    print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA / 7))
    print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB / 7))

def main():
    sportgame()
    probA, probB = InputPlayer()
    winsA, winsB = Singlegame(probA,probB)
    printSummary(winsA, winsB)

main()

篮球

from random import random

countdown = 0


def get_inputs():
    while True:
        try:
            foul_a_score, prob_a, prob_a3 = input('请输入 A 队的参数,用逗号隔开').split(',')
            foul_bscore, prob_b, prob_b3 = input("请输入 B 队的参数,用逗号隔开").split(',')
            foul_a_score = float(foul_a_score)
            foul_bscore = float(foul_bscore)
            prob_a = float(prob_a)
            prob_b = float(prob_b)
            prob_a3 = float(prob_a3)
            prob_b3 = float(prob_b3)
            break
        except:
            print('您的输入有误,请重新输入')
            continue
    return (foul_a_score, prob_a, prob_a3), (foul_bscore, prob_b, prob_b3)


def foul(score_a, score_b, foulAscore, foulBscore):
    side = random()
    if side > 0.5:
        if score_a > random():
            score_a += 1
        else:
            score_b += 1
    else:
        if score_b > random():
            score_b += 1
        else:
            score_a += 1
    return score_a, score_b


def simOneGame(argA, argB, ser):
    global countdown
    scoreA, scoreB = 0, 0
    fawl = 0.3
    serving = ser
    i = 1
    countdown = 50
    foulAscore = argA[0]
    probA = argA[1]
    probA3 = argA[2]
    foulBscore = argB[0]
    probB = argB[1]
    probB3 = argB[2]
    while not gameOver(scoreA, scoreB):
        judge = random()
        if judge < fawl:
            scoreA, scoreB = foul(scoreA, scoreB, foulAscore, foulBscore)
        else:
            if serving == 'A':
                if random() < probA:
                    scoreA += 3 if probA3 > random() else 2
                else:
                    scoreB += 3 if probB3 > random() else 2
            else:
                if random() < probB:
                    scoreB += 3 if probB3 > random() else 2
                else:
                    scoreA += 3 if probA3 > random() else 2
    print(scoreA, '--', scoreB)
    return Winner(scoreA, scoreB)


def gameOver(scoreA, scoreB):
    global countdown
    if countdown == 0:
        if scoreA == scoreB:
            countdown += 5
            return False
        else:
            return True
    else:
        countdown -= 1
        return False


def switchServing(serving):
    if serving == 'A':
        serving = 'B'
    else:
        serving = 'A'
    return serving


def Winner(scoreA, scoreB):
    return 'A' if scoreA > scoreB else 'B'


def simOneChampion():
    B = 0
    A = 0
    round = 1
    argA, argB = get_inputs()
    serving = 'A'
    while True:
        print('第{}节'.format(round))
        r = simOneGame(argA, argB, serving)
        serving = switchServing(serving)
        round += 1
        if r == 'A':
            A += 1
        else:
            B += 1
        if A == 3:
            print('A 队胜出')
            break
        elif B == 3:
            print('B 队胜出')
            break
        else:
            continue


simOneChampion()

标签:scoreA,scoreB,体育,random,else,score,竞技,prob,模拟
From: https://www.cnblogs.com/88888888b/p/17933729.html

相关文章

  • mac m1 编译cocos2d-x 在模拟器上运行 一些问题汇总
     如果涉及到侵权请联系本人删除 1  》〉/Users/yzfhkms-m/Library/Developer/Xcode/DerivedData/formi-dlcfwgxcmidqefdkxnvnfwfprpfs/Build/Products/Debug-iphonesimulator/formi-mobile.appisnotavalidpathtoanexecutablefile.Pleaserebuildtheprojectto......
  • 模拟体育分析
     fromrandomimportrandomdefprintInfo():'''function:打印程序的介绍信息'''print("通过输入2个队伍A和B的能力值(0到1之间的小数表示),能够模拟多次2个队伍A和B的排球竞技比赛,从而得出各自的胜率!")print("信计1班罗天智15")defgetInputs():'&#......
  • py123——模拟体育竞技分析:乒乓球比赛
    模拟体育竞技分析:一.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬采用乒乓球比赛规则一局比赛:‪‬......
  • go-使用通道模拟抢到活动
    packagemainimport( "fmt" "gorm.io/driver/mysql" "gorm.io/gorm" "math/rand" "sync" "time")//用户结构体typeUserstruct{ Idint Namestring Telstring Addressstring}......
  • 排球比赛模拟
    importrandomdefprint_intro():print("排球比赛模拟程序")print("-------------------------------")definput_teams():team1_ability=float(input("请输入第一个球队的能力值:"))team2_ability=float(input("请输入第二个球队的能力值:"))returnteam1......
  • 【K 个一组翻转链表】模拟
    leetcode25.K个一组翻转链表假设当前需要反转的子链表为[curHead,curTail]curDummy:当前需要反转的子链表的虚拟节点curHead:当前需要反转的子链表的头节点curTail:当前需要反转的子链表的尾节点找到尾节点curTail反转子链表[curHead,curTail](反转子链表解法参考反转子链......
  • 模拟实现strstr函数的代码——《初学C语言第40天》
    //////模拟实现strstr////(字符中的元素是连续存放的,所以不会存在跳动存放的情况,例如a1="ababcd",a2="ac"此结果就是NULL)////情况1.arr1="abcd"arr2="abc"(一次匹配就找到了)////情况2.arr1="ababcd"arr2="abc"(多次匹配才能找到)//#include<stdio.h>//#in......
  • 模拟赛简要题解
    11.16(C0389)100+10+50=160,rk3。本来BC都应该写出来的。A:dp或贪心都可以,贪心直接从下往上覆盖即可。B:注意:这里的\(\oplus\)指的是按位或。合法条件可以化简为:\(\oplus_{i=1}^{p}a_i=\oplus_{i=p+1}^{n}a_i\)。继续挖掘。看到位运算肯定想到拆位,考虑每一位第一次和......
  • 模拟体育竞技分析
    模拟体育竞技分析‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬a.采用乒乓球比赛规则(学号尾号为0,1,2......
  • VISSIM模拟上海松江新城小区道路开放及交通状况改善分析
    分析师:MingmingZhao上海市近年来不断增长的高峰出行车辆数,带来了交通负荷严重等城市问题,本项目主要探究开放封闭式小区能否实现对道路交通状况的改善。对道路交通状况的综合分析基于对道路不同时间段、不同位置交通状况的全面且真实客观的评价,鉴于道路状况受不同类型因素影响的多......