1 00:00:00,630 --> 00:00:02,393 Time to tackle the game class. 2 00:00:02,393 --> 00:00:04,271 Let's do this. 3 00:00:04,271 --> 00:00:09,223 As always, we'll start with a class and let's call it Game. 4 00:00:14,312 --> 00:00:15,854 Then add an init method. 5 00:00:20,522 --> 00:00:28,503 And inside, set the size attribute that holds the size of our grid to 4. 6 00:00:35,346 --> 00:00:39,223 Then let's create our card_options with a list of words. 7 00:00:46,362 --> 00:00:47,993 A bit of math here. 8 00:00:47,993 --> 00:00:53,890 If our grid size is 4, that means we're creating a 4 by 4 grid, 9 00:00:53,890 --> 00:00:58,311 which means there will be 16 cards in our grid. 10 00:00:58,311 --> 00:01:01,590 And since we're matching cards, 11 00:01:01,590 --> 00:01:06,800 we only need eight options because 8 times 2 is 16. 12 00:01:06,800 --> 00:01:11,299 It's also best to make sure each word is the same length so 13 00:01:11,299 --> 00:01:14,782 your grid is nicely laid out in the console. 14 00:01:14,782 --> 00:01:16,903 I stuck with three letter words, but 15 00:01:16,903 --> 00:01:21,502 feel free to add whatever words you want as long as they're all the same length. 16 00:01:47,432 --> 00:01:55,613 And since this is getting a bit long, I'm going to split it onto two lines. 17 00:02:01,830 --> 00:02:04,892 Next, let's create a list of the column names. 18 00:02:04,892 --> 00:02:09,039 Since our size is 4, we need four column names. 19 00:02:09,039 --> 00:02:11,514 A, B, C, and D will work just fine. 20 00:02:28,961 --> 00:02:33,453 Then we'll need to create an empty list to eventually hold our card instances for 21 00:02:33,453 --> 00:02:35,116 the game when we create them. 22 00:02:38,799 --> 00:02:44,532 We'll create each card and then append it to this list for use in our game. 23 00:02:44,532 --> 00:02:48,567 Lastly, we'll need to get a list of locations in our grid. 24 00:02:48,567 --> 00:02:50,716 Let's create an empty list first. 25 00:02:54,252 --> 00:02:57,963 Now we're going to loop through our columns and 26 00:02:57,963 --> 00:03:03,540 rows to create all the possible locations in our grid to add to our list. 27 00:03:03,540 --> 00:03:08,491 The locations will be A1, B1, C1, 28 00:03:08,491 --> 00:03:12,097 D1, A2, B2, and so on. 29 00:03:12,097 --> 00:03:15,026 First, create a for loop to loop through each column. 30 00:03:22,012 --> 00:03:28,133 Then we'll need to loop through a range from 1 to 4. 31 00:03:28,133 --> 00:03:33,443 Remember, range can take a start, stop, and step parameters. 32 00:03:39,282 --> 00:03:43,423 The only one that is required is the stop. 33 00:03:43,423 --> 00:03:49,884 Python will automatically start at zero and step by one if they are not stated. 34 00:03:49,884 --> 00:03:56,802 The stop number is also not included in the given range. 35 00:03:56,802 --> 00:03:58,590 For example, 36 00:04:09,077 --> 00:04:16,250 If we print out each number in a range of 3, we get the numbers 0,1, and 2. 37 00:04:16,250 --> 00:04:18,500 If we need our range to create 38 00:04:18,500 --> 00:04:24,642 the numbers 1, 2, 3, and 4, what should the range look like? 40 00:04:24,642 --> 00:04:26,631 Take a quick minute to figure that out. 41 00:04:29,575 --> 00:04:35,520 For num in starting at 1 and 42 00:04:35,520 --> 00:04:41,207 ending at 5, which is our 43 00:04:41,207 --> 00:04:48,197 self.size of our grid plus 1. 44 00:04:48,197 --> 00:04:52,593 Let's print out what we've got so far to the console so we can see what's going on. 45 00:05:22,000 --> 00:05:24,913 Don't forget to create an instance. 47 00:05:43,190 --> 00:05:45,487 Run the file. 48 00:05:48,487 --> 00:05:54,482 Cool, we're getting B1, 2, 3, 4, C1, 2, and so on. 49 00:05:54,482 --> 00:05:56,636 We're getting our 16 locations. 50 00:05:56,636 --> 00:05:59,574 Now we need to append them to our list. 51 00:06:15,530 --> 00:06:18,733 Nice, our init is all set. 52 00:06:18,733 --> 00:06:21,610 Now let's tackle creating the cards. 53 00:06:21,610 --> 00:06:25,163 We have our words, and we now have the locations. 54 00:06:25,163 --> 00:06:28,572 So let's put them together using our card class. 55 00:06:28,572 --> 00:06:30,000 First, we need to import our 56 00:06:30,000 --> 00:06:32,179 card class so we can use it. 57 00:06:40,000 --> 00:06:46,860 From cards import Card. 58 00:06:46,860 --> 00:06:51,980 We're also going to need random so we can get random locations for each card. 59 00:06:55,503 --> 00:07:00,682 Otherwise, they would all be laid out in the same pattern every time and 60 00:07:00,682 --> 00:07:03,372 the game wouldn't be that much fun. 61 00:07:03,372 --> 00:07:06,344 Now let's add a method called set_cards. 62 00:07:25,449 --> 00:07:28,683 It won't take any arguments so we only need self here. 63 00:07:36,929 --> 00:07:43,772 Now when we give a card a random location, we can't use that location again. 64 00:07:43,772 --> 00:07:46,314 There's a few ways you can do this, but 65 00:07:46,314 --> 00:07:50,259 I went with creating an empty list called used_locations. 66 00:07:52,754 --> 00:07:57,453 This will hold the locations that have been used for reference. 67 00:07:57,453 --> 00:08:02,529 Now to create the cards, we need to loop through our card_options so 68 00:08:02,529 --> 00:08:04,814 we create cards for each word. 69 00:08:11,703 --> 00:08:16,706 Then we'll also need to loop again, so that we create 70 00:08:16,706 --> 00:08:21,716 two cards for every word so we get our full 16 cards. 71 00:08:21,716 --> 00:08:26,082 To do this, we can use range(2) and the variable i. 72 00:08:26,082 --> 00:08:29,066 Since it's just a placeholder, we don't actually need it. 73 00:08:35,440 --> 00:08:40,843 Cool, now that we're set up to grab a card and then do something twice, 74 00:08:40,843 --> 00:08:46,174 let's add the logic to grab a location and create and append our card. 75 00:08:46,174 --> 00:08:49,495 First, let's grab a random location. 76 00:08:49,495 --> 00:08:54,892 Now in Python, we have a structure that allows us to remove duplicates. 77 00:08:54,892 --> 00:08:55,900 They're called sets. 78 00:09:00,664 --> 00:09:04,672 Sets look kind of like a list, but they use curly brackets instead. 79 00:09:13,793 --> 00:09:16,935 If I subtract one set from another, 80 00:09:16,935 --> 00:09:21,770 I am left with only the values that are not duplicates. 81 00:09:22,820 --> 00:09:26,097 a is in both sets, so when I subtract them, 82 00:09:26,097 --> 00:09:30,027 the duplicate is removed, leaving us only with b. 83 00:09:36,848 --> 00:09:41,735 We can use this by converting our two lists, locations and 84 00:09:41,735 --> 00:09:45,938 used_locations, into sets and subtracting them. 85 00:09:45,938 --> 00:09:50,577 We'll then end up with only locations that haven't been used because it 86 00:09:50,577 --> 00:09:52,259 removes the duplicates. 87 00:10:11,382 --> 00:10:16,282 Now I'm going to convert it back into a list so we can use random on it. 88 00:10:31,838 --> 00:10:36,669 This works because subtracting the sets removes the duplicates, 89 00:10:36,669 --> 00:10:40,474 leaving us only with locations that are available. 90 00:10:40,474 --> 00:10:43,203 And then we get to randomly choose one. 91 00:10:43,203 --> 00:10:48,049 We'll need to now append this random_location to 92 00:10:48,049 --> 00:10:53,129 our used_locations since it's now, well, used. 93 00:11:00,484 --> 00:11:03,903 Now we can finally create our card instance. 94 00:11:03,903 --> 00:11:09,084 Pass in the word and its location, and then append it to our games cards list. 95 00:11:30,543 --> 00:11:33,483 Let's do some printing to test this out. 96 00:11:33,483 --> 00:11:39,430 Now before I show you, pause me and try testing this out on your own. 97 00:11:39,430 --> 00:11:44,230 How can you check to make sure we have 16 cards that are eight different 98 00:11:44,230 --> 00:11:46,878 words with 16 different locations? 99 00:11:54,454 --> 00:11:56,441 Here's one way to test it out. 100 00:11:58,494 --> 00:12:03,587 Make this a variable, call our set_cards 101 00:12:03,587 --> 00:12:07,552 method so the cards get created. 102 00:12:07,552 --> 00:12:11,365 And then do a for loop so that you can see each of your cards. 103 00:12:17,232 --> 00:12:18,762 Now let's run the file. 104 00:12:21,621 --> 00:12:27,446 And, oops, looks like I forgot a self, line 19 105 00:12:35,894 --> 00:12:37,416 Can't forget that self. 106 00:12:40,682 --> 00:12:42,302 Okay, now let's give it a try. 107 00:12:48,405 --> 00:12:52,782 And great, we have all of our words in pairs. 108 00:12:56,784 --> 00:12:59,154 We've done a lot so far. 109 00:12:59,154 --> 00:13:03,981 Take a minute and reread the code, make sure you know what is happening. 110 00:13:03,981 --> 00:13:06,569 And if you don't, that's okay. 111 00:13:06,569 --> 00:13:11,663 Re-watch the video or play around with the code by testing it out. 112 00:13:11,663 --> 00:13:15,332 Sometimes it helps to talk through the code out loud. 113 00:13:15,332 --> 00:13:18,243 It's called the rubber ducky method. 114 00:13:18,243 --> 00:13:21,740 There's also a great community here ready to help.