outputCode (1)
outputCode (1)
class Player:
def __init__(self, name, role):
self.name = name
self.role = role
self.runs = 0
self.balls_faced = 0
self.wickets = 0
self.catches = 0
self.by_runs = 0
def strike_rate(self):
return (self.runs / self.balls_faced) * 100 if self.balls_faced > 0 else 0
class Team:
def __init__(self, name):
self.name = name
self.players = []
self.total_runs = 0
self.total_wickets = 0
self.overs = 0
def calculate_run_rate(self):
return self.total_runs / self.overs if self.overs > 0 else 0
class Match:
def __init__(self, team1, team2):
self.team1 = team1
self.team2 = team2
self.current_batsman = None
self.bowler = None
self.total_overs = 0
self.current_over = 0
self.balls_in_over = 0
self.winner = None
def bowl(self):
outcome = random.choice(['run', 'wicket', 'bye', 'leg_bye', 'wide',
'dead_ball'])
if outcome == 'run':
runs = random.choice([0, 1, 2, 3, 4])
self.current_batsman.runs += runs
self.current_batsman.balls_faced += 1
self.team1.total_runs += runs
self.balls_in_over += 1
if runs == 4:
print(f"{self.current_batsman.name} hit a boundary!")
elif outcome == 'wicket':
self.current_batsman.wickets += 1
self.team1.total_wickets += 1
print(f"{self.bowler.name} took a wicket!")
self.current_batsman = None # Batsman is out
elif outcome in ['bye', 'leg_bye']:
self.team1.total_runs += 1
elif outcome == 'wide':
self.team1.total_runs += 1
print("Wide ball!")
elif outcome == 'dead_ball':
print("Dead ball!")
if self.balls_in_over == 6:
self.current_over += 1
self.balls_in_over = 0
self.team1.overs += 1
self.bowler = random.choice(self.team2.players)
def end_innings(self):
if self.team1.total_runs > self.team2.total_runs:
self.winner = self.team1.name
else:
self.winner = self.team2.name
def man_of_the_match(self):
best_batsman = max(self.team1.players, key=lambda p: p.runs)
best_bowler = max(self.team2.players, key=lambda p: p.wickets)
return best_batsman, best_bowler
def main():
team1 = Team("Team A")
team2 = Team("Team B")
match.end_innings()
print(f"Winner: {match.winner}")
best_batsman, best_bowler = match.man_of_the_match()
print(f"Man of the Match: {best_batsman.name} (Runs: {best_batsman.runs})")
print(f"Best Bowler: {best_bowler.name} (Wickets: {best_bowler.wickets})")
if __name__ == "__main__":
main()