**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 0-51 to each of the 52 cards in a deck!

#### Kotlin Links

#### Project Files

So we need a function which maps the numbers zero through 51. 0:00 To each of the 52 cards in a standard deck of cards. 0:04 Here's how we're going to do it. 0:08 First, the value of a card will be equal to the index mod 13. 0:10 There are thirteen cards of each suit. 0:14 So taking the index mod thirteen will give us four sets of zero through 12. 0:17 Perfect. 0:23 Second, the suit of a card will be split out based on the index divided by 13. 0:24 Which, since they're both integers, will result in an integer. 0:29 Leaving us with a nice breakdown of zeros, ones, twos and threes. 0:34 Then, we just need to choose which number represents which suit. 0:38 And we're done. 0:42 Cool. 0:43 Let's add some space above our cards array,. 0:44 And get to work on this function. 0:46 Start with the fun keyword. 0:49 And then let's name it, index to card. 0:51 Then, for the parameter, let's pass in i and make it an int. 0:56 Then let's add a colon, and specify card as the return type. 1:01 And then add our brackets. 1:06 Then, let's make a new val named value. 1:08 And set it equal to i mod 13. 1:12 And let's make another new val, named suit. 1:16 And set it equal to when I, divided by thirteen. 1:21 And then add brackets. 1:29 In Kotlin, instead of the switch statement, we have the win statement. 1:30 And it looks like this. 1:37 So when I divided by thirteen is zero. 1:39 Let's set suit equal to clubs. 1:42 Then let's set command or control+D three times, And 1:46 then let's change these 1:53 to 1 and diamonds. 1:59 2 and 2:04 hearts. 2:07 And three with spades. 2:12 Cool. 2:19 But we're getting an error that. 2:20 When expression must be exhaustive. 2:23 Add necessary else branch. 2:26 Normally, you can write a when statement without needing an else branch. 2:28 But since we're using this when statement to populate our suit property. 2:32 It must return a value. 2:37 So lets just change the three to an else. 2:38 And we're good to go. 2:43 All that's left is to return a new card instance. 2:44 Return card of value and suit. 2:47 All right. 2:53 We've got our function mapping each index to a card. 2:54 Now we just need to pass in this function as the second parameter to our array. 2:57 Unfortunately, we can't just pass in the name of a function. 3:02 But what we can do, is pass in anonymous function. 3:06 To make a function anonymous in Kotlin, all we have to do is remove the name. 3:10 We can then store this function and a variable like that. 3:17 And then use this variable down here in our array. 3:22 Or, even better, we can just cut and 3:27 paste the whole function into the second parameter. 3:29 And now, when we create a new deck, we'll be ready with all 52 cards. 3:43 But we're not done yet. 3:49 This code can still look a bit better. 3:51 And we'll see how in the next video. 3:53

You need to sign up for Treehouse in order to download course files.

Sign up