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 turn our deck into something useful by adding functionality to draw a card and reset the deck!
Kotlin Links
Project Files
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
A deck isn't very useful if
you can't interact with it.
0:00
Let's fix that by adding a couple
functions to help us with our game of
0:03
solitaire.
0:07
In solitaire, there's really only two
functions we require of our deck.
0:08
We need to be able to draw a card and
we need to be able to reset the deck for
0:12
a new game.
0:16
But before we get started
on those functions,
0:17
we'll need to deal with our card's array.
0:19
You see, our card's array is immutable.
0:21
And it's also an array, and just like
Java, arrays in have a fixed size.
0:24
So shuffling or drawing a card isn't going
to work with our cards array instead,
0:31
let's use a list rather than
getting rid of our cards array.
0:37
Let's use our cards array to populate our
list this way whenever there's a new game.
0:40
We don't have to worry about
where all the cards are.
0:46
We can just use our immutable cards
array to repopulate our cards list and
0:49
start the game.
0:54
Let's add a line after our cards array.
0:55
And then create our list as
a var named cardsInDeck, and
0:58
for the type let's pick MutableList and
1:05
add card inside the angle brackets
to give us a mutable list of cards.
1:10
And Cotland just like with variables
there's a distinction between mutable and
1:16
immutable collections like lists or maps.
1:21
So, if you want to be able to add or
1:24
remove an item from a collection just make
sure you're using the mutable version.
1:25
Next, let's set our list
equal to cards.toMutableList.
1:31
Which as the name suggests,
returns our cards array as a mutable list.
1:39
And now that we've got our list,
let's get started on our two functions.
1:44
First up, the draw a card function.
1:48
Let's type fun, draw card,
and make it return a card.
1:50
Then let set it =
cardsInDeck.remove at and
1:56
pass in zero for the index.
2:04
Then lets use control Q On Windows or
2:07
F one on Mac And we can see the quick
documentation for Remove at.
2:09
RemoveAt removes the element
at the specified index and
2:15
then returns that element.
2:19
So, each time we call our draw
a card function, we're going to
2:21
take the card from index zero and
our cards and deck list and return it.
2:24
Perfect.
2:29
Moving on to the reset function.
2:30
Before we start a game, our deck
needs to have all fifty two cards and
2:32
it also needs to be a shuffled.
2:36
Let's start by declaring our function.
2:38
Fun, reset, and then brackets.
2:42
Then we need to be sure that our cards and
deck array has all the right cards.
2:46
After all at the end of a game,
2:51
we'll have lost most of our deck
thanks to the draw card function.
2:53
So to make sure our list
has all the right cards,
2:57
let's just repopulate it using the cards
array just like when we initialized it.
3:00
cardsInDeck = cards.toMutableList.
3:06
Then to shuffle the deck, we can make use
of a super helpful function from the Java
3:13
collections class named shuffle.
3:18
So let's add Collections.Shuffle
3:21
pass on our list and call it a day.
3:27
Now, whenever we're starting a new game,
we can just call reset on our deck and
3:32
have a full shuffled deck ready for
dealing.
3:38
All right, we've got our deck and
we're ready to play solitaire.
3:41
In the next stage, we'll see how we can
model an entire game of solitaire and
3:45
we'll learn a whole lot more
about Cullen along the way.
3:49
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