1 00:00:00,430 --> 00:00:03,790 We just finished up writing our one line card class. 2 00:00:03,790 --> 00:00:07,880 Now, let's see how we can use this class to create an entire deck of cards for 3 00:00:07,880 --> 00:00:09,290 a solitaire game. 4 00:00:09,290 --> 00:00:12,400 Let's start by first creating a new class named Deck. 5 00:00:14,080 --> 00:00:17,690 This class will store all 52 cards we need and 6 00:00:17,690 --> 00:00:22,515 be responsible for things like shuffling and drawing a card. 7 00:00:22,515 --> 00:00:24,016 To start off our deck, 8 00:00:24,016 --> 00:00:28,449 let's first declare a new cards property to store all 52 cards. 9 00:00:28,449 --> 00:00:30,510 And let's also make it immutable. 10 00:00:30,510 --> 00:00:34,130 That way, we'll always have the same 52 cards. 11 00:00:34,130 --> 00:00:40,770 So, val cards, and for the type, let's put an array. 12 00:00:42,640 --> 00:00:48,790 And then inside the angle brackets, Card, to make it an array of cards. 13 00:00:48,790 --> 00:00:51,910 Then we need to initialize our array. 14 00:00:51,910 --> 00:00:56,490 Remember, unless you're making something abstract, you need to initialize it. 15 00:00:56,490 --> 00:00:57,720 So let's add equals. 16 00:00:59,290 --> 00:01:04,600 And then the easiest way to initialize an array is to use the arrayOf function. 17 00:01:04,600 --> 00:01:07,020 Let's type array and then check it out. 18 00:01:07,020 --> 00:01:09,577 There's the arrayOf function we're looking for. 19 00:01:09,577 --> 00:01:13,525 But note there's also arrayOfNulls, emptyArray and 20 00:01:13,525 --> 00:01:17,742 there's a special array for each of the Java primitives. 21 00:01:17,742 --> 00:01:21,555 arrayOfNulls and emptyArray are here to make our lives easier for 22 00:01:21,555 --> 00:01:26,890 when we need to initialize an array but don't yet know what to initialize it to. 23 00:01:26,890 --> 00:01:30,012 And the primitive arrays, like booleanArray and 24 00:01:30,012 --> 00:01:35,230 intArray, use primitives behind the scenes to make better use of system resources. 25 00:01:35,230 --> 00:01:37,620 So if we're making an array of ints, 26 00:01:37,620 --> 00:01:42,220 we'd be better off using an intArray than an array filled with ints. 27 00:01:42,220 --> 00:01:44,190 Okay, back to our cards array. 28 00:01:44,190 --> 00:01:46,230 Let's finish off the call to arrayOf. 29 00:01:47,450 --> 00:01:51,330 And now that we've got our cards array equal to an array of nothing, and 30 00:01:51,330 --> 00:01:55,960 since it's a mutable, it will always be an array of nothing. 31 00:01:55,960 --> 00:01:57,610 Not great. 32 00:01:57,610 --> 00:02:01,620 So inside the arrayOf function, let's add a new card. 33 00:02:01,620 --> 00:02:05,980 Another awesome part about Kotlin is that just like semicolons, 34 00:02:05,980 --> 00:02:10,190 they just completely got rid of the new keyword and it's great. 35 00:02:10,190 --> 00:02:14,070 So let's type card then pass in a 0 for the value. 36 00:02:15,100 --> 00:02:21,260 Let's pass in Clubs for the suit, and that's one card down, 51 to go. 37 00:02:21,260 --> 00:02:26,330 So let's add a comma after our first card, and then declare the next card. 38 00:02:26,330 --> 00:02:30,460 Card, pass in 1 for the value and Clubs for the suit. 39 00:02:31,650 --> 00:02:32,570 And all right. 40 00:02:32,570 --> 00:02:33,910 Just 50 more. 41 00:02:33,910 --> 00:02:35,695 And we really could do it this way. 42 00:02:35,695 --> 00:02:39,412 The arrayOf function will let us add as many cards as we want. 43 00:02:39,412 --> 00:02:43,450 And editing tools wouldn't make it all that bad. 44 00:02:43,450 --> 00:02:46,240 But of course, there is a better way. 45 00:02:46,240 --> 00:02:48,680 Let's get rid of this arrayOf function. 46 00:02:48,680 --> 00:02:53,450 And this time, let's create a new instance of the array class, ourselves. 47 00:02:53,450 --> 00:02:54,520 Let's type, array. 48 00:02:56,880 --> 00:02:59,565 And remember, we don't need the new keyword. 49 00:02:59,565 --> 00:03:04,299 And then for the first parameter, the size, let's pass in 52. 50 00:03:04,299 --> 00:03:09,187 The second parameter is an initializer function that takes in an int and 51 00:03:09,187 --> 00:03:10,334 returns a card. 52 00:03:10,334 --> 00:03:14,630 This way, we don't need to create all 52 cards ourselves. 53 00:03:14,630 --> 00:03:19,770 We can just create a function mapping each index to a card and call it a day. 54 00:03:19,770 --> 00:03:22,597 Which is exactly what we’ll do in the next video.