0% found this document useful (0 votes)
7 views2 pages

outputCode (1)

The document contains a Python script that simulates a cricket match between two teams. It defines classes for Player, Team, and Match, allowing for player statistics tracking and match progression. The main function initializes teams, adds players, simulates bowling, and determines the match winner and player awards.

Uploaded by

fuggifuggi53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

outputCode (1)

The document contains a Python script that simulates a cricket match between two teams. It defines classes for Player, Team, and Match, allowing for player statistics tracking and match progression. The main function initializes teams, adds players, simulates bowling, and determines the match winner and player awards.

Uploaded by

fuggifuggi53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import random

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 add_player(self, player):


self.players.append(player)

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 start_innings(self, batting_team):


self.current_batsman = batting_team.players[0]
self.bowler = random.choice(self.team2.players)

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")

team1.add_player(Player("Batsman 1", "batsman"))


team1.add_player(Player("Batsman 2", "batsman"))
team2.add_player(Player("Bowler 1", "bowler"))
team2.add_player(Player("Bowler 2", "bowler"))

match = Match(team1, team2)


match.start_innings(team1)

for _ in range(6): # Simulate 6 balls


match.bowl()

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()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy