Cricket Game- Python Project for CBSE Class 12

Source Code:

# Cricket Game print(""" ~~~~~~~~~~ Game of Cricket ~~~~~~~~~~ Instructions: 1. You have to select any random number from 1 to 6. 2. The computer will also select a number. 3. While batting, if the number selected by you and computer is different, then your number will add to your runs. If the number selected by you and computer is same, then you will lose your wicket. 4. While bowling, if the number selected by you and computer is different, then the computer's number will add to its runs. If the number selected by you and computer is same, then the computer will lose its wicket. 5. Each player will get 2 wickets and 2 overs (12 balls) for batting and bowling. 6. The innings will end after either the three wickets fell or the overs end. 7. The player with maximum runs wins. """) print("\n---------- Start Game ----------") import random # Toss print("\nHere comes the Toss") toss = (input("Choose heads or tails: ")).lower() random_toss = random.randint(1,2) # In random_toss (1 = Heads) and (2 = Tails) random_opt = random.randint(1,2) # In random_opt (1 = bat) and (2 = ball) u_opt = 0 c_opt = 0 if random_toss == 1 and toss == "heads": print("\nYou won the toss") u_opt = (input("Choose bat or ball: ")).lower() elif random_toss == 2 and toss == "tails": print("\nYou won the toss") u_opt = (input("Choose bat or ball: ")).lower() else: print("\nYou lost the toss") if random_opt == 1: c_opt = "bat" print("Computer choose to",c_opt) elif random_opt == 2: c_opt = "ball" print("Computer choose to",c_opt) # First Innings print("\n---------- First Innings Begins ----------") runs_1 = 0 wickets_1 = 0 balls_1 = 0 while wickets_1 != 2 and balls_1 != 12: u_choice = int(input("\nChoose any number from 1 to 6: ")) c_choice = random.randint(1,6) if u_choice < 1 or u_choice > 6: print("\nPlease choose a value from 1 to 6.") else: print("Your choice: ",u_choice,"\nComputer's choice: ",c_choice) if u_choice == c_choice: wickets_1 += 1 else: if u_opt == "bat" or c_opt == "ball": Bat_first = "You" Ball_first = "Computer" runs_1 += u_choice elif u_opt == "ball" or c_opt == "bat": Bat_first = "Computer" Ball_first = "You" runs_1 += c_choice print("\nScore =",runs_1,"/",wickets_1) balls_1 += 1 if balls_1 == 6: print("End of Over 1") elif balls_1 == 12: print("End of Over 2") print("Balls remaining: ",12 - balls_1) print("\n---------- End of Innings ----------") print("\nFinal Score:") print("Runs =",runs_1) print("wickets =",wickets_1) print("\n",Ball_first,"needs",runs_1 + 1,"runs to win.") # Second Innings print("\n---------- Second Innings Begins ----------") runs_2 = 0 wickets_2 = 0 balls_2 = 0 while wickets_2 != 2 and balls_2 != 12 and runs_2 <= runs_1: u_choice = int(input("\nChoose any number from 1 to 6: ")) c_choice = random.randint(1,6) if u_choice < 1 or u_choice > 6: print("\nPlease choose a value from 1 to 6.") else: print("Your choice: ",u_choice,"\nComputer's choice: ",c_choice) if u_choice == c_choice: wickets_2 += 1 else: if Bat_first == "Computer": runs_2 += u_choice Bat_second = "You" elif Bat_first == "You": runs_2 += c_choice Bat_second = "Computer" print("\nScore =",runs_2,"/",wickets_2) balls_2 += 1 if balls_2 == 6: print("End of Over 1") elif balls_2 == 12: print("End of Over 2") if runs_2 <= runs_1 and balls_2 <= 11 and wickets_2 != 2: print("To win:",runs_1 - runs_2 + 1,"runs needed from",12 - balls_2,"balls.") print("\n---------- End of Innings ----------") print("\nFinal Score:") print("Runs =",runs_2) print("wickets =",wickets_2) # Result of Match print("\n~~~~~~~~~~ Result ~~~~~~~~~~") if runs_1 > runs_2: if Bat_first == "You": print("\nCongratulations! You won the Match by",runs_1 - runs_2,"runs.") else: print("\nBetter luck next time! The Computer won the Match by",runs_1 - runs_2,"runs.") elif runs_2 > runs_1: if Bat_second == "You": print("\nCongratulations! You won the Match by",2 - wickets_2,"wickets.") else: print("\nBetter luck next time! The Computer won the Match by",2 - wickets_2,"wickets.") else: print("The Match is a Tie.","\nNo one Wins.")

Output:

