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 see how we can write a function to map the numbers 051 to each of the 52 cards in a deck!
Kotlin Links
Project Files

0:00
So we need a function which maps the numbers zero through 51.

0:04
To each of the 52 cards in a standard deck of cards.

0:08
Here's how we're going to do it.

0:10
First, the value of a card will be equal to the index mod 13.

0:14
There are thirteen cards of each suit.

0:17
So taking the index mod thirteen will give us four sets of zero through 12.

0:23
Perfect.

0:24
Second, the suit of a card will be split out based on the index divided by 13.

0:29
Which, since they're both integers, will result in an integer.

0:34
Leaving us with a nice breakdown of zeros, ones, twos and threes.

0:38
Then, we just need to choose which number represents which suit.

0:42
And we're done.

0:43
Cool.

0:44
Let's add some space above our cards array,.

0:46
And get to work on this function.

0:49
Start with the fun keyword.

0:51
And then let's name it, index to card.

0:56
Then, for the parameter, let's pass in i and make it an int.

1:01
Then let's add a colon, and specify card as the return type.

1:06
And then add our brackets.

1:08
Then, let's make a new val named value.

1:12
And set it equal to i mod 13.

1:16
And let's make another new val, named suit.

1:21
And set it equal to when I, divided by thirteen.

1:29
And then add brackets.

1:30
In Kotlin, instead of the switch statement, we have the win statement.

1:37
And it looks like this.

1:39
So when I divided by thirteen is zero.

1:42
Let's set suit equal to clubs.

1:46
Then let's set command or control+D three times, And

1:53
then let's change these

1:59
to 1 and diamonds.

2:04
2 and

2:07
hearts.

2:12
And three with spades.

2:19
Cool.

2:20
But we're getting an error that.

2:23
When expression must be exhaustive.

2:26
Add necessary else branch.

2:28
Normally, you can write a when statement without needing an else branch.

2:32
But since we're using this when statement to populate our suit property.

2:37
It must return a value.

2:38
So lets just change the three to an else.

2:43
And we're good to go.

2:44
All that's left is to return a new card instance.

2:47
Return card of value and suit.

2:53
All right.

2:54
We've got our function mapping each index to a card.

2:57
Now we just need to pass in this function as the second parameter to our array.

3:02
Unfortunately, we can't just pass in the name of a function.

3:06
But what we can do, is pass in anonymous function.

3:10
To make a function anonymous in Kotlin, all we have to do is remove the name.

3:17
We can then store this function and a variable like that.

3:22
And then use this variable down here in our array.

3:27
Or, even better, we can just cut and

3:29
paste the whole function into the second parameter.

3:43
And now, when we create a new deck, we'll be ready with all 52 cards.

3:49
But we're not done yet.

3:51
This code can still look a bit better.

3:53
And we'll see how in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up