Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

"""

Cricket Score Card Displayer

"""

class Player:
def __init__(self, runs, balls, sixes, fours, player_id, position):
self.runs = runs
self.balls = balls
self.sixes = sixes
self.fours = fours
self.player_id = player_id
self.position = position

def set_runs(self, runs):


self.runs += runs
if runs == 6:
self.sixes += 1
if runs == 4:
self.fours += 1

def dotBalls(self):
self.balls += 1

class Team:
def __init__(self, overs, players: Player):
self.overs = overs
self.players = players

def print_scorecard(self, players: Player, score, over, balls, b1, b2,


wickets):
print(f"Player Name Score 4s 6s Balls")

for player in players:


print(player.player_id, end='')
if (player.position == b1):
print("*", end="")
elif player.position == b2:
print("#", end="")
else:
print("", end="")

print(f" {player.runs} {player.fours} {player.sixes}


{player.balls}")

print(f"Total: {score} / {wickets}")

if balls == 0:
print(f"Overs : {over}")
else:
print(f"Overs : {over}.{balls}")

def match_innings(self, team_number, other_team_score):


print(f"{other_team_score}: other_team_score")
players = []
score = 0
scorecard = []
play_sequence = []
print(f"\nBatting Order for team : {team_number}")
for player in range(self.players):
play_sequence.append(input())

for i, lineup_val in enumerate(play_sequence):


players.append(Player(0, 0, 0, 0, lineup_val, i))
scorecard.append(Player(0, 0, 0, 0, lineup_val, i))

p1 = players.pop(0)
p2 = players.pop(0)

strike = p1
wickets = 0

for over in range(1, self.overs + 1):


balls = 6

print(f"\nOver: {over}")

while (balls > 0):


ball = input()

if ball in "0123456":
strike.set_runs(int(ball))
strike.dotBalls()
score += int(ball)
if id(strike) == id(p1):
p1 = strike
if int(ball) % 2:
strike = p2

else:
p2 = strike
if int(ball) % 2:
strike = p1

balls -= 1
elif ball == "W":
wickets += 1
strike.dotBalls()
scorecard[strike.position] = strike

if id(strike) == id(p1):
p1 = strike

if players:
p1 = players.pop()
strike = p1
else:
scorecard[p2.position] = p2
self.print_scorecard(scorecard, score, over, (6 -
balls) + 1, p2.position, -1, wickets)
return score, wickets

else:
if players:
p2 = players.pop()
strike = p2
else:
scorecard[p1.position] = p1
self.print_scorecard(scorecard, score, over, (6 -
balls) + 1, p1.position, -1, wickets)
return score, wickets

balls -= 1

elif ball == "Wd":


score += 1

if other_team_score:
# print(f"other team score: {other_team_score}")
# # print(f"score: {score}")
if score > other_team_score:
self.print_scorecard(scorecard, score, over, 0,
p1.position, p2.position, wickets)
return score, wickets

if id(strike) == id(p1):
p1 = strike
strike = p2
else:
p2 = strike
strike = p1

scorecard[p1.position] = p1
scorecard[p2.position] = p2

self.print_scorecard(scorecard, score, over, 0, p1.position,


p2.position, wickets)

return score, wickets

# scorecard = [p1 obj, p2 obj,....]

def start_match():
players = int(input("Players: "))
overs = int(input("Overs: "))
t = Team(overs=overs, players=players)
team1 = t.match_innings(team_number=1, other_team_score=None)
team2 = t.match_innings(team_number=2, other_team_score=team1[0])
if team1[0] > team2[0]:
print(f"\nTeam 1 won the match by {team1[0] - team2[0]} runs!")
elif team1[0] < team2[0]:
print(f"\nTeam 2 won the match by {team2[0] - team1[0]} runs!")
else:
print(f"Match is Draw with runs: {team1[0]}")

if __name__ == "__main__":
start_match()

You might also like