首页 > 编程语言 >羽毛球比赛python

羽毛球比赛python

时间:2023-12-28 16:12:12浏览次数:34  
标签:羽毛球 比赛 python wins self team1 team2 else score

import random
import os

print("2班 17向悦")
# 介绍比赛以及程序
def print_introduce():
print("This is a badminton game simulation program")
print("The program requires two players' ability values (expressed in decimals from 0 to 1)")
print("The last two student number: 19")
'''
羽毛球比赛规则
1. 21分制,3局2胜为佳
2. 每球得分制
3. 每回合中,取胜的一方加1分
4. 当双方均为20分时,领先对方2分的一方赢得该局比赛
5. 当双方均为29分时,先取得30分的一方赢得该局比赛
6. 一局比赛的获胜方在下一局率先发球
'''


# 获得输入
def get_inputs():
a = eval(input("Please enter the ability value of player A: "))
b = eval(input("Please enter the ability value of player B: "))
return a, b


# 模拟n场比赛
def sim_n_games(pow_a, pow_b):
a_wins, b_wins = 0, 0
while not race_over(a_wins, b_wins):
a_score, b_score = sim_one_game(pow_a, pow_b)
if a_score > b_score:
a_wins += 1
else:
b_wins += 1
return a_wins, b_wins


# 单局比赛结束
def game_over(a, b):
if (a == 21 and b < 20) or (b == 21 and a < 20):
return True
elif 20 <= a < 30 and 20 <= b < 30 and abs(a-b) == 2:
return True
elif a == 30 or b == 30:
return True


# 比赛结束
def race_over(a, b):
return a == 2 or b == 2


# 模拟单局比赛
def sim_one_game(pow_a, pow_b):
score_a, score_b = 0, 0
serving = 'A'
while not game_over(score_a, score_b):
if serving == 'A':
if random.random() < pow_a:
score_a += 1
else:
score_b += 1
serving = 'B'
else:
if random.random() < pow_b:
score_b += 1
else:
score_a += 1
serving = 'A'
return score_a, score_b


# 输出结果
def print_summary(a_wins, b_wins):
print(f"A : B --- {a_wins}:{b_wins}")
if a_wins > b_wins:
print("Player A took the lead in winning two games and won.")
else:
print("Player B took the lead in winning two games and won.")


def main():
print_introduce()
a_pow, b_pow = get_inputs()
wins_a, wins_b = sim_n_games(a_pow, b_pow)
print_summary(wins_a, wins_b)


main()
os.system('pause')

 

# 定义一个羽毛球队类,包含队名和能力值属性
class BadmintonTeam:
def __init__(self, name, ability):
self.name = name
self.ability = ability


# 定义一个羽毛球比赛类,包含两个队伍和比赛结果属性
class BadmintonMatch:
def __init__(self, team1, team2):
self.team1 = team1
self.team2 = team2
self.result = None # 比赛结果,格式为 (team1的局数, team2的局数)

# 定义一个模拟一局比赛的方法,返回获胜的队伍
def simulate_one_game(self):
score1 = 0 # team1的得分
score2 = 0 # team2的得分
serve = 1 # 发球方,1表示team1,2表示team2
while True:
# 生成一个0到1之间的随机数,表示本回合的胜率
import random
prob = random.random()
# 根据能力值和发球方,计算team1的胜率
win_rate = self.team1.ability / (self.team1.ability + self.team2.ability)
if serve == 2:
win_rate = 1 - win_rate
# 根据胜率和随机数,判断本回合的胜方
if prob < win_rate:
winner = 1 # team1胜
else:
winner = 2 # team2胜
# 根据胜方,更新得分和发球方
if winner == 1:
score1 += 1
serve = 1
else:
score2 += 1
serve = 2
# 判断是否达到结束条件,即一方达到21分或者双方均为29分
if score1 == 21 or score2 == 21 or (score1 == 29 and score2 == 29):
break
# 判断是否达到延长条件,即双方均为20分或者双方均为30分
if (score1 == 20 and score2 == 20) or (score1 == 30 and score2 == 30):
# 延长比赛,直到一方领先2分
while abs(score1 - score2) < 2:
# 重复上述步骤
prob = random.random()
win_rate = self.team1.ability / (self.team1.ability + self.team2.ability)
if serve == 2:
win_rate = 1 - win_rate
if prob < win_rate:
winner = 1
else:
winner = 2
if winner == 1:
score1 += 1
serve = 1
else:
score2 += 1
serve = 2
break
# 返回获胜的队伍
if score1 > score2:
return self.team1
else:
return self.team2

# 定义一个模拟三局两胜的方法,返回比赛结果
def simulate_best_of_three(self):
game1 = self.simulate_one_game() # 模拟第一局
game2 = self.simulate_one_game() # 模拟第二局
if game1 == game2: # 如果前两局同一队伍获胜,直接返回结果
self.result = (2, 0) if game1 == self.team1 else (0, 2)
return self.result
else: # 如果前两局不同队伍获胜,模拟第三局
game3 = self.simulate_one_game()
if game3 == game1: # 如果第三局和第一局同一队伍获胜,返回结果
self.result = (2, 1) if game1 == self.team1 else (1, 2)
return self.result
else: # 如果第三局和第二局同一队伍获胜,返回结果
self.result = (1, 2) if game1 == self.team1 else (2, 1)
return self.result


