Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
It's Alive!6:33 with Ben Deitch
In this video we'll write a test to help give us an idea of whether or not our game is working!
We're all ready with our debug print function and 0:00 we've got a pretty good idea about how we can drive the game by using our presenter. 0:02 Now let's get to that test. 0:07 Let's start by adding a new class to our test directory. 0:09 And let's name it GameTest. 0:15 Then inside this class, 0:17 let's add the test annotation, and then create our test function. 0:21 To test our game, 0:27 we're going to see if we can get a king into our first foundation pile. 0:29 So let's name this function, kingInFirstFoundationPile. 0:33 Then let's add the arrange act assert comments, 0:40 they're in the teacher's notes below as well. 0:42 And for the arrange section it might take us a few games to get a king into 0:49 the first foundation pile. 0:54 So let's declare a num games property to keep track of 0:56 how many games have been played. 0:59 Var numGames, and let's start it at zero. 1:01 Let's also declare a max games property to make sure we don't get stuck in 1:05 an infinite loop, val maxGames and let's set it equal to 10,000. 1:09 Then in the act section let's start with a for 1:17 loop that loops through the max number of games. 1:20 For i in 1 to maxGames and inside the loop let's 1:23 first increment our numGames property by one. 1:28 Then let's reset the game, GameModel.resetGame. 1:35 And next to simulate a game being played let's tap the deck 1:41 then tap the waste pile and then tap the last card and 1:45 each of the tableau tiles and let's do all of that 100 times. 1:49 So let's start by creating a four loop, to loop from one to 100. 1:53 For j in 1 to 100 and 1:57 then inside this loop, let's tap the deck GamePresenter.onDeckTap, 2:04 then tap the waste pile, GamePresenter.onWasteTap. 2:11 And then tap the last card and each tab low pile GameModel.tableauPiles and 2:16 let's use the for each indexed function. 2:23 And for the index let's change this to index and then inside this function 2:26 lets call GamePresenter.onTableauTap pass in index for the Tableau index. 2:32 And for the second parameter to tap the last card in each pile, 2:41 let's pass in the index of the last card. 2:45 tableauPile.cards.lastIndex. 2:48 Next, after we've tapped everything but the foundation's 100 times. 2:56 Let's check and see if we've got a king and the first foundation. 3:01 An easy way to do this is just to check if the foundation has 13 cards in it. 3:04 The only way a foundation can have 13 cards in it is if it has a king on it. 3:09 So let's type if GameModel.foundationPiles and 3:14 we want the 0 indexed one .cards.size == 13. 3:21 And if it does equal 13, then we've succeeded in getting a king. 3:28 So let's break out of our loop and move on to the assert section. 3:34 In the assert section. 3:40 Let's start by printing out the game model so we can see what's going on. 3:42 GameModel.debugPrint then on the next one just for fun, 3:45 let's print out how many games were played and I will call this num Games and 3:51 then we'll print out numGames. 3:57 Finally let's assert that numGames is less than the max. 4:00 Assert that numGames is less than maxGames. 4:05 So we're asserting that and less than 10,000 games we will get a king and 4:12 the first foundation pile, which is a pretty good test. 4:17 All right, now for the moment of truth. 4:21 Let's click the Run button next to our test to run it and see what we get. 4:24 Cool, and if we run it a few more times. 4:34 We should be able to find one where we didn't completely finish the game, 4:37 Here we go, hey that's not quite right. 4:45 Some of these cards should be face down. 4:48 Well, it turns out that as part of our decks reset function 4:50 we should also turn all the cards face down. 4:54 So over on our deck class before we shuffle the deck, 4:57 let's call cardsInDeck.forEach. 5:02 And for each card in the deck, let's make it face down. 5:08 Face up equals false. 5:15 Then let's run our test a few more times. 5:18 Perhaps a lot more times. 5:29 And there we go, our cards are back to being hidden. 5:40 I don't know about you but I'm feeling a whole lot more confident in our game. 5:44 We haven't even played it and 5:49 yet thanks to our test, I'm pretty sure it's going to work just fine. 5:50 I bet you didn't think we'd be able to get all of this done in such a short time but 5:55 thanks to Colon we're able to do a lot with just a little code. 6:00 From package level properties, to extension functions, and 6:04 even to just not needing to add semi-colons, 6:07 Colon does a great job at making our lives a lot easier. 6:10 I hope you've learned a ton from this course and 6:15 that you never forget any of it. 6:17 But if you do, remember that the Colon docs are awesome. 6:20 So if you find yourself confused about something just check out the docs. 6:23 If that doesn't work you can always make a post in the community and 6:27 will be happy to help. 6:31 Until next time. 6:32
You need to sign up for Treehouse in order to download course files.Sign up