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 talk about all the actions a user can take, and then we're going to handle a user tapping on the deck!
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
Okay.
0:00
The table's all set up and we can finally
start thinking about playing Solitaire.
0:01
At this point is when the user would
start interacting with the play area.
0:06
So now we need to program all of
the interactions that the user
0:09
can take with our game.
0:13
To keep things simple,
when a player taps on a card.
0:15
If that card is playable,
we'll just move it to the right spot.
0:19
This way, we won't have to deal
with any extra complications
0:22
from things like dragging and
dropping cards.
0:26
Before we get to coding,
0:29
let's take a look at what actions
a player will be able to take.
0:30
In our game, a player will be able
to do four different actions.
0:34
They can tap the deck to flip a card into
the waste pile, they can tap the waste
0:37
pile to attempt to play a card,
they can tap a foundation pile
0:42
to send a card back down to the Tablo,
or they can tap a Tablo pile to try and
0:46
send a card or cards to a foundation
pile or another Tablo pile.
0:51
All right, let's start with tapping the
deck to flip a card into the waste pile.
0:57
Let's add some space below
our reset game function and
1:02
then declare a new function
named on deck tap.
1:05
When we tap on the deck we want to draw
a card, make that card face up, and
1:09
then add it to our waste pile.
1:14
So inside this function,
let's first draw a card,
1:16
Val card equals deck.drawCard.
1:21
Then, let's make that card face up.
1:24
Card.faceUp = true.
1:27
And finally,
let's add that card to our waste pile.
1:31
WastePile.add and we'll add that card.
1:33
And now when we tap on the deck, we'll
draw a card into the waste pile, nice.
1:38
But we're not done yet, we still need to
handle the case where the deck is empty.
1:43
If we tap on an empty
deck we want to transfer
1:48
all the cards from
the waste pile to the deck.
1:51
Let's start by adding an if statement so
1:54
that we only call this code if there
is at least one card in the deck.
1:56
So if deck.cardsInDeck.size
is greater than 0,
2:00
and then let's add the other bracket
down here, and add an else statement.
2:05
Inside this else statement,
2:15
let's copy all the cards from
our waste pile into our deck.
2:17
So deck.cardsInDeck equals
2:20
wastePile.toMutableList.
2:25
Now I know wastePile is
already a immutable list but
2:31
here we are adding toMutableList
as a way to return
2:35
a copy of our wastePile instead
of the actual wastePile.
2:39
After we've copied the contents
of our wastePile into our deck,
2:44
all that's left is to empty the wastePile.
2:47
wastePile.clear.
2:50
And there we go, now we are completely
ready to handle taps on our deck.
2:53
That's one user action down and
three to go.
2:59
In the next video,
3:02
we'll see how we can deal with the user
interacting with our waste pile.
3:03
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