# 定义一个羽毛球联赛类,包含参赛队伍和排名属性
class BadmintonLeague:
def __init__(self, teams):
self.teams = teams # 参赛队伍,是一个列表
self.ranking = None # 排名,是一个字典,键为队伍,值为胜场数

# 定义一个模拟循环赛的方法,返回排名
def simulate_round_robin(self):
self.ranking = {} # 初始化排名
for team in self.teams:
self.ranking[team] = 0 # 初始化每个队伍的胜场数为0
# 对每一对队伍进行比赛
import itertools
for team1, team2 in itertools.combinations(self.teams, 2):
match = BadmintonMatch(team1, team2) # 创建比赛对象
match.simulate_best_of_three() # 模拟比赛
if match.result[0] > match.result[1]: # 如果team1获胜,更新排名
self.ranking[team1] += 1
else: # 如果team2获胜,更新排名
self.ranking[team2] += 1
# 根据胜场数降序排序排名
self.ranking = dict(sorted(self.ranking.items(), key=lambda x: x[1], reverse=True))
return self.ranking


# 输入四个队伍的队名和能力值,创建队伍对象
teamA = BadmintonTeam("A队", 0.8)
teamB = BadmintonTeam("B队", 0.7)
teamC = BadmintonTeam("C队", 0.6)
teamD = BadmintonTeam("D队", 0.5)

# 创建联赛对象,传入队伍列表
league = BadmintonLeague([teamA, teamB, teamC, teamD])

# 模拟联赛,输出排名
league.simulate_round_robin()
print("羽毛球联赛的排名如下:")
for i, (team, wins) in enumerate(league.ranking.items()):
print(f"{i + 1}. {team.name},胜场数:{wins}")

 

标签:羽毛球,比赛,python,wins,self,team1,team2,else,score
From: https://www.cnblogs.com/hexindui/p/17932915.html

相关文章

  • python 将文件移入回收站
     python如果要删除一个文件,通常使用os.remove(filename)但是这样就直接从磁盘删除了。有些文件需要删除到回收站,以便误删后还能找回文件fromwin32com.shellimportshell,shellcondebug=Falsedefdeltorecyclebin(filename):print('deltorecyclebin',filename)......
  • python 数据存储,写入
    '''以下是同一个功能的代码段落,但是所耗时间却是天差地别'''st=time.time()#字典格式共耗时40sdsd={}#forkey,valueinfile_h.items():#ifvalueinhash_values:#dsd[value]=dsd.get(value,[])+[key]#......
  • python 文件读写权限 PermissionError: [Errno 13] Permission denied
    概述os.chmod()方法用于更改文件或目录的权限。语法chmod()方法语法格式如下:os.chmod(path,mode)参数path --文件名路径或目录路径。flags --可用以下选项按位或操作生成,目录的读权限表示可以获取目录里文件名列表,,执行权限表示可以把工作目录切换到此目录,删......
  • python word预设样式
    通过预设样式,来控制段落文本样式,来达到批量调节段落的格式样式。   大纲级别中:1级-9级代表的是标题级别。在word自动生成目录时才能正确生成。请正确设置docx:doc=Document()doc.styles["Normal"]  "Normal"表示正文的样式,[“Heading2”]表示2级标题的样式,当然......
  • 如何在 Python 程序中读取和写入文件
     在Python编程中,文件读写是一项常见的操作。通过文件读写,我们可以从文件中读取数据,或将数据写入到文件中。本文将介绍在Python程序中进行文件读写的基本操作。 读取文件 要读取文件,我们可以使用Python内置的`open()`函数。`open()`函数接受文件路径和打开模式作为参数,并返回一......
  • Python编程该怎么实现socket文件传输
    在网络编程中,Socket是一种常用的通信协议,它可以在计算机之间进行数据传输。在Python中,我们可以使用内置的socket模块来实现Socket文件传输。本文将介绍如何使用Python编程实现Socket文件传输的步骤和示例代码。步骤一:创建服务器端首先,我们需要创建一个服务器端来接收文件。以下是创......
  • python是否存在LTS这个概念
    LTS(Long-TermSupport,长期支持)是一个常见的概念,通常用于描述软件的发布策略。然而,与其他一些编程语言和软件不同,Python并没有官方的LTS版本。在本文中,我们将探讨Python的版本发布和支持策略,以及如何选择适合自己需求的Python版本。Python版本发布策略Python的版本发布策略是基于PEP......
  • Python 库和模块的概念有何不同
    在Python编程中,库(Library)和模块(Module)是两个常见的概念。虽然它们有一些相似之处,但在功能和使用方法上有一些区别。本文将介绍Python库和模块的概念,并解释它们之间的区别。模块的概念模块是Python中的一个基本概念,它是一个包含了变量、函数和类等定义的文件。一个模块可以包含多个......
  • 【 python 】《 Anaconda安装与操作 》
    安装包下载1)官网下载地址:https://www.anaconda.com/download2)其他版本下载地址:repo.anaconda.com/archive/详细安装步骤1、双击运行安装程序,点击Next2、点击IAgree3、点击Next4、选择安装路径,确保空间足够即可,然后点击Next5、勾选两个框,设置环境变量以及设为默认......
  • 排球比赛模拟
    importrandomdefprint_intro():print("排球比赛模拟程序")print("-------------------------------")definput_teams():team1_ability=float(input("请输入第一个球队的能力值:"))team2_ability=float(input("请输入第二个球队的能力值:"))returnteam1......