1 00:00:00,180 --> 00:00:02,880 So the problem with computerFire and 2 00:00:02,880 --> 00:00:07,200 computerPlaceShip is that they have random outcomes built in. 3 00:00:07,200 --> 00:00:10,320 Each of these functions really does two different things. 4 00:00:10,320 --> 00:00:15,580 First they build a random location, then they do something with that location, 5 00:00:15,580 --> 00:00:18,200 like fire a shot or place a ship. 6 00:00:19,600 --> 00:00:23,630 In programming, we call this tight coupling when two distinct functions 7 00:00:23,630 --> 00:00:26,960 are bundled up so that they can only really be used together. 8 00:00:26,960 --> 00:00:30,730 Now a hallmark of tightly coupled code is that it makes our job of writing unit 9 00:00:30,730 --> 00:00:32,310 tests harder. 10 00:00:32,310 --> 00:00:34,940 So to refactor this code I think we can actually just 11 00:00:34,940 --> 00:00:37,250 abstract those two ideas apart. 12 00:00:37,250 --> 00:00:41,290 We see that the same code appears in both the computerFire and 13 00:00:41,290 --> 00:00:43,220 computerPlaceShip functions, 14 00:00:43,220 --> 00:00:47,480 the part that generates two numbers in the board range and puts them in an array. 15 00:00:47,480 --> 00:00:51,810 So we can pull that out into its own function that returns the result. 16 00:00:51,810 --> 00:00:54,040 Let's call the function getRandomCoordinates. 17 00:01:06,540 --> 00:01:10,872 Then I'll cut the variable declarations for x, y, and coordinates out of 18 00:01:10,872 --> 00:01:15,230 the computerFire function and paste them inside getRandomCoordinates. 19 00:01:16,370 --> 00:01:19,993 So now to put the x and y numbers in an array, 20 00:01:19,993 --> 00:01:25,250 instead of var coordinates = x, y, we'll say return x, y. 21 00:01:26,950 --> 00:01:29,930 So now I can delete these from computerPlaceShip as well. 22 00:01:34,790 --> 00:01:40,790 So, computerPlayShip also randomly chooses horizontal or vertical. 23 00:01:40,790 --> 00:01:44,910 So we can use the same strategy by pulling out that logic into a separate function. 24 00:01:46,170 --> 00:01:48,485 Let's call this function getRandomDirection. 25 00:01:56,451 --> 00:02:01,023 So now I'll go ahead and cut the direction declaration out of 26 00:02:01,023 --> 00:02:05,785 computerPlaceShip and paste it inside getRandomDirection. 27 00:02:07,940 --> 00:02:12,668 And in this function instead of var direction equals = 28 00:02:12,668 --> 00:02:18,061 Math.random, we'll say return Math.random. 29 00:02:18,061 --> 00:02:22,884 So at this point there's actually nothing left of our original 30 00:02:22,884 --> 00:02:26,640 functions besides a call to fire and placeShip. 31 00:02:26,640 --> 00:02:30,820 So, at the bottom of our file we can remove the module that exports for 32 00:02:30,820 --> 00:02:33,280 computerPlaceShip and computerFire. 33 00:02:35,701 --> 00:02:39,552 But now we can also use the randomizers with the existing fire and 34 00:02:39,552 --> 00:02:43,629 placeShip functions in place of the original computer functions. 35 00:02:51,710 --> 00:02:55,303 So for example, we can set up the game to call fire and 36 00:02:55,303 --> 00:03:01,208 placeShip with some attempted random coordinates in-between human player turns. 37 00:03:01,208 --> 00:03:02,318 Like this. 38 00:03:15,531 --> 00:03:18,988 We also get to use these randomizer functions in other parts of our game 39 00:03:18,988 --> 00:03:20,450 engine if we need to. 40 00:03:20,450 --> 00:03:21,700 That's a big plus. 41 00:03:21,700 --> 00:03:24,020 More reusable code will save us time later. 42 00:03:26,530 --> 00:03:29,220 And this way we don't have to add any tests at all. 43 00:03:29,220 --> 00:03:31,880 In fact, this is another thing that writing tests helps us with. 44 00:03:33,710 --> 00:03:36,007 When it's hard to write tests for something, 45 00:03:36,007 --> 00:03:40,060 you might wonder whether you really need that function in the first place. 46 00:03:40,060 --> 00:03:43,780 And this way, writing tests can guide us toward simpler code. 47 00:03:43,780 --> 00:03:46,690 We still can't do a super meaningful test for 48 00:03:46,690 --> 00:03:49,430 the randomizer functions because of their random nature. 49 00:03:49,430 --> 00:03:54,460 But we've pulled the random pieces apart from our other application code so 50 00:03:54,460 --> 00:03:56,620 that our other parts remain testable. 51 00:03:56,620 --> 00:04:00,175 Thinking about good unit test has improved our code yet again, 52 00:04:00,175 --> 00:04:02,336 making it more reusable and modular. 53 00:04:15,425 --> 00:04:17,310 We've learned a lot in this stage. 54 00:04:17,310 --> 00:04:20,100 Set up and tear down in our test suites, testing for 55 00:04:20,100 --> 00:04:23,870 edge cases, and keeping our code decoupled and testable. 56 00:04:23,870 --> 00:04:28,020 So you're ready to use Mocha and Chai to keep doing BDD in your applications. 57 00:04:29,050 --> 00:04:32,990 In the last stage, I'll show you some other convenient ways to display your test 58 00:04:32,990 --> 00:04:35,960 results and introduce some more advanced features of Mocha.