import random
import os
# 介绍比赛以及程序
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: 49")
'''
羽毛球比赛规则
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()
标签:羽毛球,比赛,wins,score,pow,print,return,def From: https://www.cnblogs.com/ylnx/p/17934458.html