Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Tackle creating the grid.
Accomplished in this Video
- Created the grid using 2 methods
-
create_row
- found all of the cards in the given row
- added either spaces or the word to the row to show which ones have been matched
-
create_grid
- created the header
- used the row method to create all 4 rows and print them out to the console
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've created our instance attributes and
our set cards method.
0:01
Next, let's tackle creating the grid.
0:06
In order to create our grid,
I think creating the rows in their own
0:10
separate method will help keep
things a bit cleaner and clear.
0:15
Create a method called create_row.
0:20
We'll pass in the row number so
we know which row to create.
0:37
For example, if one is passed in,
0:44
then we will need to find all
the cards with one in their location.
0:47
We'll need an empty list
where we can append
0:55
either cards if they have been guessed or
empty spaces.
0:57
Let's loop through the columns so we can
find the matching locations for this row.
1:03
Now we'll need to loop through our cards
1:15
to find the ones that
are located in this row.
1:23
If card.location equals,
1:27
The current column and
the row number, if they are,
1:35
we need to check to see
if they've been matched.
1:42
If card.matched, if so,
1:46
then we need to append
the word to our row.
1:52
row.append, string of the card,
just to make sure the word gets appended.
1:57
If not, then we need to append empty
spaces that are the same size as our word.
2:07
So three spaces.
2:13
One, two, three.
2:20
This helps keep our grid nice and neat.
2:24
Finally, we'll need to return the row.
2:28
Let's test this out.
2:36
You can see I've already called
the create_row method for
2:43
each row that we have in our grid.
2:48
And I also grabbed the first
few cards in our cards list and
2:51
switch their matched attribute to true.
2:55
This way we can make sure we're also going
to see the words as they're matched.
2:59
Great, it's looking like a grid already.
3:12
Okay, so now let's jump into
our create grid method that
3:20
we'll use our create row to
create the grid in the console.
3:25
We can call it create_grid.
3:38
Now, the first thing that needs
to get printed out is our header.
3:46
Let's create a template using comments
to show what this will look like.
3:50
Okay, so we can use join to print
out our header with spaces and
4:19
pipes in between each one.
4:25
Now that we know what it should look like,
4:29
we're going to use join
to put it all together.
4:31
Going to start with the beginning.
4:40
And we should have a space for
the row number.
4:46
Our pipe and then two spaces.
4:51
This is because our column names
are a single letter while our words
4:55
are three letters. Then we're
5:00
going to add all of our
columns using join, so
5:04
we're gonna join with a double space,
space space, pipe, space space.
5:09
Again, this is because our
columns are single letter
5:16
while the words are going
to be three letters.
5:20
This will help keep it nice and
neat, .join(self.columns).
5:23
And then, we need our ending pipe,
5:32
plus space, space, pipe.
5:37
Now since it's our header, and
5:41
it's the beginning of our game grid,
we need to print it out.
5:43
Let's scroll down to the bottom and
call the function.
5:49
Great, that looks nice.
5:59
Now it's time to create the rows,
we'll need to loop through a range,
6:09
that will start at one and end at four.
6:15
We did this before,
up in the _init_ method.
6:18
Now we can start our print row variable.
6:29
This will be the row that will
be printed to our console.
6:35
The row should start with our row number,
6:39
A pipe, and then one space.
6:46
Then we'll need to call our row method and
pass in the row we're currently on.
6:50
Let's save that to
a variable called get_row.
6:55
Now we can add to our print row by
joining all of our words or spaces,
7:05
just like we did in the header above.
7:10
Print_row +=.
7:14
Then we'll need a space, pipe,
7:19
space, .join, our get_row.
7:25
And then finish it off with a space, pipe.
7:33
Finally print out the row.
7:40
Now we can run the file and see what
our grid looks like in the console.
7:45
Great, that looks really nice.
7:54
Test this out further by changing some
of the cards matched attribute, to true.
7:56
Game.cards, first one
8:09
.matched = True.
8:14
I'm gonna copy this, paste, paste, paste.
8:18
This will be, three, two, one, okay.
8:26
Now when we run the file again,
I should see the words inside of our grid.
8:30
Awesome.
8:40
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up