Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Tapping the Tableau5:10 with Ben Deitch
In this video we'll write the logic to handle a user interacting with the Tableau!
Just one user action remaining and don't worry, we saved the best for last. 0:01 When we tap on a tab row pile just like with the foundation pile, 0:06 we need to be able to tell which pile it is. 0:09 But with the tableau pile, we also need to know which card was tapped in the pile. 0:11 So we'll need to include both of those as its parameters. 0:17 Right above our play card function, let's create our tableau tab function. 0:20 Fun onTableauTap. 0:26 And for the first parameter, let's pass an tableauIndex, which is an Int. 0:29 And for the second parameter, let's pass in cardIndex, which is also an Int. 0:38 Next, we need to figure out which tableauPile we're talking about. 0:45 So it's type val tableauPile and 0:49 set it equal to tableauPiles at index tableauIndex. 0:54 And now that we've got our tableauPile Let's make sure it's not empty. 1:01 If tableauPile.cards.size is greater than 0 and 1:05 if our pile isn't empty, we need to get the card or 1:11 cards that the player is trying to move. 1:17 So let's create a new variable named cards and 1:22 set it equal to tableauPile.cards.subList, 1:27 and then let's use F1 on Mac or 1:32 Ctrl+Q on Windows to check out the quick documentation. 1:35 So the sublist function returns a mutable list of the portion of this 1:41 list between the specified fromIndex, inclusive and toIndex, exclusive. 1:47 So let's pass an card index cardindex for 1:54 the fromIndex and for the toIndex, 1:58 let's pass in tableauPile.cards.lastIndex + 1. 2:02 Since the toIndex is exclusive, meaning, it doesn't include this index and our 2:09 list, we need to add one more to make sure that our sublist includes the last card. 2:14 And by the way, lastIndex here is what's known as an extension property. 2:22 It's just like an extension function, except instead of a function, 2:28 it's a property. 2:32 And if we use Cmd or Ctrl+B to jump to the declaration, 2:33 we can see that it's an extension property on the list object and 2:37 it's just equal to the size of our list- 1. 2:42 How convenient. 2:45 Back in our tableauPile. 2:48 Now that we've got the cards were interested in just like 2:50 with the other functions, we need to try and play them. 2:53 And if we can play them, then we need to go ahead and 2:55 remove them from this tableauPile. 2:58 So let's type if playCards pass in our 3:00 cards list and then add brackets. 3:05 And if we played the cards successfully, let's remove them from our tableauPile. 3:10 TableauPile.removeCards and pass in the cardIndex. 3:16 Next, let's use Alt+Enter to create the play cards function. 3:22 And inside this function, 3:30 let's start by handling the case where there's only one card in the list. 3:31 When there's only one card in the list, 3:36 we can just pass that card along to the play card function and call it a day. 3:39 So, let's start with if cards.size is equal to 1. 3:43 And if it is, 3:52 let's return the result of passing that card into our playCard function. 3:53 Awesome, that takes care of that case. 3:58 All that's left is the case where we're trying to move multiple cards from one 4:00 tableauPile to another. 4:05 To handle this, let's start with an L statement. 4:07 And then just like we did with our playCard function, 4:11 let's try adding those cards to each of our tableauPiles to try and find a match. 4:14 In fact, let's just copy this tableauPiles loop from our playCard function and 4:19 paste it up here into our else block. 4:25 Then instead of passing an mutableListof(card), 4:27 let's just pass in the cards parameter. 4:31 Finally, if we make it to the end of the function without finding a match, 4:38 then we need to return false. 4:42 We did it. 4:48 Not only does our model contain all the objects we need, 4:49 but now it also contains all the logic we need to tie those objects together. 4:53 At this point, we're pretty much done with our game. 4:57 Remember, we're saving the UI for another course. 5:00 So at this point, all we've got left is the presenter and the view and 5:03 we'll finish those up in the next video. 5:07
You need to sign up for Treehouse in order to download course files.Sign up