Making the Deck3:23 with Ben Deitch
In this video we'll start learning about Arrays in Kotlin as we begin creating a class to represent a deck of cards!
We just finished up writing our one line card class. 0:00 Now, let's see how we can use this class to create an entire deck of cards for 0:03 a solitaire game. 0:07 Let's start by first creating a new class named Deck. 0:09 This class will store all 52 cards we need and 0:14 be responsible for things like shuffling and drawing a card. 0:17 To start off our deck, 0:22 let's first declare a new cards property to store all 52 cards. 0:24 And let's also make it immutable. 0:28 That way, we'll always have the same 52 cards. 0:30 So, val cards, and for the type, let's put an array. 0:34 And then inside the angle brackets, Card, to make it an array of cards. 0:42 Then we need to initialize our array. 0:48 Remember, unless you're making something abstract, you need to initialize it. 0:51 So let's add equals. 0:56 And then the easiest way to initialize an array is to use the arrayOf function. 0:59 Let's type array and then check it out. 1:04 There's the arrayOf function we're looking for. 1:07 But note there's also arrayOfNulls, emptyArray and 1:09 there's a special array for each of the Java primitives. 1:13 arrayOfNulls and emptyArray are here to make our lives easier for 1:17 when we need to initialize an array but don't yet know what to initialize it to. 1:21 And the primitive arrays, like booleanArray and 1:26 intArray, use primitives behind the scenes to make better use of system resources. 1:30 So if we're making an array of ints, 1:35 we'd be better off using an intArray than an array filled with ints. 1:37 Okay, back to our cards array. 1:42 Let's finish off the call to arrayOf. 1:44 And now that we've got our cards array equal to an array of nothing, and 1:47 since it's a mutable, it will always be an array of nothing. 1:51 Not great. 1:55 So inside the arrayOf function, let's add a new card. 1:57 Another awesome part about Kotlin is that just like semicolons, 2:01 they just completely got rid of the new keyword and it's great. 2:05 So let's type card then pass in a 0 for the value. 2:10 Let's pass in Clubs for the suit, and that's one card down, 51 to go. 2:15 So let's add a comma after our first card, and then declare the next card. 2:21 Card, pass in 1 for the value and Clubs for the suit. 2:26 And all right. 2:31 Just 50 more. 2:32 And we really could do it this way. 2:33 The arrayOf function will let us add as many cards as we want. 2:35 And editing tools wouldn't make it all that bad. 2:39 But of course, there is a better way. 2:43 Let's get rid of this arrayOf function. 2:46 And this time, let's create a new instance of the array class, ourselves. 2:48 Let's type, array. 2:53 And remember, we don't need the new keyword. 2:56 And then for the first parameter, the size, let's pass in 52. 2:59 The second parameter is an initializer function that takes in an int and 3:04 returns a card. 3:09 This way, we don't need to create all 52 cards ourselves. 3:10 We can just create a function mapping each index to a card and call it a day. 3:14 Which is exactly what we’ll do in the next video. 3:19
You need to sign up for Treehouse in order to download course files.Sign up