Let the Games Begin5:47 with Ben Deitch
In this video we'll start creating a function to help us set up the game. We'll also learn about how we can use extension functions to write less code!
[MUSIC] 0:00 We finally finished creating all of the pieces that make up a game of solitaire. 0:03 And now it's time that we start putting these pieces together to create a fully 0:09 functional game. 0:12 The first step in any game of Solitaire is to set up the play area. 0:14 We need to make sure that we're starting the game with an empty waste pile, 0:18 as well as empty foundation piles. 0:21 Then we need to shuffle the deck [SOUND] and 0:24 deal out the cards into seven new tableau piles. 0:26 Inside our GameModel class, let's create a new function named resetGame. 0:28 This function will be responsible for resetting our game to the starting state. 0:34 We'll call it once when the app is launched and 0:38 we'll call it another time each time we want to start a new game. 0:41 Inside this function, 0:44 the first thing we're going to do is reset the waste pile to be empty. 0:45 So let's type wastePile.clear and 0:49 then move on to clearing out our foundation piles. 0:52 To clear out our foundation piles, 0:55 all we need to do is call our reset function on each pile. 0:57 Sounds like we need to use another for loop. 1:01 Let's add a line below where we clear our waste pile and then type for (pile 1:03 in foundationPiles) and then brackets. 1:08 To loop over all of our foundation piles then inside the for loop, 1:15 let's call the reset function on each of the piles, pile.reset. 1:20 And now we've reset all of our foundation piles as well, but 1:26 there's actually a better way we can do this. 1:29 So let's delete that and 1:32 instead let's type foundationPiles.forEach. 1:35 And then inside the brackets, let's type it, 1:40 which represents a foundation pile .reset. 1:44 And there we go. 1:50 It's functionally the same code but now we're doing it all on one line. 1:51 Before we get any further, let's take a minute to talk about this for 1:56 each function and how it works behind the scenes. 1:59 First off, we need to realize that for each is a function that takes an action. 2:02 Also known as a lambda expression, which remember, 2:07 is just an anonymous function with different syntax. 2:11 To make that a little more clear, 2:14 I'm going to add parentheses around the brackets. 2:16 And since there's no error, 2:20 it's clear that this is really just the first parameter. 2:21 In Kotlin, when a function only has one parameter and 2:25 that parameter is an action, we're allowed to leave off the parentheses. 2:29 So I'll undo adding them. 2:33 And now I'd like to talk about extension functions. 2:37 You probably haven't noticed but 2:40 we've used a few extension functions already like first and last. 2:42 An extension function is a way for 2:46 us to add a function to a class without modifying that class. 2:48 Check it out. 2:52 Let's say we wanted to have a function on the string class 2:53 to tell us if the underlying string was longer than five characters. 2:56 And we wanted to name this function, isLongerThan5. 3:01 This would be pretty impossible with Java. 3:04 But with Kotlin, it's super easy. 3:06 And just for fun, let's do this as a package level function. 3:09 So let's add some space below our GameModel class and 3:13 then type fun_String dot, the function name, 3:16 isLongerThan5. 3:21 Then let's add our parentheses. 3:26 Make it return a Boolean and then add our brackets. 3:28 If you want to add a function to an already existing class, 3:32 all you need to do is add the name of the function after the name of the class. 3:36 It's super easy and super useful. 3:42 Back inside the function, let's make it return true 3:45 if the string is longer than five characters, and false otherwise. 3:48 So let's type return. 3:52 And then to access the underlying string, we just use the this keyword. 3:55 So we'd like to return this.length > 5. 4:00 Now that we've created our first extension function, let's see how we can use it. 4:05 Let's jump over to our app file and 4:10 then let's see if Treehouse is longer than five characters. 4:13 First, let's create a variable for our Treehouse string. 4:17 val treehouse = Treehouse. 4:20 And then let's print out whether or not our string is longer than five characters. 4:27 sout Tab or just the println function, and then inside this, 4:32 let's print treehouse.isLongerThan5. 4:38 Then let's quickly run this by first changing our run configuration to 4:42 AppKT and then hitting the Run button. 4:47 And there we go, treehouse is longer than five characters. 4:50 Now that we've got a better understanding of extension functions, 4:55 let's go ahead undo all of that. 4:58 But before we continue on with our resetGame function, let's use Cmd or 5:07 Ctrl+B on the forEach method to go to its declaration. 5:12 And here we can see that forEach is an extension function on the array class, and 5:18 it takes on a function mapping from type T, 5:23 which in our case is a foundation pile, to Unit. 5:26 And inside the function, it just loops over all the elements and 5:31 this array and performs the action on each element. 5:35 Pretty sweet, right? 5:40 In the next video, 5:42 we'll pick up where we left off with our GameModel's resetGame function. 5:43
You need to sign up for Treehouse in order to download course files.Sign up