You've already started watching Tapping the Tableau
In this video we'll write the logic to handle a user interacting with the Tableau!

0:01
Just one user action remaining and don't worry, we saved the best for last.

0:06
When we tap on a tab row pile just like with the foundation pile,

0:09
we need to be able to tell which pile it is.

0:11
But with the tableau pile, we also need to know which card was tapped in the pile.

0:17
So we'll need to include both of those as its parameters.

0:20
Right above our play card function, let's create our tableau tab function.

0:26
Fun onTableauTap.

0:29
And for the first parameter, let's pass an tableauIndex, which is an Int.

0:38
And for the second parameter, let's pass in cardIndex, which is also an Int.

0:45
Next, we need to figure out which tableauPile we're talking about.

0:49
So it's type val tableauPile and

0:54
set it equal to tableauPiles at index tableauIndex.

1:01
And now that we've got our tableauPile Let's make sure it's not empty.

1:05
If tableauPile.cards.size is greater than 0 and

1:11
if our pile isn't empty, we need to get the card or

1:17
cards that the player is trying to move.

1:22
So let's create a new variable named cards and

1:27
set it equal to tableauPile.cards.subList,

1:32
and then let's use F1 on Mac or

1:35
Ctrl+Q on Windows to check out the quick documentation.

1:41
So the sublist function returns a mutable list of the portion of this

1:47
list between the specified fromIndex, inclusive and toIndex, exclusive.

1:54
So let's pass an card index cardindex for

1:58
the fromIndex and for the toIndex,

2:02
let's pass in tableauPile.cards.lastIndex + 1.

2:09
Since the toIndex is exclusive, meaning, it doesn't include this index and our

2:14
list, we need to add one more to make sure that our sublist includes the last card.

2:22
And by the way, lastIndex here is what's known as an extension property.

2:28
It's just like an extension function, except instead of a function,

2:32
it's a property.

2:33
And if we use Cmd or Ctrl+B to jump to the declaration,

2:37
we can see that it's an extension property on the list object and

2:42
it's just equal to the size of our list 1.

2:45
How convenient.

2:48
Back in our tableauPile.

2:50
Now that we've got the cards were interested in just like

2:53
with the other functions, we need to try and play them.

2:55
And if we can play them, then we need to go ahead and

2:58
remove them from this tableauPile.

3:00
So let's type if playCards pass in our

3:05
cards list and then add brackets.

3:10
And if we played the cards successfully, let's remove them from our tableauPile.

3:16
TableauPile.removeCards and pass in the cardIndex.

3:22
Next, let's use Alt+Enter to create the play cards function.

3:30
And inside this function,

3:31
let's start by handling the case where there's only one card in the list.

3:36
When there's only one card in the list,

3:39
we can just pass that card along to the play card function and call it a day.

3:43
So, let's start with if cards.size is equal to 1.

3:52
And if it is,

3:53
let's return the result of passing that card into our playCard function.

3:58
Awesome, that takes care of that case.

4:00
All that's left is the case where we're trying to move multiple cards from one

4:05
tableauPile to another.

4:07
To handle this, let's start with an L statement.

4:11
And then just like we did with our playCard function,

4:14
let's try adding those cards to each of our tableauPiles to try and find a match.

4:19
In fact, let's just copy this tableauPiles loop from our playCard function and

4:25
paste it up here into our else block.

4:27
Then instead of passing an mutableListof(card),

4:31
let's just pass in the cards parameter.

4:38
Finally, if we make it to the end of the function without finding a match,

4:42
then we need to return false.

4:48
We did it.

4:49
Not only does our model contain all the objects we need,

4:53
but now it also contains all the logic we need to tie those objects together.

4:57
At this point, we're pretty much done with our game.

5:00
Remember, we're saving the UI for another course.

5:03
So at this point, all we've got left is the presenter and the view and

5:07
we'll finish those up in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up