Loading...
Hello, my name's Mrs. Jones and I'm really pleased that you are here to learn with me today.
Today's lesson is called 2D Lists Challenge, and we are going to look at using 2D lists to create a game.
So let's get started.
Welcome to today's lesson: 2D Lists Challenge from the unit Programming: Strings and Lists.
By the end of this lesson, you will be able to create a game using two-dimensional lists.
There are two keywords to today's lesson: two-dimensional.
Two-dimensional is a list or an array that has both columns and rows of values.
And subroutine.
subroutine is a sequence of instructions with an identifiable name that performs a specific task.
There are two sections to today's lesson.
The first is Create a game board, and the second is Implement a game using 2D lists.
So let's start with Create a game board.
The aim of this lesson is to make a game that uses a two-dimensional list to act as a game board.
Many real life games use boards that can be conceptualised as 2D lists, for example, noughts and crosses, battleships, chess, and many more.
The game you will be making is the Matching Pairs game.
Let's have a quick check.
Which of the following games would not be suitable to represent using a 2D list? Is it A: noughts and crosses, B: chess, or C: a pair matching card game? Pause the video to consider your answer and then we'll check it.
Let's check your answer.
The answer was C: a pair matching card game.
Well done if you got that correct.
The Matching Pairs game is a popular memory game.
A 4 X 4 grid is displayed and within it are hidden eight pairs of matching images.
The user has to choose two locations which reveals the hidden images.
If the images match, then they are left revealed.
If they don't match, the images are hidden again.
The aim of the game is to reveal all the images in the least amount of time.
This is an example of the game in Python.
Once finished, this game should be adapted to include your favourite emojis or a bigger board.
You can see here the revealed symbol is a cat.
Correct.
You found a match because they were both found on the board.
The data structure that will be used to hold the game board will be a 4x4 2D list.
In fact, you'll need two of these lists.
One: One 2D list to hold the hidden locations of all the symbols, and two: the one 2D list that is shown to the user and starts blank, then slowly gets populated with symbols as the user correctly guesses their positions.
And here's an example.
The first is a board of hidden letters.
There are eight pairs they have to find.
And the second is a blank board that gets shown to the user.
To start with, the game will just have hidden letters and these can then be replaced with emojis later.
Unfortunately using a print statement with a 2D list results in unhelpful formatting in the text output, because remember, we get those square brackets output as well.
To fix this, you will create a subroutine called print_board that will print this 2D list in the grid format needed for the game.
And here we've got the example code.
We have the subroutine created and inside the subroutine it's gonna loop through each row, initialise a blank line, loop through each column append a bar and the column value to the line and print the line.
And we get the output in a little bit nicer format that we can understand.
Let's have a quick check.
In this print_board subroutine, which line appends the list items to a string that will be displayed on screen? Is it A: line 3, B: line 5, or C: line 6? Pause the video to consider your answer and then we'll check it.
Let's check your answer.
The answer was B: line 5.
Well done if you've got that correct.
Let's start the activity, and you're going to start by opening the starter programme and then initialise two 2D lists that have four rows and four columns.
One is going to be named hidden_board that contains eight pairs of randomly placed letters, and the other 2D list is named user_board, that contains 16 blank spaces.
Remember the blank space is just a space within two speech marks.
Use the sub that has already been created for you called print_board to display both hidden_board and user_board.
Pause the video, have a go at that.
Remember to open the starter programme first and follow those instructions and then we'll go through the solution.
Let's have a look at a solution.
So we have hidden_board, now we've got that set up at the beginning and user_board with the empty spaces that are displayed.
And then following on from that, we have the subroutine, print_board, and we set that up so that it displays it with the lines in a format that makes it easier for us to see.
Well done if you've got that correct.
Now I want you to go and modify the print_board subroutine so that it prints out the indexes of the rows and columns to help the user to identify the X and the Y coordinates to enter for their guess.
Print a new row at the top that contains 0, 1, 2, 3.
Create a new variable board_row and initialise it to 0.
Change the instruction that creates the line variable so that instead of a blank string, it is the string version of board_row.
Increment board_row by 1 at the end of each loop.
Pause the video and have a go at modifying your programme and then we'll go through the solution.
Let's have a look at the solution.
And here you can see that additional code inside the subroutine.
On line 14, we have print and we have the 0, 1, 2, 3.
We have the board_row initialised as 0.
Then in the line 21, you can see that increment as well.
Well done if you've got that correct.
Let's move on to the second part of today's lesson: Implement again using 2D lists.
For the Matching Pairs game, we need to be able to access an item in a 2D array.
When you have the index number or numbers of the item you are interested in, you can print out the item from that list.
See here we have print(hidden_board).
Inside that square brackets we have 3 and then 2.
And then you can see that that will output C.
You need to ask the user to enter an X, which is the column and Y, which is the row coordinate and print out the item that is held in that position.
We have print("Enter X1:") x1 = int(input()) print("Enter Y1:") Y1 = int(input()) symbol1 = hidden_board[Y1][x1] and then print in the (f"Revealed symbol is: {symbol1}") Notice how the Y coordinate is listed first as the row number appears first in Python.
Let's have a quick check.
What is the purpose of the int function on line 2 and line 3? Is it A: to convert the user's input into an integer, B: to convert the user's input into a string, or C to remove any spaces in the user's input? Pause the video to consider your answer and then we'll check it.
Let's check your answer.
The answer was A: to convert the user's input into an integer.
Well done if you got that correct.
Next, we need to be able to update an item in the 2D array.
When you have the index number or numbers of the item you are interested in, you can override that position in the list with a new value.
Here we've got position 3, 2 and we want to overwrite it with C.
You can see what the text output will be.
It puts that in and is displayed as a text output.
Let's have a quick check.
Which statement will update the user_board with an X in the marked position? Is it A: user_board[1][3]="X" B: user_board[3][1]="X" or C: user_board[2][4]="X"? Pause the video, consider your answer and then we'll check it.
Let's check your answer.
The answer was A: user_board[1][3]="X" Well done if you got that correct.
This is a plan for the completed game in structured English.
One: Print the blank board, user_board.
Two: while not all eight pairs have been matched, ask the user to enter the first pair of coordinates.
Print out the symbol at that position.
Ask the user to enter the second pair of coordinates.
Print out the symbol at that position.
If these symbols match, add one to the correct guess count.
Print, "Congratulations.
You have a matched symbol." Update the user_board to show the position of the two matched symbols on the user_board.
Else show "No match." Once all eight pairs have been matched, print out "Congratulations you have matched all the symbols." Let's start the activity.
We're going to open the starter programme which has been given to you, and you're going to use the structured English plan and the previous Python code snippets to build the Matching Pairs game.
Pause the video, have a go at creating that programme.
Go back through the slides and look at those Python code snippets and use that starter programme to create your Matching Pairs game, then we'll go through a solution.
Let's have a look at a solution.
And this programme has been shared with you so that you can have a look and compare this with your matching one.
Well done if you've got yours working and well done if it's working correctly.
In summary, you have created a fun game using 2D lists.
The board was modelled using 2D lists and you used list techniques to access and update items within the lists.
This game can be extended in many ways: you could count the total number of guesses, you could create a larger board, you could validate the user input so they can only input valid coordinates, you could use emojis instead of letters, you could prevent the user from entering the same guessed coordinate twice.
Well done for completing this lesson, 2D Lists Challenge.