Play a Game of Hand Cricket Using Python – MUO – MakeUseOf

Step up to the crease and try your hand at this text-based cricket simulation.
With an estimated 2.5 billion fans, cricket is one of the biggest and most popular sports in the world. In India alone, cricket is worth a whopping 5.3 billion dollars. Sachin Tendulkar, Donald Bradman, Brian Lara, and MS Dhoni are just a few legends whose names live in the hearts of millions.
It comes as no surprise that cricket has found its way to virtual reality, video games, and other variations like book and hand cricket. Take this game one step further by building a hand cricket game using Python.
Hand cricket is a game in which two play compete against each other using their fingers. In each turn, both players display a number using their fingers at the same time. If the scores match, the batsman is out, otherwise they gain their number as runs. Finally, the player who scores the most wins the game.
You can build this game and play it against a computer too. Instead of holding out fingers to represent the score, you enter it into the program. Python is one of the easiest and most convenient languages to use. If you have no prior experience using Python, you can get up to speed with any of these free online Python courses for beginners.
Once you learn the fundamentals, it is a good idea to build mini-projects and games to solidify your learning. You can start by building a digital clock, rolling dice, quiz, or a word counter. You can also gain inspiration from these Python project ideas for beginners.
You can find the source code of Hand Cricket Using Python in this GitHub repository.
Import the random library, required for the initial toss and the computer's choices. Define a function named input_num that accepts two arguments, min and max respectively to validate the user's input. Using the input() function, store the number user enters into variable num, and cast it from the default String to Integer type using int().
If the number the user enters is greater than the maximum limit or less than the minimum limit, ask the user to enter a valid choice by calling the function recursively. If the number is within the range, return it.
Define a function innings that accepts the current batsman and the runs to chase as input arguments. Ask the user to enter numbers within the range of one to six. According to the rules of hand cricket, if both the user and the computer choose the same number, the current batsman is out.
Initialize a variable total to store the total runs scored by a batsman and set it to zero. Begin an infinite while loop and call the input_num() function to store and validate the user’s input. Use the random module's randint() function to choose a random number between one and six for the computer.
Display the choices of both players, then check whether they are equal. If they are, display that the batsman is out and return the total runs they scored in that innings.
Otherwise, check who is batting and add the appropriate value to the total score, either pnum or cnum. Display the current score of the batsman.
In the case of the second innings, you want to check if the batter has already beaten the score of their opponent. To do so, check if the to_chase value is not empty and if the batsman’s score is greater. If so, return their winning score.
Display the message for toss and validate the user's choice using the input_num() function. Toss the coin and store the result in the variable coin. Initialize the default value of player_bowls to false. If the coin flip matches the user's choice, the user wins the toss and chooses between batting and bowling.
Take the user's choice and check if he has entered one. If yes, evaluate the expression to true and store in the player_bowls variable. Display the user's choice. On the other hand, if the computer won the toss, execute randint() and evaluate the appropriate value to player_bowls.
Display the computer's choice and begin the innings.
If player_bowls is true, the computer bats first and vice versa. Calculate the runs scored in an innings using the function you defined earlier. The function requires the name of the current batsman and the runs to chase. As the first batsman sets the score, make it an optional parameter and pass None. For the second innings, pass the batsman's score who set the target.
If the computer scores less than the user, the user wins, and vice versa. In case the scores are the same, display a tie.
Put all the code together and get ready to play hand cricket anytime, anywhere right at your fingertips.
You can see the output of a game of hand cricket Using Python as follows:
When you execute the program, the program asks you to choose a choice for the toss and assigns the appropriate result. If you win, you get to decide if you want to bat or bowl else the computer makes a decision. Each inning continues until the numbers match and one is out. In the end, the computer compares the final score and declares the winner.
Building games is an amazing way to learn Python and is a lot of fun. You do not have to be an expert to start coding and experimenting in Python. With just the right fundamentals, you can build some amazing terminal games.
Start by building the Magic 8 Ball, Hangman, Rock Paper Scissors, Mad Libs Generator, and the Number Guessing Game. As you advance further, explore the PyGame library to build your very own graphical video games as well.
Sai Ashish is a Full Stack Developer with industry experience building websites and web applications. He loves to build innovative products and write insightful articles on programming.

source

Leave a Comment