1 00:00:00,230 --> 00:00:04,370 So we need a function which maps the numbers zero through 51. 2 00:00:04,370 --> 00:00:08,750 To each of the 52 cards in a standard deck of cards. 3 00:00:08,750 --> 00:00:10,280 Here's how we're going to do it. 4 00:00:10,280 --> 00:00:14,460 First, the value of a card will be equal to the index mod 13. 5 00:00:14,460 --> 00:00:17,790 There are thirteen cards of each suit. 6 00:00:17,790 --> 00:00:23,420 So taking the index mod thirteen will give us four sets of zero through 12. 7 00:00:23,420 --> 00:00:24,250 Perfect. 8 00:00:24,250 --> 00:00:29,940 Second, the suit of a card will be split out based on the index divided by 13. 9 00:00:29,940 --> 00:00:34,260 Which, since they're both integers, will result in an integer. 10 00:00:34,260 --> 00:00:38,950 Leaving us with a nice breakdown of zeros, ones, twos and threes. 11 00:00:38,950 --> 00:00:42,530 Then, we just need to choose which number represents which suit. 12 00:00:42,530 --> 00:00:43,940 And we're done. 13 00:00:43,940 --> 00:00:44,640 Cool. 14 00:00:44,640 --> 00:00:46,630 Let's add some space above our cards array,. 15 00:00:46,630 --> 00:00:47,900 And get to work on this function. 16 00:00:49,910 --> 00:00:51,810 Start with the fun keyword. 17 00:00:51,810 --> 00:00:54,740 And then let's name it, index to card. 18 00:00:56,080 --> 00:01:00,170 Then, for the parameter, let's pass in i and make it an int. 19 00:01:01,190 --> 00:01:06,150 Then let's add a colon, and specify card as the return type. 20 00:01:06,150 --> 00:01:07,000 And then add our brackets. 21 00:01:08,970 --> 00:01:12,210 Then, let's make a new val named value. 22 00:01:12,210 --> 00:01:16,540 And set it equal to i mod 13. 23 00:01:16,540 --> 00:01:19,750 And let's make another new val, named suit. 24 00:01:21,690 --> 00:01:28,050 And set it equal to when I, divided by thirteen. 25 00:01:29,660 --> 00:01:30,682 And then add brackets. 26 00:01:30,682 --> 00:01:37,770 In Kotlin, instead of the switch statement, we have the win statement. 27 00:01:37,770 --> 00:01:39,180 And it looks like this. 28 00:01:39,180 --> 00:01:42,400 So when I divided by thirteen is zero. 29 00:01:42,400 --> 00:01:45,220 Let's set suit equal to clubs. 30 00:01:46,460 --> 00:01:53,101 Then let's set command or control+D three times, And 31 00:01:53,101 --> 00:01:59,296 then let's change these 32 00:01:59,296 --> 00:02:04,317 to 1 and diamonds. 33 00:02:04,317 --> 00:02:07,810 2 and 34 00:02:07,810 --> 00:02:12,000 hearts. 35 00:02:12,000 --> 00:02:14,890 And three with spades. 36 00:02:19,470 --> 00:02:20,220 Cool. 37 00:02:20,220 --> 00:02:21,870 But we're getting an error that. 38 00:02:23,500 --> 00:02:26,150 When expression must be exhaustive. 39 00:02:26,150 --> 00:02:28,400 Add necessary else branch. 40 00:02:28,400 --> 00:02:32,600 Normally, you can write a when statement without needing an else branch. 41 00:02:32,600 --> 00:02:37,060 But since we're using this when statement to populate our suit property. 42 00:02:37,060 --> 00:02:38,990 It must return a value. 43 00:02:38,990 --> 00:02:43,570 So lets just change the three to an else. 44 00:02:43,570 --> 00:02:44,920 And we're good to go. 45 00:02:44,920 --> 00:02:47,514 All that's left is to return a new card instance. 46 00:02:47,514 --> 00:02:53,740 Return card of value and suit. 47 00:02:53,740 --> 00:02:54,580 All right. 48 00:02:54,580 --> 00:02:57,940 We've got our function mapping each index to a card. 49 00:02:57,940 --> 00:03:02,800 Now we just need to pass in this function as the second parameter to our array. 50 00:03:02,800 --> 00:03:06,940 Unfortunately, we can't just pass in the name of a function. 51 00:03:06,940 --> 00:03:10,720 But what we can do, is pass in anonymous function. 52 00:03:10,720 --> 00:03:16,400 To make a function anonymous in Kotlin, all we have to do is remove the name. 53 00:03:17,850 --> 00:03:22,910 We can then store this function and a variable like that. 54 00:03:22,910 --> 00:03:25,810 And then use this variable down here in our array. 55 00:03:27,700 --> 00:03:29,470 Or, even better, we can just cut and 56 00:03:29,470 --> 00:03:32,052 paste the whole function into the second parameter. 57 00:03:43,934 --> 00:03:49,655 And now, when we create a new deck, we'll be ready with all 52 cards. 58 00:03:49,655 --> 00:03:51,085 But we're not done yet. 59 00:03:51,085 --> 00:03:53,525 This code can still look a bit better. 60 00:03:53,525 --> 00:03:55,135 And we'll see how in the next video.