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

Python Cricket Game Simulation

The document outlines a Python program for a simple cricket game where a user can play against a computer. It includes features like a toss to decide who bats or bowls first, scoring runs, and tracking wickets. The game concludes with a final scoreboard displaying the scores of both the user and the computer, along with the match result.
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)
47 views2 pages

Python Cricket Game Simulation

The document outlines a Python program for a simple cricket game where a user can play against a computer. It includes features like a toss to decide who bats or bowls first, scoring runs, and tracking wickets. The game concludes with a final scoreboard displaying the scores of both the user and the computer, along with the match result.
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

import random

import time

class CricketGame:
def __init__(self):
[Link] = 2
self.balls_per_over = 6
self.user_score = 0
self.cpu_score = 0
self.user_wickets = 0
self.cpu_wickets = 0
self.max_wickets = 2

def toss(self):
print("\n🪙 Toss Time!")
choice = input("Choose heads or tails: ").lower()
toss_result = [Link](["heads", "tails"])
print(f"Toss result: {toss_result.upper()}")

if choice == toss_result:
print("🎉 You won the toss!")
decision = input("Do you want to bat or bowl first? (bat/bowl):
").lower()
return decision
else:
print("🤖 Computer won the toss!")
decision = [Link](["bat", "bowl"])
print(f"Computer chooses to {decision} first.")
return "bowl" if decision == "bat" else "bat"

def batting(self, batting_player):


runs = 0
wickets = 0
print(f"\n🏏 {batting_player.upper()} is batting now!")
for over in range(1, [Link] + 1):
print(f"\nOver {over}")
for ball in range(1, self.balls_per_over + 1):
if wickets == self.max_wickets:
print("All wickets down!")
return runs

if batting_player == "user":
user_run = int(input("Enter your run (1-6): "))
cpu_ball = [Link](1, 6)
print(f"Computer bowled: {cpu_ball}")
else:
cpu_run = [Link](1, 6)
user_ball = int(input("Enter your bowl (1-6): "))
print(f"Computer played: {cpu_run}")

# Check wicket
if (batting_player == "user" and user_run == cpu_ball) or (
batting_player == "cpu" and cpu_run == user_ball
):
wickets += 1
print("💥 WICKET!")
else:
run_scored = user_run if batting_player == "user" else cpu_run
runs += run_scored
print(f"Runs this ball: {run_scored} | Total:
{runs}/{wickets}")

[Link](0.6)
return runs

def play(self):
print("🏏 Welcome to the Advanced Python Cricket Game 🏏")
user_choice = [Link]()

if user_choice == "bat":
self.user_score = [Link]("user")
print(f"\nYour innings ends at {self.user_score} runs.")
print("\nComputer needs", self.user_score + 1, "runs to win.")
self.cpu_score = [Link]("cpu")
else:
self.cpu_score = [Link]("cpu")
print(f"\nComputer innings ends at {self.cpu_score} runs.")
print("\nYou need", self.cpu_score + 1, "runs to win.")
self.user_score = [Link]("user")

print("\n🏁 Final Scoreboard 🏁")


print(f"You: {self.user_score}")
print(f"Computer: {self.cpu_score}")

if self.user_score > self.cpu_score:


print("🎉 You Win!")
elif self.user_score < self.cpu_score:
print("🤖 Computer Wins!")
else:
print("🤝 Match Tied!")

if __name__ == "__main__":
game = CricketGame()
[Link]()

You might also like