Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video we'll add the ability to remove cards to our tableau piles, and we'll add our tableau piles to our model!
-
0:00
We've only got one function standing between us and a finished Tableau class
-
0:04
and that's the remove cards function, but it won't be left standing for long.
-
0:08
Let's start by declaring our remove cards function right after our add cards
-
0:13
function.
-
0:13
Fun, remove cards, then for the parameter let's add an integer
-
0:19
named tapped index to represent which card was tapped.
-
0:29
And then add our brackets.
-
0:30
Now, we just need to remove the cards from our cards list from
-
0:34
the tappedIndex to the end.
-
0:36
To do that, we're going to use a for loop.
-
0:39
And Kotlin for loops are a little bit different than in Java.
-
0:43
For starters, in Kotlin all for loops take the form of some variable
-
0:48
followed by the in keyword and then the thing you'd like to loop over.
-
0:52
So if we wanted to loop over all the cards in our cards list, we would just type for
-
0:58
card in cards and then add our brackets.
-
1:03
Notice that I didn't use a var or vow to declare the card variable and
-
1:08
a for loop it's always going to be immutable.
-
1:11
So we can leave that part off.
-
1:13
So that's nice but we don't really want to loop through our entire card's list.
-
1:17
We just want to loop from the tappedIndex to the end.
-
1:20
So instead of looping with a card let's loop with an index like we're used to.
-
1:25
And let's use i and instead of having the cards list as the thing we'd like to loop
-
1:30
over, let's just use a range from the tappedIndex to the end.
-
1:35
A range is just a set of numbers like the numbers between one and seven.
-
1:41
If we wanted to print the numbers one through seven,
-
1:43
we could use a range like this one.
-
1:46
The two dots represent that this range
-
1:48
will include all the integers between one and seven.
-
1:52
But these dots only work if the range is increasing.
-
1:55
If we want to go from seven down to one we would need to write 7 downTo 1.
-
2:02
Now that we've got that covered,
-
2:04
let's loop over a range from the tappedIndex to the lastIndex.
-
2:09
Let's delete 7 downTo 1 and instead write tappedIndex to
-
2:14
cards.lastIndex.
-
2:19
And now we're successfully looping from the tappedIndex to the lastIndex.
-
2:24
So to remove those cards let's jump and set our four loop and
-
2:28
remove the card index i.
-
2:29
cards.removeAt and pass in i.
-
2:36
Finally, now that we've handled removing the cards,
-
2:39
we just need to make sure that the last card in our pile is face up,
-
2:43
provided that there is a last card in our pile.
-
2:46
Below our for
-
2:47
loop let's first make sure there's at least one card in our cards list.
-
2:51
If cards.size is greater than
-
2:56
zero and if there are let's set the last card to face up,
-
3:01
cards.last.faceUp equals true.
-
3:09
All right, we're all finished with our tableau class.
-
3:13
All that's left is to jump over to our game model class and
-
3:17
create a property for our tableau piles.
-
3:20
Let's add a line below our foundation piles and then type val tableau piles
-
3:27
and let's set it equal to a new array of size seven.
-
3:34
And then let's populate our array with seven instances of an empty tableau pile.
-
3:39
Remember, we'll be creating the tableau piles at the start of every game.
-
3:43
So for now we're just populating them with something so we don't get an error.
-
3:47
To populate our seven tableau piles,
-
3:49
let's use a lambda expression like we did in our debt class.
-
3:53
Let's start with the brackets, and then inside let's create a new tableau pile.
-
4:01
And since each tableau pile needs to be created with a list of cards,
-
4:05
let's add mutable list of as our parameter to initialize it with an empty list.
-
4:11
Next let's flip back to our Tableau pile class and inside the init
-
4:16
block let's copy in this if card.size greater than zero section.
-
4:27
To make sure we don't try to access a last card that doesn't exist.
-
4:32
After all, we did just call the constructor with an empty list.
-
4:36
So at this point, it's guaranteed to happen.
-
4:39
For one final step, I think it looks a little bit cleaner
-
4:42
if we just have an empty list be the default value for our cards list.
-
4:46
So inside our constructor let's add equals mutable list of and
-
4:52
now that we've got a default value.
-
4:54
Let's jump back to our game model and
-
4:57
now we can get rid of this unnecessary parameter.
-
5:02
We've come so far since the beginning of this course.
-
5:05
We've learned tons about Kotlin and we just finished up our Tableau object
-
5:09
which was the last object we needed to start modeling our game of solitaire.
-
5:14
But before we can move on, I feel like we've been doing a lot of coding and
-
5:18
we haven't been able to check if any of that works.
-
5:21
So let's take a minute and write some tests for a tableau pile.
You need to sign up for Treehouse in order to download course files.
Sign up