1 00:00:00,180 --> 00:00:02,220 Now that we have a random number generator. 2 00:00:02,220 --> 00:00:06,220 Let's use it to make it possible for the towers to occasionally miss their targets. 3 00:00:07,410 --> 00:00:11,440 Let's start by deciding how accurate we want our towers to be and 4 00:00:11,440 --> 00:00:12,450 then make that a constant. 5 00:00:13,750 --> 00:00:17,630 We'll set the accuracy to .75. 6 00:00:17,630 --> 00:00:23,750 Meaning on average 75% of the time the tower will hit its target. 7 00:00:23,750 --> 00:00:26,430 No let's create a method that when called returns 8 00:00:26,430 --> 00:00:29,340 true when the tower successfully hits the target. 9 00:00:29,340 --> 00:00:32,590 We'll call it IsSuccessfulShot. 10 00:00:32,590 --> 00:00:35,490 Now here's where we can use our random number generator. 11 00:00:35,490 --> 00:00:39,940 We can ask for a number between zero and one by calling NextDouble, and 12 00:00:39,940 --> 00:00:43,340 return true if the number generated is less than the accuracy. 13 00:00:44,792 --> 00:00:48,423 The .NET random number generator is uniformly random, 14 00:00:48,423 --> 00:00:53,571 meaning every value between zero and one has an equal chance of being generated. 15 00:00:53,571 --> 00:00:59,256 So 75% of the time it will generate a number between zero and 0.75. 16 00:00:59,256 --> 00:01:02,670 So if the number it generates is less than 0.75, 17 00:01:02,670 --> 00:01:06,670 we'll count that as hitting the target otherwise it's a miss. 18 00:01:06,670 --> 00:01:07,920 Notice that here, 19 00:01:07,920 --> 00:01:12,580 when using the random field, we're referencing the Tower class name. 20 00:01:12,580 --> 00:01:14,800 This is how we use static members of a class. 21 00:01:16,150 --> 00:01:20,150 Actually, this is only necessary when accessing the number from 22 00:01:20,150 --> 00:01:23,590 outside the class in order to state which class is being used. 23 00:01:24,740 --> 00:01:29,020 However, since we're already in the Tower class, we don't need this. 24 00:01:29,020 --> 00:01:31,990 We could type it here but we don't need to. 25 00:01:31,990 --> 00:01:35,440 We can just use it like any other field in the class. 26 00:01:35,440 --> 00:01:40,520 What we can't do is type this, because this 27 00:01:40,520 --> 00:01:46,000 refers to the current object and the object doesn't have the random variable. 28 00:01:46,000 --> 00:01:47,890 Only the Tower class itself has one. 29 00:01:49,080 --> 00:01:53,650 Now we can check for successful shots in the fire on invaders method. 30 00:01:53,650 --> 00:01:57,050 We only want to decrease the health of the invader the tower attempts to shoot at, 31 00:01:57,050 --> 00:01:59,230 if it's a successful shot. 32 00:01:59,230 --> 00:02:03,262 So I'll wrap this call to decrease health by this if statement like so. 33 00:02:07,655 --> 00:02:09,852 To see what's happening while the game runs, 34 00:02:09,852 --> 00:02:13,040 let's print the results to the console here. 35 00:02:13,040 --> 00:02:15,636 Let's add the system namespace to the top of the file. 36 00:02:19,215 --> 00:02:21,732 And we can remove references to it here. 37 00:02:26,795 --> 00:02:29,350 Now, let's print something if it's a successful shot. 38 00:02:31,140 --> 00:02:34,810 So I'll say, shot at and hit an invader. 39 00:02:37,240 --> 00:02:38,974 Let's print something else if they miss. 40 00:02:44,017 --> 00:02:49,205 We'll say, shot at and missed an invader. 41 00:02:52,418 --> 00:02:53,930 Don't forget to type else here. 42 00:02:55,670 --> 00:02:58,893 Let's also print something if the invader has been neutralized. 43 00:02:58,893 --> 00:03:06,576 So I’ll say if invader.IsNeutralized. 44 00:03:10,995 --> 00:03:15,904 Then we'll print Neutralized an invader. 45 00:03:19,968 --> 00:03:23,116 Now let's compile and run the game to see how this changes things. 46 00:03:30,502 --> 00:03:34,590 As you can see, the player wins sometimes and loses sometimes. 47 00:03:35,700 --> 00:03:38,660 Adding a little randomness increases the challenge of playing the game.