1 00:00:00,630 --> 00:00:02,860 Since we know how many invaders there are and 2 00:00:02,860 --> 00:00:08,270 how much health each of them has, the outcome of the game is fairly predictable. 3 00:00:08,270 --> 00:00:12,090 There's lots of things we can do to make the game more interesting. 4 00:00:12,090 --> 00:00:16,060 For instance, we could break the game up into multiple rounds or 5 00:00:16,060 --> 00:00:18,550 limit how many towers the player can place. 6 00:00:18,550 --> 00:00:20,350 Or require the player to pay for 7 00:00:20,350 --> 00:00:24,370 towers using resources that they acquire while playing the game. 8 00:00:24,370 --> 00:00:26,820 These are all common elements in tower defense games. 9 00:00:28,160 --> 00:00:30,250 One thing that nearly all games have, 10 00:00:30,250 --> 00:00:33,840 including tower defense games, is uncertainty. 11 00:00:33,840 --> 00:00:38,310 Uncertainty means that even if you were to play the game the exact same way 12 00:00:38,310 --> 00:00:41,540 every time, you might get different outcomes. 13 00:00:41,540 --> 00:00:44,720 One way we can add uncertainty to treehouse defense 14 00:00:44,720 --> 00:00:48,750 is by making it possible for the towers to miss their target. 15 00:00:48,750 --> 00:00:53,500 Right now, if a tower shoots an invader, it's always successful. 16 00:00:53,500 --> 00:00:56,810 This makes the outcome of the game rather predictable. 17 00:00:56,810 --> 00:01:00,050 We can make it possible for the towers to occasionally miss. 18 00:01:01,180 --> 00:01:05,280 The easiest way to create uncertainty is to use a random number generator. 19 00:01:06,630 --> 00:01:10,850 A simple random number generator produces a randomly created number 20 00:01:10,850 --> 00:01:12,100 every time it's asked for one. 21 00:01:13,350 --> 00:01:16,410 The .NET framework contains a class named random 22 00:01:16,410 --> 00:01:19,160 that provides a random number generator. 23 00:01:19,160 --> 00:01:23,750 Let's use the random class to make our game less predictable. 24 00:01:23,750 --> 00:01:28,110 To get an idea of how the random class provided by the .NET framework works, 25 00:01:28,110 --> 00:01:30,340 let's open the C# repo. 26 00:01:30,340 --> 00:01:33,830 First, we need to construct an object of type random. 27 00:01:33,830 --> 00:01:38,660 Random is in the system namespace, so we'll type System.Random, 28 00:01:38,660 --> 00:01:42,830 give it a name equals new, 29 00:01:42,830 --> 00:01:46,490 System.Random. 30 00:01:46,490 --> 00:01:51,390 Now that we have a random number generator object, we can ask it for random numbers. 31 00:01:51,390 --> 00:01:53,250 We can do this by calling the next method. 32 00:01:55,250 --> 00:01:58,030 This gives a new random integer. 33 00:01:58,030 --> 00:02:00,395 Another handy method is NextDouble. 34 00:02:00,395 --> 00:02:06,710 NextDouble returns a random decimal number that's between zero and one. 35 00:02:07,800 --> 00:02:11,510 Notice that every time these methods are called, they return a different number. 36 00:02:13,140 --> 00:02:16,410 The random number generator provided by the .NET framework 37 00:02:16,410 --> 00:02:19,610 is actually a pseudo random number generator. 38 00:02:19,610 --> 00:02:21,820 If you'd like to know more about what that means, 39 00:02:21,820 --> 00:02:25,590 then check out the teacher's notes for a link to the random classes documentation. 40 00:02:26,670 --> 00:02:29,790 For what we want, the random class is more than sufficient. 41 00:02:31,070 --> 00:02:33,390 Using random, we're going to make it possible for 42 00:02:33,390 --> 00:02:36,300 a tower to occasionally miss its target. 43 00:02:36,300 --> 00:02:40,730 Each tower object will need to have access to an instance of the random class. 44 00:02:41,860 --> 00:02:45,650 We could instantiate the random class for each tower object. 45 00:02:45,650 --> 00:02:47,550 This is unnecessary though. 46 00:02:47,550 --> 00:02:51,150 The towers actually just need to share a single random object. 47 00:02:52,310 --> 00:02:56,770 They'll all call the random's next method, and each time they'll get a random number. 48 00:02:57,860 --> 00:03:02,210 There's a way to allow all objects of a class to share the same objects. 49 00:03:03,310 --> 00:03:06,730 We can make the random object a static field of the tower class. 50 00:03:08,140 --> 00:03:09,770 We've used static members before. 51 00:03:10,810 --> 00:03:15,040 We used the static square root and power methods from the math class. 52 00:03:15,040 --> 00:03:17,820 We also used the static WriteLine method in the tower class. 53 00:03:18,860 --> 00:03:23,740 Remember, to use a static method, we don't have to have an instance of the class. 54 00:03:23,740 --> 00:03:25,570 We call them on the class itself. 55 00:03:27,170 --> 00:03:29,540 We can also have static fields and properties. 56 00:03:30,610 --> 00:03:35,080 Let's add a static field to the tower class for the random number generator and 57 00:03:35,080 --> 00:03:35,890 see what that looks like. 58 00:03:37,510 --> 00:03:40,905 Static members of a class can have any accessibility level. 59 00:03:40,905 --> 00:03:45,830 Math.squareroot is a public method so that any other class can use it. 60 00:03:45,830 --> 00:03:49,350 Only the tower class needs access to this, so we'll make it private. 61 00:03:50,470 --> 00:03:52,855 Next, and this is what makes the field static. 62 00:03:52,855 --> 00:03:55,790 We'll type the static keyword. 63 00:03:55,790 --> 00:03:58,590 We'll also need to make this field read only since we don't want 64 00:03:58,590 --> 00:04:00,840 this field to ever be overridden. 65 00:04:00,840 --> 00:04:03,883 Now, we'll declare this field just like any other field and 66 00:04:03,883 --> 00:04:05,503 initialize it at the same time. 67 00:04:10,334 --> 00:04:12,405 Because we made this field static, 68 00:04:12,405 --> 00:04:17,310 there can only be one of these no matter how many towers are created. 69 00:04:17,310 --> 00:04:20,920 In contrast, location is not static and 70 00:04:20,920 --> 00:04:24,200 every tower object has its own location on the map. 71 00:04:24,200 --> 00:04:27,320 Which can be different for every tower object. 72 00:04:27,320 --> 00:04:30,510 So, random is initialized once and only once. 73 00:04:30,510 --> 00:04:32,510 And it's shared by all the other tower objects. 74 00:04:33,570 --> 00:04:34,790 In the next video, 75 00:04:34,790 --> 00:04:38,220 we'll see how to use random to add some uncertainty to our game.