~~~~~~~~~~ Game of Cricket ~~~~~~~~~~ Instructions: 1. You have to select any random number from 1 to 6. 2. The computer will also select a number. 3. While batting, if the number selected by you and computer is different, then your number will add to your runs. If the number selected by you and computer is same, then you will lose your wicket. 4. While bowling, if the number selected by you and computer is different, then the computer's number will add to its runs. If the number selected by you and computer is same, then the computer will lose its wicket. 5. Each player will get 2 wickets and 2 overs (12 balls) for batting and bowling. 6. The innings will end after either the three wickets fell or the overs end. 7. The player with maximum runs wins. ---------- Start Game ---------- Here comes the Toss Choose heads or tails: "tails" You won the toss Choose bat or ball: "bat" ---------- First Innings Begins ---------- Choose any number from 1 to 6: 4 ('Your choice: ', 4, "\nComputer's choice: ", 2) ('\nScore =', 4, '/', 0) ('Balls remaining: ', 11) Choose any number from 1 to 6: 1 ('Your choice: ', 1, "\nComputer's choice: ", 2) ('\nScore =', 5, '/', 0) ('Balls remaining: ', 10) Choose any number from 1 to 6: 3 ('Your choice: ', 3, "\nComputer's choice: ", 4) ('\nScore =', 8, '/', 0) ('Balls remaining: ', 9) Choose any number from 1 to 6: 2 ('Your choice: ', 2, "\nComputer's choice: ", 2) ('\nScore =', 8, '/', 1) ('Balls remaining: ', 8) Choose any number from 1 to 6: 6 ('Your choice: ', 6, "\nComputer's choice: ", 4) ('\nScore =', 14, '/', 1) ('Balls remaining: ', 7) Choose any number from 1 to 6: 3 ('Your choice: ', 3, "\nComputer's choice: ", 6) ('\nScore =', 17, '/', 1) End of Over 1 ('Balls remaining: ', 6) Choose any number from 1 to 6: 2 ('Your choice: ', 2, "\nComputer's choice: ", 6) ('\nScore =', 19, '/', 1) ('Balls remaining: ', 5) Choose any number from 1 to 6: 5 ('Your choice: ', 5, "\nComputer's choice: ", 6) ('\nScore =', 24, '/', 1) ('Balls remaining: ', 4) Choose any number from 1 to 6: 6 ('Your choice: ', 6, "\nComputer's choice: ", 5) ('\nScore =', 30, '/', 1) ('Balls remaining: ', 3) Choose any number from 1 to 6: 3 ('Your choice: ', 3, "\nComputer's choice: ", 3) ('\nScore =', 30, '/', 2) ('Balls remaining: ', 2) ---------- End of Innings ---------- Final Score: ('Runs =', 30) ('wickets =', 2) ('\n', 'Computer', 'needs', 31, 'runs to win.') ---------- Second Innings Begins ---------- Choose any number from 1 to 6: 2 ('Your choice: ', 2, "\nComputer's choice: ", 1) ('\nScore =', 1, '/', 0) ('To win:', 30, 'runs needed from', 11, 'balls.') Choose any number from 1 to 6: 1 ('Your choice: ', 1, "\nComputer's choice: ", 2) ('\nScore =', 3, '/', 0) ('To win:', 28, 'runs needed from', 10, 'balls.') Choose any number from 1 to 6: 4 ('Your choice: ', 4, "\nComputer's choice: ", 5) ('\nScore =', 8, '/', 0) ('To win:', 23, 'runs needed from', 9, 'balls.') Choose any number from 1 to 6: 2 ('Your choice: ', 2, "\nComputer's choice: ", 2) ('\nScore =', 8, '/', 1) ('To win:', 23, 'runs needed from', 8, 'balls.') Choose any number from 1 to 6: 4 ('Your choice: ', 4, "\nComputer's choice: ", 6) ('\nScore =', 14, '/', 1) ('To win:', 17, 'runs needed from', 7, 'balls.') Choose any number from 1 to 6: 6 ('Your choice: ', 6, "\nComputer's choice: ", 5) ('\nScore =', 19, '/', 1) End of Over 1 ('To win:', 12, 'runs needed from', 6, 'balls.') Choose any number from 1 to 6: 4 ('Your choice: ', 4, "\nComputer's choice: ", 6) ('\nScore =', 25, '/', 1) ('To win:', 6, 'runs needed from', 5, 'balls.') Choose any number from 1 to 6: 3 ('Your choice: ', 3, "\nComputer's choice: ", 5) ('\nScore =', 30, '/', 1) ('To win:', 1, 'runs needed from', 4, 'balls.') Choose any number from 1 to 6: 2 ('Your choice: ', 2, "\nComputer's choice: ", 5) ('\nScore =', 35, '/', 1) ---------- End of Innings ---------- Final Score: ('Runs =', 35) ('wickets =', 1) ~~~~~~~~~~ Result ~~~~~~~~~~ ('\nBetter luck next time! The Computer won the Match by', 1, 'wickets.') >>>