-
Notifications
You must be signed in to change notification settings - Fork 1
Matching Game
In this lesson we will learn how to make a matching game in Python using arrays of numbers and through the parsing of user input. For our game, we will create an array of 10 numbers, which are made up of 5 pairs of identical numbers (for example: 0,0,1,1,2,2,3,3,4,4). We will randomly put the numbers into the array and then have the user guess which indices of the array have the same value (for example, if the user enters 3 and 7, then we check to see if the numbers stored at the 3-index and 7-index of the array are of the same value). We will display the values at the indices guessed but will reset the values to be hidden if the numbers/indices entered are not of the same value.
First, we need to create a main method. This method will be the controller for the game and will be called when the program is started. For our game implementation, we will use 2 different arrays: 1 array will hold the values of the pairs of the numbers, while the other will initially hold 'x' as a placeholder for each value (this 2nd array will be the one displayed to the user). We want there to be 10 total "cards" to match (5 pairs of matching cards) so our arrays will be of size 10. We first create a boolean variable for checking if the game has been won (we will use this later to determine if the user has matched all of the cards). We will call this boolean variable "won" and set it initially to False. Now we create our "hidden array" that only holds 'x' values. To do this we use hiddenarray = ['x'] * 10
To be able to create our other array (the one which holds the pairs of values) efficiently and to help organize the code, we will create a separate method and call it "initarray." This method should fill an array of size 10 with 5 different pairs of matching values, so that the values are randomly distributed throughout the array. The method then returns this array to the caller. In this initarray method, we will create an array that contains 5 pairs of matching values and call it list: list = [0, 0, 1, 1, 2, 2, 3, 3, 4, 4]
. We will also create another array that is the one that gets filled with the values and is eventually returned; this array will be called "arr" : arr = [0] * 10
Now what we want to do is randomly select a value from the "list" array and put it into the "arr" array, and then delete the value from the "list" array, and then repeat the process until the "list" array is empty and the "arr" array is full. First we will need a variable to hold the index of the "arr" array (i = 0
). We also need to use a function to generate random integers. We can use the randint function that the random class provides. To use this function, at the beginning of our program we must import it: from random import randint
To implement this task of filling the "arr" array with values from the "list" array, we will use a while loop that checks to see if the "list" array is empty (size of 0) or not. If the "list" array is not empty, the loop will continue. Within the loop, we will set a variable "num" to be a random number between 0 and the length of the "list" array minus 1 (watch out for array index out of bounds issues; an array of size 7 has indices 0-6). We will then set the value at the index of "i" in the "arr" array to be the value of the index of "num" in the "list" array. In other words: arr[i] = list[num]
. Then list[num] is deleted, and i is incremented by 1 (so that the whole "arr" array is being filled). This loop will continue until "list" is empty. Remember that we can use the len() function to find the size of an array. Finally, we return the "arr" array.
Now that we have a method initarray that creates an array for us and returns it, we can create an array full of 10 values with 5 matching pairs called "array" by simply calling the initarray method: array = initarray()
. To be able to print out the arrays so that the user can actually play the game, we will create another method called "printarray." This method will take an array "array" as a parameter and will print out the values of "array" separated by 1 space and with numeric indices above those values. We will only be passing in the "hidden array" (the one initially filled with 'x') to this method because we don't want the user to see all the values (that would defeat the purpose of the game). In this "printarray" method we will start by adding a line of space for better readability of our program. Then we will create a line that reads "1 2 3 4 5 6 7 8 9 10" under which the values of the array will go. Notice that 1-10 translates to indices 0-9 of the array, so array[0] should be displayed under 1, array[1] should be displayed under 2, etc. The last line that we create in the printarray method should print out the values of the array that is passed into it as a parameter. to print out "1 2 3 4 5 6 7 8 9 10" we use print(" ".join(str(x) for x in range(1, len(array) + 1)))
. In this case, len(array) = 10 but the range function is exclusive of the ending number so we add 1. Similarly, print(" ".join(str(x) for x in array))
allows us to print out each value in array separated by a space.
We can now start working on the main part of the program: handling the output of the game and input of the user. This will all be handled within a while loop inside of the main function. We can set the loop to continue while the boolean variable "won" is False, and then within the loop we can set the "won" variable to be True once the user has matched all of the cards (and therefore won the game). Each time that we want to display the current status of the game, we use printarray(hiddenarray)
. We first want to call printarray when the functionality of the game starts, which is in the while loop. Then we will allow the user to put in the number of their first guess and we will store that number in a variable called "first": first = int(input("Enter number of 1st card: ")) - 1
. Notice that the input from the user is a String, so we must explicitly cast it to an integer. Also notice that the number is subtracted by 1, to adjust for the array indices. We now set the hiddenarray's value at the "first" index to be equal to the non-hiddenarray's value at the "first" index: hiddenarray[first] = array[first]
. This is because we want to display to the user the value at the index (card number) that they entered. Now we call printarray so that the user can decide what their 2nd card will be based on the result of their first guess.
We now want to repeat the process of getting the input from the user, casting it to an integer, and subtracting it by 1. However, this time we will store the value in a variable called "second." We also want to display the result of the 2nd guess, which should show the values of the cards for both guesses. At this point, we have to program the functionality of what to do if the cards do/don't match (if the values from the 2 guesses are equal or not) Don't get the indices and values mixed up. The variables "first" and "second" represent the indices, while hiddenarray[first] and hiddenarray[second] represent the values of the cards We can check if the values are equal by using an if statement, and if they are not equal, we will reset the values to be 'x' (as if to flip the cards back over and make them hidden again).
The last thing that we need to do in the while loop is check to see if the user has matched all of the cards. If the user has matched all of the cards, then we should set the "won" variable to be True, so that the loop doesn't continue infinitely. To check if the user has matched all of the cards, we will create a new method called checkarray() with 1 parameter, "array" . This parameter will represent the hiddenarray which we will pass in for checking. What this method will do is search the array for any instances of 'x' and will return False if it finds any. If there are no instances of 'x' in the array, then that means that all of the cards have been matched (they are all flipped over). This method purposely returns a True or False value, so that we can set "won" to equal the value returned. To accomplish this, we will create a boolean variable "good" in the method and set it equal to True. Then we have a for loop that checks the value at each index of the array to see if it equals 'x' . If it equals 'x' then we set "good" to be False. Finally, we return the value of "good." Don't forget to set "won" equal to the result of the checkarray function call inside of the while loop of the main method.
Once outside of the while loop, the user should be greeted with a victory message. Lastly, we need to call the main() method at the end of our code so that the functionality is started when the program is run.