This course will be retired on July 14, 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
Well done!
You have completed Kotlin for Java Developers!
You have completed Kotlin for Java Developers!
Preview
In this video we'll handle what happens when a user taps on the waste pile. If we're lucky, we might even get to handle tapping on a foundation pile as well!
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 just dealt with what happens
when a user taps the deck, but
0:00
it's time to move on to the waste pile.
0:03
When a user taps the waste pile, there's
really only two things that can happen.
0:06
Either the card is playable, and
we play the card or it's not playable and
0:10
we just leave it.
0:14
Let's start by creating
our onWasteTap function.
0:15
[SOUND].
0:18
And then add our brackets.
0:24
Then we need to grab the top
card from our waste pile and
0:25
see if we can't play it on to any
of the foundation or tableau piles.
0:28
But before we can do that we need to make
sure that our waste pile isn't empty.
0:32
Let's type if wastePile.size,
0:37
> 0, and then brackets.
0:42
And I'm going to add some
room at the bottom so
0:46
I can put this more in the center.
0:49
[SOUND].
0:51
Okay.
Now inside let's make a new variable for
0:56
the top card in our waste pile,
val card and
0:59
let's set it equal to wastePile.last.
1:04
Then we're going to need a function to
help us see if we can play that card and
1:09
if we can play the card then we need
to remove it from our waste pile.
1:14
So let's type if playCard
1:18
pass in our card and
then add the brackets and
1:24
if we were able to play it let's
remove it from our waste pile.
1:28
WastePile.remove and
will pass in the card then let's use
1:32
alt enter to create our
play card function.
1:36
Now we just need to try and play our card.
1:47
Let's start by trying to play
on one of the foundation piles.
1:50
Let's type foundationPiles.forEach
to start looping through our piles.
1:54
And then inside the loop,
let's type if it which
2:02
represents a foundation pile .addCard.
2:07
Pass in our card and then if that's true,
2:12
let's return true because
we found a match.
2:15
Then let's do the same thing for
our tableau piles.
2:22
[SOUND].
2:24
TableauPiles.forEach and
inside if it.addCards and
2:26
remember this function
requires a MutableList.
2:32
Let's pass an a mutableListOf our card and
2:38
then if this is true we want
to return true as well.
2:43
Finally, at the bottom of the function,
2:48
let's return false because if we
made it this far there is no match.
2:50
Awesome.
2:58
Now whenever we tap on the waste pile,
if that card is playable we'll play it.
2:59
Moving right along,
let's see if we can't handle what
3:03
happens when we tap on a foundation pile,
by the end of this video.
3:06
Let's start right below
our onWasteTap function,
3:10
by creating a new function
named onFoundationTap.
3:13
And different from on waste tab, when
we're talking about foundation taps it's
3:20
important that we first specify which
foundation we're talking about.
3:24
Let's add foundation index as a parameter,
And make it an Int.
3:29
Then inside the function, let's figure
out which foundation pile was tapped.
3:37
Val foundationPile = foundationPiles
3:41
at foundationIndex.
3:48
And actually at this point it's pretty
much the same as we did on onWasteTap.
3:52
Let's copy what's inside
our onWasteTap function and
3:57
paste it into onFoundationTap.
4:03
Then let's replace wastePile,
with foundationPile.cards.
4:06
And if we play the cards successfully,
4:17
let's call foundationPile.removeCard and
pass in our card.
4:22
Nice.
4:31
Now there's just one user
action remaining, and
4:32
that's tapping on a tableau pile.
4:35
We'll see how to handle
that in the next video.
4:37
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