Shining, Shimmering, Splendid6:21 with Ben Deitch
In this video we'll start out by just printing the first line, but by the end of the video we'll have a print out of the entire game!
We've got each card being represented and exactly three characters. 0:00 Now let's get back to our debug print function and 0:04 see if we can handle printing that first line. 0:06 Let's start by creating a variable to hold our print string for 0:09 the first line, of our first line. 0:12 Then since the first thing on this line is the top waste file card. 0:16 Let set this equal to a string And 0:21 inside that string let's put dollar sign bracket, and let's do waste pile. 0:23 last. 0:29 But we can't forget that the waste pile can be empty. 0:31 So let's only use the last card if there is a last card. 0:34 If, waste pile. 0:40 size is greater than zero. 0:42 And if there is not a last card. 0:45 Let's just print three underscores to show an empty pile. 0:48 On the next line let's use the pad end function again to add spaces to our 0:52 first line to get it ready for adding in the foundation piles. 0:57 The first foundation pile starts on the fourth cell. 1:00 So we need to add padding to our first line 1:04 until it's the length of the first three cells. 1:06 And since each cell is six characters long 1:09 we need our first line to be 18 characters long before we start the foundations. 1:12 So let's type firstLine = firstLine.padEnd and pass in 18. 1:17 Now that we've got that covered, 1:25 let's move on to adding our foundation piles to our string. 1:27 The first step is to loop through each foundation pile. 1:31 FoundationPiles.foreach then inside the loop let's add to our first line. 1:33 FirstLine plus equals and to make it easy let's copy what we did for the wastePile. 1:42 And then just replace waste pile with it which is the foundation pile not cards. 1:52 And ultimately on the next line, let's add three spaces to the end of our 2:01 first line property as the space between the foundation piles. 2:06 And finally, at the bottom of the debug print method, 2:10 let's print out the first line. 2:13 S-O-U-T, tab, first line. 2:15 Then let's run the app and see what we get. 2:20 Well, I guess that's a good thing. 2:26 It's kind of boring, but it looks like it should, 2:28 especially considering at this point, we've only just set up the board. 2:31 Dont worry, things will get a bit more exciting around here 2:35 once we finish printing the tab row piles. 2:38 Remember, for the tab row piles, we'll be looping through 13 rows and 2:40 then inside each row, we'll be looping through the tab row piles. 2:44 But before we can get to the tab row piles, we need to add our space. 2:48 So in the next line, let's add an empty print statement. 2:53 Then let's create a loop to loop through all 13 possible rows. 2:57 for i in 0..12. 3:01 And inside this loop, 3:07 let's start by creating a variable to represent the string for this row. 3:10 Var row equals empty string. 3:15 Then lets a loop through our tableaupiles. 3:17 Tableaupiles.forEach. 3:20 And for each pile, we need to add on to our row variable. 3:23 So row +=, but before we start adding, 3:26 we need to know if this pile even has a card at index i. 3:31 Luckily, there's an easy way to accomplish this. 3:35 We can just check if the number of cards in that pile is greater than i. 3:38 So let's type if (it.cards.size > i) and 3:43 if it is then let's return a string 3:49 representation of the card at index i. 3:54 And inside the brackets, it.cards index i. 4:02 And if there is no card at index i, let's just add three spaces. 4:07 Then on the next line, let's not forget about the three spaces of padding. 4:16 And finally right below our Tablo piles loop, Let's print out our row. 4:24 Then let's run the app. 4:37 And awesome. 4:45 That's exactly what our board should look like and And 4:46 we can easily see the whole thing. 4:49 This is definitely going to make writing tests a lot more fun. 4:51 So, let's get right to it. 4:55 At this point we'd like to write some kind of test 4:57 to help us know that everything is working but before we can write the test, 5:00 we need to do a little more exploring about how we interact with our game model. 5:04 For starters, 5:09 let's see if we can tap the deck to turn a card over into the waste pile. 5:10 Over in our app, our kt file, let's add a line after the call to reset game, 5:14 and now we need to call the on deck tap method in our presenter. 5:20 But instead of creating a new presenter object. 5:24 Let's just change our presenter to be a singleton just like our model. 5:27 So we'll replace class with object. 5:31 After all we're only ever going to have one presenter. 5:34 Next back in the app file, let's call GamePresenter.onDeckTap, 5:37 then let's run the app again, and there we go we've got a card in the waste pile. 5:46 Also, notice that we didn't get a null pointer exception. 5:54 Over in the game presenter class, we definitely tried to call 5:58 view.update on a null object. 6:02 But thanks to the safe call operator, instead of a null pointer exception, 6:07 we just got null for the whole statement. 6:11 And so nothing happened which is pretty cool and 6:14 the next video will test our app and see where we stand. 6:18
You need to sign up for Treehouse in order to download course files.Sign up