Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Basic Object-Oriented Python!
      
    
You have completed Basic Object-Oriented Python!
Preview
    
      
  Start writing the logic for the game class for our memory game.
Accomplished in this Video
- Created a Game class
 - created instances attributes
- size
 - card_options = words
 - column names
 - list to hold our cards
 - all grid locations
 
 - Created our cards
- Used 
randomand sets to get random locations for each card - created the Card instance and saved it to our card list
 
 - Used 
 
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
                      Time to tackle the game class.
                      0:00
                    
                    
                      Let's do this.
                      0:02
                    
                    
                      As always, we'll start with a class and
let's call it Game.
                      0:04
                    
                    
                      Then add an init method.
                      0:14
                    
                    
                      And inside, set the size attribute
that holds the size of our grid to 4.
                      0:20
                    
                    
                      Then let's create our card_options
with a list of words.
                      0:35
                    
                    
                      A bit of math here.
                      0:46
                    
                    
                      If our grid size is 4,
that means we're creating a 4 by 4 grid,
                      0:47
                    
                    
                      which means there will
be 16 cards in our grid.
                      0:53
                    
                    
                      And since we're matching cards,
                      0:58
                    
                    
                      we only need eight options
because 8 times 2 is 16.
                      1:01
                    
                    
                      It's also best to make sure
each word is the same length so
                      1:06
                    
                    
                      your grid is nicely laid
out in the console.
                      1:11
                    
                    
                      I stuck with three letter words, but
                      1:14
                    
                    
                      feel free to add whatever words you want
as long as they're all the same length.
                      1:16
                    
                    
                      And since this is getting a bit long,
I'm going to split it onto two lines.
                      1:47
                    
                    
                      Next, let's create a list
of the column names.
                      2:01
                    
                    
                      Since our size is 4,
we need four column names.
                      2:04
                    
                    
                      A, B, C, and D will work just fine.
                      2:09
                    
                    
                      Then we'll need to create an empty list
to eventually hold our card instances for
                      2:28
                    
                    
                      the game when we create them.
                      2:33
                    
                    
                      We'll create each card and then append
it to this list for use in our game.
                      2:38
                    
                    
                      Lastly, we'll need to get a list
of locations in our grid.
                      2:44
                    
                    
                      Let's create an empty list first.
                      2:48
                    
                    
                      Now we're going to loop
through our columns and
                      2:54
                    
                    
                      rows to create all the possible locations
in our grid to add to our list.
                      2:57
                    
                    
                      The locations will be A1, B1, C1,
                      3:03
                    
                    
                      D1, A2, B2, and so on.
                      3:08
                    
                    
                      First, create a for
loop to loop through each column.
                      3:12
                    
                    
                      Then we'll need to loop
through a range from 1 to 4.
                      3:22
                    
                    
                      Remember, range can take a start,
stop, and step parameters.
                      3:28
                    
                    
                      The only one that is required is the stop.
                      3:39
                    
                    
                      Python will automatically start at zero
and step by one if they are not stated.
                      3:43
                    
                    
                      The stop number is also not
included in the given range.
                      3:49
                    
                    
                      For example,
                      3:56
                    
                    
                      If we print out each number in a range
of 3, we get the numbers 0,1, and 2.
                      4:09
                    
                    
                      If we need our range to create
                      4:16
                    
                    
                      the numbers 1, 2, 3, and 4,
what should the range look like?
                      4:18
                    
                    
                      Take a quick minute to figure that out.
                      4:24
                    
                    
                      For num in starting at 1 and
                      4:29
                    
                    
                      ending at 5, which is our
                      4:35
                    
                    
                      self.size of our grid plus 1.
                      4:41
                    
                    
                      Let's print out what we've got so far to
the console so we can see what's going on.
                      4:48
                    
                    
                      Don't forget to create an instance.
                      5:22
                    
                    
                      Run the file.
                      5:43
                    
                    
                      Cool, we're getting B1, 2,
3, 4, C1, 2, and so on.
                      5:48
                    
                    
                      We're getting our 16 locations.
                      5:54
                    
                    
                      Now we need to append them to our list.
                      5:56
                    
                    
                      Nice, our init is all set.
                      6:15
                    
                    
                      Now let's tackle creating the cards.
                      6:18
                    
                    
                      We have our words, and
we now have the locations.
                      6:21
                    
                    
                      So let's put them together
