1 00:00:00,380 --> 00:00:03,860 We've just dealt with what happens when a user taps the deck, but 2 00:00:03,860 --> 00:00:06,310 it's time to move on to the waste pile. 3 00:00:06,310 --> 00:00:10,760 When a user taps the waste pile, there's really only two things that can happen. 4 00:00:10,760 --> 00:00:14,560 Either the card is playable, and we play the card or it's not playable and 5 00:00:14,560 --> 00:00:15,495 we just leave it. 6 00:00:15,495 --> 00:00:18,479 Let's start by creating our onWasteTap function. 7 00:00:18,479 --> 00:00:24,159 [SOUND]. 8 00:00:24,159 --> 00:00:25,345 And then add our brackets. 9 00:00:25,345 --> 00:00:28,415 Then we need to grab the top card from our waste pile and 10 00:00:28,415 --> 00:00:32,725 see if we can't play it on to any of the foundation or tableau piles. 11 00:00:32,725 --> 00:00:37,740 But before we can do that we need to make sure that our waste pile isn't empty. 12 00:00:37,740 --> 00:00:42,467 Let's type if wastePile.size, 13 00:00:42,467 --> 00:00:46,219 > 0, and then brackets. 14 00:00:46,219 --> 00:00:49,011 And I'm going to add some room at the bottom so 15 00:00:49,011 --> 00:00:51,079 I can put this more in the center. 16 00:00:51,079 --> 00:00:54,415 [SOUND]. 17 00:00:56,579 --> 00:00:59,760 Okay. Now inside let's make a new variable for 18 00:00:59,760 --> 00:01:04,470 the top card in our waste pile, val card and 19 00:01:04,470 --> 00:01:09,810 let's set it equal to wastePile.last. 20 00:01:09,810 --> 00:01:14,460 Then we're going to need a function to help us see if we can play that card and 21 00:01:14,460 --> 00:01:17,910 if we can play the card then we need to remove it from our waste pile. 22 00:01:18,980 --> 00:01:22,200 So let's type if playCard 23 00:01:24,280 --> 00:01:28,810 pass in our card and then add the brackets and 24 00:01:28,810 --> 00:01:32,150 if we were able to play it let's remove it from our waste pile. 25 00:01:32,150 --> 00:01:36,760 WastePile.remove and will pass in the card then let's use 26 00:01:36,760 --> 00:01:40,182 alt enter to create our play card function. 27 00:01:47,425 --> 00:01:50,850 Now we just need to try and play our card. 28 00:01:50,850 --> 00:01:54,125 Let's start by trying to play on one of the foundation piles. 29 00:01:54,125 --> 00:02:02,190 Let's type foundationPiles.forEach to start looping through our piles. 30 00:02:02,190 --> 00:02:07,400 And then inside the loop, let's type if it which 31 00:02:07,400 --> 00:02:12,370 represents a foundation pile .addCard. 32 00:02:12,370 --> 00:02:15,214 Pass in our card and then if that's true, 33 00:02:15,214 --> 00:02:18,317 let's return true because we found a match. 34 00:02:22,048 --> 00:02:24,108 Then let's do the same thing for our tableau piles. 35 00:02:24,108 --> 00:02:26,642 [SOUND]. 36 00:02:26,642 --> 00:02:32,712 TableauPiles.forEach and inside if it.addCards and 37 00:02:32,712 --> 00:02:38,610 remember this function requires a MutableList. 38 00:02:38,610 --> 00:02:43,220 Let's pass an a mutableListOf our card and 39 00:02:43,220 --> 00:02:46,860 then if this is true we want to return true as well. 40 00:02:48,380 --> 00:02:50,640 Finally, at the bottom of the function, 41 00:02:50,640 --> 00:02:54,480 let's return false because if we made it this far there is no match. 42 00:02:58,080 --> 00:02:59,050 Awesome. 43 00:02:59,050 --> 00:03:03,670 Now whenever we tap on the waste pile, if that card is playable we'll play it. 44 00:03:03,670 --> 00:03:06,350 Moving right along, let's see if we can't handle what 45 00:03:06,350 --> 00:03:10,070 happens when we tap on a foundation pile, by the end of this video. 46 00:03:10,070 --> 00:03:13,302 Let's start right below our onWasteTap function, 47 00:03:13,302 --> 00:03:16,538 by creating a new function named onFoundationTap. 48 00:03:20,623 --> 00:03:24,775 And different from on waste tab, when we're talking about foundation taps it's 49 00:03:24,775 --> 00:03:28,520 important that we first specify which foundation we're talking about. 50 00:03:29,590 --> 00:03:37,000 Let's add foundation index as a parameter, And make it an Int. 51 00:03:37,000 --> 00:03:41,453 Then inside the function, let's figure out which foundation pile was tapped. 52 00:03:41,453 --> 00:03:48,604 Val foundationPile = foundationPiles 53 00:03:48,604 --> 00:03:52,610 at foundationIndex. 54 00:03:52,610 --> 00:03:56,680 And actually at this point it's pretty much the same as we did on onWasteTap. 55 00:03:57,770 --> 00:04:03,051 Let's copy what's inside our onWasteTap function and 56 00:04:03,051 --> 00:04:06,205 paste it into onFoundationTap. 57 00:04:06,205 --> 00:04:11,164 Then let's replace wastePile, with foundationPile.cards. 58 00:04:17,515 --> 00:04:22,977 And if we play the cards successfully, 59 00:04:22,977 --> 00:04:31,926 let's call foundationPile.removeCard and pass in our card. 60 00:04:31,926 --> 00:04:32,650 Nice. 61 00:04:32,650 --> 00:04:35,200 Now there's just one user action remaining, and 62 00:04:35,200 --> 00:04:37,320 that's tapping on a tableau pile. 63 00:04:37,320 --> 00:04:39,400 We'll see how to handle that in the next video.