Skip to content

Matching Game

David Silvan edited this page Feb 22, 2017 · 9 revisions

Overview

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.

Let's Get Started:

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

(IMAGE1)

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

(IMAGE2)

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

(IMAGE3)

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.

(IMAGE4)

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.

(IMAGE5)

Clone this wiki locally