using our card class.
                      6:25
                    
                    
                      First, we need to import our
                      6:28
                    
                    
                      card class so we can use it.
                      6:30
                    
                    
                      From cards import Card.
                      6:40
                    
                    
                      We're also going to need random so
we can get random locations for each card.
                      6:46
                    
                    
                      Otherwise, they would all be laid out
in the same pattern every time and
                      6:55
                    
                    
                      the game wouldn't be that much fun.
                      7:00
                    
                    
                      Now let's add a method called set_cards.
                      7:03
                    
                    
                      It won't take any arguments so
we only need self here.
                      7:25
                    
                    
                      Now when we give a card a random location,
we can't use that location again.
                      7:36
                    
                    
                      There's a few ways you can do this, but
                      7:43
                    
                    
                      I went with creating an empty
list called used_locations.
                      7:46
                    
                    
                      This will hold the locations that
have been used for reference.
                      7:52
                    
                    
                      Now to create the cards, we need to
loop through our card_options so
                      7:57
                    
                    
                      we create cards for each word.
                      8:02
                    
                    
                      Then we'll also need to loop again,
so that we create
                      8:11
                    
                    
                      two cards for every word so
we get our full 16 cards.
                      8:16
                    
                    
                      To do this, we can use range(2) and
the variable i.
                      8:21
                    
                    
                      Since it's just a placeholder,
we don't actually need it.
                      8:26
                    
                    
                      Cool, now that we're set up to grab
a card and then do something twice,
                      8:35
                    
                    
                      let's add the logic to grab a location and
create and append our card.
                      8:40
                    
                    
                      First, let's grab a random location.
                      8:46
                    
                    
                      Now in Python, we have a structure
that allows us to remove duplicates.
                      8:49
                    
                    
                      They're called sets.
                      8:54
                    
                    
                      Sets look kind of like a list, but
they use curly brackets instead.
                      9:00
                    
                    
                      If I subtract one set from another,
                      9:13
                    
                    
                      I am left with only the values
that are not duplicates.
                      9:16
                    
                    
                      a is in both sets, so
when I subtract them,
                      9:22
                    
                    
                      the duplicate is removed,
leaving us only with b.
                      9:26
                    
                    
                      We can use this by converting
our two lists, locations and
                      9:36
                    
                    
                      used_locations, into sets and
subtracting them.
                      9:41
                    
                    
                      We'll then end up with only locations
that haven't been used because it
                      9:45
                    
                    
                      removes the duplicates.
                      9:50
                    
                    
                      Now I'm going to convert it back into
a list so we can use random on it.
                      10:11
                    
                    
                      This works because subtracting
the sets removes the duplicates,
                      10:31
                    
                    
                      leaving us only with
locations that are available.
                      10:36
                    
                    
                      And then we get to randomly choose one.
                      10:40
                    
                    
                      We'll need to now append
this random_location to
                      10:43
                    
                    
                      our used_locations since it's now,
well, used.
                      10:48
                    
                    
                      Now we can finally create
our card instance.
                      11:00
                    
                    
                      Pass in the word and its location, and
then append it to our games cards list.
                      11:03
                    
                    
                      Let's do some printing to test this out.
                      11:30
                    
                    
                      Now before I show you, pause me and
try testing this out on your own.
                      11:33
                    
                    
                      How can you check to make sure we have
16 cards that are eight different
                      11:39
                    
                    
                      words with 16 different locations?
                      11:44
                    
                    
                      Here's one way to test it out.
                      11:54
                    
                    
                      Make this a variable, call our set_cards
                      11:58
                    
                    
                      method so the cards get created.
                      12:03
                    
                    
                      And then do a for loop so
that you can see each of your cards.
                      12:07
                    
                    
                      Now let's run the file.
                      12:17
                    
                    
                      And, oops,
looks like I forgot a self, line 19
                      12:21
                    
                    
                      Can't forget that self.
                      12:35
                    
                    
                      Okay, now let's give it a try.
                      12:40
                    
                    
                      And great,
we have all of our words in pairs.
                      12:48
                    
                    
                      We've done a lot so far.
                      12:56
                    
                    
                      Take a minute and reread the code,
make sure you know what is happening.
                      12:59
                    
                    
                      And if you don't, that's okay.
                      13:03
                    
                    
                      Re-watch the video or play around
with the code by testing it out.
                      13:06
                    
                    
                      Sometimes it helps to talk
through the code out loud.
                      13:11
                    
                    
                      It's called the rubber ducky method.
                      13:15
                    
                    
                      There's also a great
community here ready to help.
                      13:18
                    
              
        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