|
| 1 | +import random |
| 2 | + |
| 3 | +def get_user_choice(): |
| 4 | + while True: |
| 5 | + try: |
| 6 | + user_choice = int(input("Choose any number from 1 to 6: ")) |
| 7 | + if 1 <= user_choice <= 6: |
| 8 | + return user_choice |
| 9 | + else: |
| 10 | + print("Please choose a number from 1 to 6.") |
| 11 | + except ValueError: |
| 12 | + print("Invalid input. Please enter a number between 1 and 6.") |
| 13 | + |
| 14 | +def play_innings(player, overs, max_wickets): |
| 15 | + runs = 0 |
| 16 | + wickets = 0 |
| 17 | + balls = 0 |
| 18 | + |
| 19 | + print(f"\n{player}'s Innings Begins") |
| 20 | + |
| 21 | + while wickets < max_wickets and balls < overs * 6: |
| 22 | + try: |
| 23 | + user_choice = get_user_choice() |
| 24 | + computer_choice = random.randint(1, 6) |
| 25 | + |
| 26 | + print(f"Your choice: {user_choice}\nComputer's choice: {computer_choice}") |
| 27 | + |
| 28 | + if user_choice == computer_choice: |
| 29 | + wickets += 1 |
| 30 | + else: |
| 31 | + runs += user_choice |
| 32 | + |
| 33 | + balls += 1 |
| 34 | + |
| 35 | + if balls % 6 == 0: |
| 36 | + over_number = balls // 6 |
| 37 | + print(f"End of Over {over_number}") |
| 38 | + print(f"Over {over_number} Summary: {player} scored {runs} runs with {wickets} wickets.") |
| 39 | + |
| 40 | + print(f"Total Score: {runs}/{wickets}") |
| 41 | + print(f"Balls remaining: {overs * 6 - balls}") |
| 42 | + |
| 43 | + except KeyboardInterrupt: |
| 44 | + print("\nGame interrupted. Exiting.") |
| 45 | + exit() |
| 46 | + except Exception as e: |
| 47 | + print(f"An error occurred: {e}") |
| 48 | + print("Please try again.") |
| 49 | + |
| 50 | + print("\nEnd of Innings") |
| 51 | + print(f"Final Score for {player}:\nRuns = {runs}\nWickets = {wickets}") |
| 52 | + |
| 53 | + return runs, wickets |
| 54 | + |
| 55 | +print("~ Welcome to the Game of Cricket ~") |
| 56 | + |
| 57 | +print("\nInstructions:") |
| 58 | +print("1. You have to select any number from 1 to 6.") |
| 59 | +print("2. The computer will also select a number.") |
| 60 | +print("3. While batting, if your number and computer's number are different, you'll add to your runs.") |
| 61 | +print(" If they are the same, you'll lose a wicket.") |
| 62 | +print("4. While bowling, if your number and computer's number are different, the computer adds to its runs.") |
| 63 | +print(" If they are the same, the computer loses a wicket.") |
| 64 | +print("5. Each player will get 2 wickets and 2 overs (12 balls) for batting and bowling.") |
| 65 | +print("6. The innings will end after either three wickets fall or the overs end.") |
| 66 | +print("7. The player with the maximum runs wins.") |
| 67 | + |
| 68 | +print("\n---------- Start Game ----------") |
| 69 | + |
| 70 | +# Toss |
| 71 | +toss_result = random.choice(["Heads", "Tails"]) |
| 72 | +user_toss_choice = input("Choose heads or tails: ").capitalize() |
| 73 | +toss_winner = "User" if user_toss_choice == toss_result else "Computer" |
| 74 | +print(f"\nToss Result: {toss_result}") |
| 75 | +print(f"{toss_winner} won the toss") |
| 76 | + |
| 77 | +if toss_winner == "User": |
| 78 | + user_choice = input("Choose to bat or bowl: ").lower() |
| 79 | + computer_choice = "bowl" if user_choice == "bat" else "bat" |
| 80 | +else: |
| 81 | + computer_choice = random.choice(["bat", "bowl"]) |
| 82 | + user_choice = "bowl" if computer_choice == "bat" else "bat" |
| 83 | + |
| 84 | +print(f"{toss_winner} chose to {user_choice} and the computer chose to {computer_choice}") |
| 85 | + |
| 86 | +user_runs, user_wickets = play_innings("You", 2, 2) |
| 87 | +computer_runs, computer_wickets = play_innings("Computer", 2, 2) |
| 88 | + |
| 89 | +print("\n~~~~~~~~~~ Result ~~~~~~~~~~") |
| 90 | +print(f"Your total runs: {user_runs}") |
| 91 | +print(f"Computer's total runs: {computer_runs}") |
| 92 | + |
| 93 | +if user_runs > computer_runs: |
| 94 | + print(f"Congratulations! You won the Match by {user_runs - computer_runs} runs.") |
| 95 | +elif user_runs < computer_runs: |
| 96 | + print(f"Better luck next time! The Computer won the Match by {computer_runs - user_runs} runs.") |
| 97 | +else: |
| 98 | + print("The Match is a Tie. No one Wins.") |
0 commit comments