(1)模拟体育竞技分析:(不同学号选做不同题目,必做题)
b.采用排球比赛规则(学号尾号为4,5,6同学必做及格题)。赛制规定:
前4局比赛采用25分制,每个队只有赢得至少25分,并同时超过对方2分时 ,才胜1局。
正式比赛采用5局3胜制,决胜局的比赛采用15分制,一队先得8分后,两队交换场区,按原位置顺序继续比赛到结束。
在决胜局(第五局)之比赛,先获15分并领先对方2分为胜。
代码如下:
from random import random
def printInfo():
'''
function: 打印程序的介绍信息
'''
print(" 排球竞技模拟分析器")
print("22信计1班14号张菀芸")
def getInputs():
'''
function: 获得用户输入的参数
'''
probA = eval(input("请输入队伍A的能力值(0~1):"))
probB = eval(input("请输入队伍B的能力值(0~1):"))
n = eval(input("请输入需要模拟比赛的场次数:"))
return probA, probB, n
def simNGames(n, probA, probB):
'''
function: 模拟n场比赛
n: 模拟n场比赛
probA, probB: 分别为队伍A和B的能力值
winA, winB: 队伍A和B在一场比赛中获胜的局数
winsA, winsB: 队伍A和B赢得比赛的场数,总共n场
'''
winsA, winsB = 0, 0
for _ in range(n):
winA, winB = simOneGame(probA, probB)
if winA > winB:
winsA += 1
else:
winsB += 1
return winsA, winsB
def simOneGame(probA, probB):
'''
function: 模拟一场比赛,包括五局,采取五局三胜制
probA, probB: 分别为队伍A和B的能力值
return: 返回队伍A和B在本场比赛中获胜的局数
scoreA, scoreB: 分别为队伍A和B一局比赛获得的分数
winA, winB: 分别为队伍A和B一场比赛获胜的局数
'''
N = 1 # 代表本次比赛的局次
winA, winB = 0, 0
for _ in range(5):
scoreA, scoreB = simAGame(N, probA, probB)
if scoreA > scoreB:
winA += 1
else:
winB += 1
N += 1
if winA == 3 or winB == 3:
break
return winA, winB
def simAGame(N, probA, probB):
'''
function: 模拟一局比赛
N: 代表本次比赛的局次
probA, probB: 分别为队伍A和B的能力值
return: 返回队伍A和B在本局比赛中获得的分数
'''
scoreA, scoreB = 0, 0 # 分别为队伍A和B一局比赛获得的分数
serving = 'A' # 发球方
while not GameOver(N, scoreA, scoreB):
if serving == 'A':
if random() > probA:
scoreB += 1
serving = 'B'
else:
scoreA += 1
if serving == 'B':
if random() > probB:
scoreA += 1
serving = 'A'
else:
scoreB += 1
return scoreA, scoreB
def GameOver(N, scoreA, scoreB):
'''
function: 定义一局比赛的结束条件
N: 代表当前局次(第五局为决胜局)
return: 若比赛结束的条件成立返回真,否则为假
'''
if N <= 4:
return (scoreA>=25 and scoreB>=25 and abs(scoreA-scoreB)>=2)
else:
return (scoreA>=15 and abs(scoreA-scoreB)>=2) or (scoreB>=15 and abs(scoreA-scoreB)>=2)
def printResult(n, winsA, winsB):
'''
function: 输出模拟比赛的结果
'''
print("竞技分析开始,共模拟{}场比赛。".format(n))
print(">>>队伍A获胜{}场比赛,占比{:0.1%}".format(winsA,winsA/n))
print(">>>队伍B获胜{}场比赛,占比{:0.1%}".format(winsB,winsB/n))
if __name__ == "__main__":
printInfo()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printResult(n, winsA, winsB)
(2)采用篮球或者足球规则模拟比赛,分析体育竞技规则,模拟比赛。(良好),基本要求同上。
采用乒乓球、羽毛球、篮球、足球等规则中的一种。同时采用循环赛或者晋级赛形式,模拟分析4个队及以上体育竞技,并输出排名。建议可以采用面向对象方法或者改动上面程序。
class Team:
def __init__(self, name):
self.name = name
self.points = 0
def win_match(self):
self.points += 3
def draw_match(self):
self.points += 1
def lose_match(self):
pass
def __str__(self):
return f"Team: {self.name}, Points: {self.points}"
class Match:
def __init__(self, team1, team2, result):
self.team1 = team1
self.team2 = team2
self.result = result
def play_match(self):
if self.result == "win":
self.team1.win_match()
self.team2.lose_match()
elif self.result == "draw":
self.team1.draw_match()
self.team2.draw_match()
elif self.result == "lose":
self.team1.lose_match()
self.team2.win_match()
def simulate_tournament(teams, matches):
for match in matches:
match.play_match()
sorted_teams = sorted(teams, key=lambda x: x.points, reverse=True)
for i, team in enumerate(sorted_teams):
print(f"Rank {i+1}: {team}")
team1 = Team("Team A")
team2 = Team("Team B")
team3 = Team("Team C")
team4 = Team("Team D")
match1 = Match(team1, team2, "win")
match2 = Match(team2, team3, "draw")
match3 = Match(team3, team4, "lose")
match4 = Match(team4, team1, "lose")
teams = [team1, team2, team3, team4]
matches = [match1, match2, match3, match4]
simulate_tournament(teams, matches)
标签:比赛,scoreB,排球比赛,self,probB,模拟,体育竞技,def,scoreA From: https://www.cnblogs.com/vinden/p/17913140.html