Guessing game.

Let’s create a guessing game with a list of five objects containing three ‘O’ and two ‘X’ which would be shuffled randomly. The player would then be asked to choose a position (Left to right – numbered 1 to 5) that do NOT contain an ‘X’. This is equivalent to a probability of 0.6 of getting a correct answer. A correct answer earns the player 1 point and the player gets to go again, while an incorrect answer will earn no point and the game ends.
A typical game would be something as follows:

The instruction of the game is first shown followed by the default positions of ‘O’ and ‘X’. The sequence of ‘O’ and ‘X’ will be shuffled and hidden, and the player is asked to choose a position from 1 to 5. Afterwhich the shuffled positions is shown and the points awarded accordingly. The game ends if the player chose incorrectly.

The code for this programme is as follows:

Line 1 imports the random module from Python, which contains the shuffle function that will be used in this programme.
Line 2 imports the time module to introduce a pause between lines of the code being executed. This is not essential, but just serves to introduce a 2 second pause (Line 9) after the game instructions (Line 4 to 8) are being shown.
Line 11 sets the initial score variable to 0.

As we wanted the game to repeat the process of shuffling the five positions, asking the player to choose a position, followed by awarding a point or ending the game, the code for running the game is placed within a while loop from Line 13 to 54.
Line 14 and 15 first creates the list of the five objects and prints it out, while Line 16 to 19 just shows text to inform the player of the shuffling process. Line 20 utilises the shuffle function within the random module to execute the actual shuffling process. The list from Line 14 is now overwritten with the shuffled list from Line 20.

Line 22 creates a new empty pos_X list.
Line 23 to 26 uses a for loop to go through the shuffled list, and append the index (i.e. the position) of ‘X’ in this shuffled list into the pos_X list. Line 25 adds 1 to the index as the Python index (i.e. Python’s way of counting) begins with 0 and not 1.

Line 28 to 35 ask for the player to input the guess, with a try-except block being used to catch and handle exceptions to the input. An input in the form of integers 1 to 5 is considered to be a valid response, and the programme would throw an error and crash the programme if any other input is entered. With this try-except block, nestled in a while loop, the input would be requested until a valid response is entered as shown below. The integer ‘8’ or the character ‘q’ are not valid response, hence the player will be prompted for an input again.

Line 38 will show the shuffled list after the player has entered a valid response.
Line 40 then counts the number of occurrence of the player’s response as compared to the pos_X list containing the list of positions of ‘X’s, storing it as a variable called result. This comparison would either give an answer of 0, meaning the player chose a position that is not ‘X’, or 1, meaning the player chose a position that is ‘X’. If the answer is 0 (Line 42), the player is awarded a point (Line 43), and the code repeats itself from Line 14. If the answer is 1 (Line 50), then no points are awarded, and the game ends as the while loop is broken in Line 54.

Is there any way to make the above codes more concise and elegant? Feel free to comment.

One thought on “Guessing game.

Leave a comment