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