1 00:00:00,280 --> 00:00:03,930 We're almost done with all the objects required to model our game. 2 00:00:03,930 --> 00:00:08,100 Now we just need to create one more class to represent a TableauPile. 3 00:00:08,100 --> 00:00:12,690 Let's start by declaring a new class named TableauPile. 4 00:00:15,619 --> 00:00:21,114 Then lets add a constructor and inside the constructor, 5 00:00:21,114 --> 00:00:27,080 let's add var_cards and make it a MutableList of type Card. 6 00:00:28,500 --> 00:00:33,810 Unlike a foundation, a TableauPile contains cards right from the get go. 7 00:00:33,810 --> 00:00:36,010 So instead of declaring our cards property and 8 00:00:36,010 --> 00:00:40,170 the class body, we'll declare it in the constructor. 9 00:00:40,170 --> 00:00:45,460 This way our cards property will have all the right cards right from the start. 10 00:00:45,460 --> 00:00:48,430 But we're not done with the constructor just yet. 11 00:00:48,430 --> 00:00:52,880 When we created each of our card objects, we left out the argument for 12 00:00:52,880 --> 00:00:57,130 the face up parameter, which means each card is currently face down. 13 00:00:57,130 --> 00:01:01,630 But to properly initialize a TableauPile, the last card needs to be face up. 14 00:01:01,630 --> 00:01:06,005 To do this, let's add an init block and inside, 15 00:01:06,005 --> 00:01:10,808 let's get the last card on our list, cards.last and 16 00:01:10,808 --> 00:01:14,720 set its faceUp property to true. 17 00:01:14,720 --> 00:01:17,617 .FaceUp = true. 18 00:01:17,617 --> 00:01:21,160 Nice, now that we've handled initializing our TableauPile, 19 00:01:21,160 --> 00:01:24,100 let's take a look at the actions involving the Tableau. 20 00:01:24,100 --> 00:01:28,910 First, we can move one card from the waste pile to a TableauPile. 21 00:01:28,910 --> 00:01:33,890 And we can also move one card from foundation down to a Tableau pile or 22 00:01:33,890 --> 00:01:36,230 even just between two tableau piles. 23 00:01:36,230 --> 00:01:40,080 In addition to adding and removing one card, we can also add and 24 00:01:40,080 --> 00:01:43,270 remove multiple cards between tableau piles. 25 00:01:43,270 --> 00:01:46,160 When we want to reset the game, rather than try and 26 00:01:46,160 --> 00:01:50,190 reuse our tableau piles, we'll just create new ones. 27 00:01:50,190 --> 00:01:53,314 So unlike our waste file and our foundation files, 28 00:01:53,314 --> 00:01:55,501 we won't have a reset method here. 29 00:01:55,501 --> 00:01:57,808 All right, to handle each of these actions, 30 00:01:57,808 --> 00:02:00,140 we'll need to create two functions. 31 00:02:00,140 --> 00:02:03,390 One function for adding cards and one for removing. 32 00:02:03,390 --> 00:02:08,387 Let's start with adding by creating a function named addCards. 33 00:02:09,510 --> 00:02:12,810 It will be just like our foundation's addCard function 34 00:02:12,810 --> 00:02:16,980 except this time we'll be able to add multiple cards at once. 35 00:02:16,980 --> 00:02:24,340 So let's add newCards as our parameter and make it a MutableList of type card. 36 00:02:24,340 --> 00:02:26,430 And then let's return a boolean for whether or 37 00:02:26,430 --> 00:02:28,450 not we're able to add the cards. 38 00:02:29,880 --> 00:02:33,930 Inside this function, we need to determine if the new cards are a match for 39 00:02:33,930 --> 00:02:35,710 this TableauPile. 40 00:02:35,710 --> 00:02:40,560 To do that, we need to make a couple of comparisons between the first new card and 41 00:02:40,560 --> 00:02:42,670 the last current card. 42 00:02:42,670 --> 00:02:46,120 Specifically, the first card in our newCards list, 43 00:02:46,120 --> 00:02:50,960 needs to have a value of one less than the last card and our cards property. 44 00:02:50,960 --> 00:02:57,500 Also, the first new card and the last current card need to be different colors. 45 00:02:57,500 --> 00:03:02,143 So inside this function, let's start with an if statement and 46 00:03:02,143 --> 00:03:06,180 then let's check that the value of the first new card. 47 00:03:06,180 --> 00:03:11,125 NewCards.first().value is equal to 48 00:03:11,125 --> 00:03:16,775 the value of the last current card minusone1, 49 00:03:16,775 --> 00:03:21,589 equals cards.last().value- 1. 50 00:03:21,589 --> 00:03:27,790 Then let's add an and and make sure the suits are different colors as well. 51 00:03:27,790 --> 00:03:31,680 And since checking the colors is a little more work than we should be doing here, 52 00:03:31,680 --> 00:03:34,570 let's create a function to do that work for us. 53 00:03:34,570 --> 00:03:39,890 Let's type suitCheck and pass in the two cards we'd like to check. 54 00:03:41,710 --> 00:03:45,858 New,Cards.first and cards.last, 55 00:03:45,858 --> 00:03:51,778 then let's finish off our if statement with brackets. 56 00:03:51,778 --> 00:03:54,525 And use Alt + Enter to create the suitCheck function. 57 00:03:59,001 --> 00:04:02,630 And since this function is just going to check if the two cards 58 00:04:02,630 --> 00:04:07,040 are different colors, first and last aren't good variable names. 59 00:04:07,040 --> 00:04:10,598 Instead let's just use c1 and c2. 60 00:04:10,598 --> 00:04:15,530 And I'm gonna put this if statement on two lines 61 00:04:15,530 --> 00:04:19,483 just to make it a little easier to see. 62 00:04:19,483 --> 00:04:21,429 Inside our suitCheck function, 63 00:04:21,429 --> 00:04:25,883 we need to make sure that one of these cards is red and the other card is black. 64 00:04:25,883 --> 00:04:30,205 But before we dive in, wouldn't it be nice to have a property telling us which suits 65 00:04:30,205 --> 00:04:32,730 are red and which suits are black? 66 00:04:32,730 --> 00:04:34,880 Let's head over to our Card class and make it so. 67 00:04:36,740 --> 00:04:42,135 Let's create a new package level property below our suit properties named redSuits. 68 00:04:44,200 --> 00:04:50,079 And let's set it equal to arrayOf diamonds and hearts. 69 00:04:51,510 --> 00:04:54,380 Then let's do the same thing for the black suits. 70 00:04:54,380 --> 00:05:02,930 Val blackSuits equals array of clubs and spades. 71 00:05:02,930 --> 00:05:07,280 And now it's incredibly easy for us to check if a card is red or black. 72 00:05:07,280 --> 00:05:09,380 If we want to check if a card is red, 73 00:05:09,380 --> 00:05:13,600 we can just check that the card's suit is contained in the redSuits property. 74 00:05:14,660 --> 00:05:18,100 In the next video, we'll check back in with our suitCheck function and 75 00:05:18,100 --> 00:05:19,710 then finish up our tableau pile.