1 00:00:00,490 --> 00:00:03,460 Another place we'll want to use arrays is to create a path for 2 00:00:03,460 --> 00:00:05,160 invaders to move down. 3 00:00:05,160 --> 00:00:09,430 We can represent the path in code as an array of map locations. 4 00:00:09,430 --> 00:00:11,750 Starting at index zero in the array, 5 00:00:11,750 --> 00:00:15,720 invaders can move down the path by moving to the next index in the array. 6 00:00:17,010 --> 00:00:20,540 To start, let's create an array of map locations here in Main. 7 00:00:20,540 --> 00:00:23,580 This will create a path that moves from the left to right across 8 00:00:23,580 --> 00:00:24,350 the middle of the map. 9 00:00:25,470 --> 00:00:29,050 So we've already got our mapLocation variable here. 10 00:00:29,050 --> 00:00:33,831 So let's take this out and put our square brackets here. 11 00:00:33,831 --> 00:00:37,752 We'll call it path, and opening closing curly braces, 12 00:00:37,752 --> 00:00:40,375 we'll use the shortcut method here. 13 00:00:40,375 --> 00:00:45,361 And then we'll create a new MapLocation 14 00:00:45,361 --> 00:00:50,491 starting at x equals 0 and y equals 2, so 15 00:00:50,491 --> 00:00:55,929 that it's going down the middle of the map. 16 00:00:55,929 --> 00:00:59,370 And we'll pass in the map so that we can validate that it's on the map. 17 00:01:00,840 --> 00:01:05,057 Now we need to create seven more of these, so I'll just copy and 18 00:01:05,057 --> 00:01:07,376 paste here till I got eight total. 19 00:01:07,376 --> 00:01:09,459 So, let's see here. 20 00:01:09,459 --> 00:01:12,764 One, two, three, four, five, six, seven, eight. 21 00:01:12,764 --> 00:01:13,655 Okay, now, 22 00:01:13,655 --> 00:01:18,590 we need to make the path move from the left to the right side of the map. 23 00:01:18,590 --> 00:01:22,080 So starting at zero, we'll go to, so 24 00:01:22,080 --> 00:01:26,661 this is x equals 0, then we'll go to x equals 1, 25 00:01:26,661 --> 00:01:31,917 x equals 2, x equals 3, x equals 4, 5, 6, and 7. 26 00:01:31,917 --> 00:01:35,577 All right. So now we've got an array of map locations 27 00:01:35,577 --> 00:01:40,050 that represents our path going down the middle of the map. 28 00:01:41,070 --> 00:01:44,080 Even though we've named this array of map locations path, 29 00:01:44,080 --> 00:01:46,890 it's still just an array of map locations. 30 00:01:46,890 --> 00:01:48,280 It isn't a path object. 31 00:01:49,320 --> 00:01:53,280 This is the data that specifies where the path is on the map. 32 00:01:53,280 --> 00:01:58,700 We really should wrap this data in a class that models not only where the path is, 33 00:01:58,700 --> 00:02:01,470 but also how the path is used. 34 00:02:01,470 --> 00:02:06,704 That brings us to the next core principle of object-oriented programming, 35 00:02:06,704 --> 00:02:08,080 encapsulation. 36 00:02:08,080 --> 00:02:13,870 Encapsulation helps to define what an object is while hiding how it works. 37 00:02:13,870 --> 00:02:18,010 A good example of encapsulation in the physical world is a car. 38 00:02:18,010 --> 00:02:21,860 Cars are complex machines with lots of moving parts inside. 39 00:02:21,860 --> 00:02:26,790 However, you don't really need to know all about these parts in order to drive a car. 40 00:02:26,790 --> 00:02:30,460 You only need to know how to use the steering wheel, gas pedal, 41 00:02:30,460 --> 00:02:32,710 brake pedal, and gear shifter. 42 00:02:32,710 --> 00:02:36,270 Everything else is hidden from view as much as possible. 43 00:02:36,270 --> 00:02:38,370 Much of it is under the hood, if you will. 44 00:02:39,440 --> 00:02:43,070 What's nice about this system us that even though different cars 45 00:02:43,070 --> 00:02:48,080 might have different engines, I can still get in any car and drive it. 46 00:02:48,080 --> 00:02:50,540 All this makes driving a car a lot easier. 47 00:02:50,540 --> 00:02:53,985 And by making the car easier to drive, it's also safer to use. 48 00:02:55,235 --> 00:02:57,795 We want to make our classes easy to use. 49 00:02:57,795 --> 00:03:00,455 So when writing a class, we need to make sure 50 00:03:00,455 --> 00:03:04,915 that we're exposing only what's absolutely necessary to use the class. 51 00:03:04,915 --> 00:03:07,835 This will help people to know how to use the class and 52 00:03:07,835 --> 00:03:10,065 help them avoid accidentally misusing it. 53 00:03:11,180 --> 00:03:14,760 Encapsulation is used to hide implementation details and 54 00:03:14,760 --> 00:03:18,010 restrict what users of objects can do. 55 00:03:18,010 --> 00:03:19,760 Back to our path example. 56 00:03:19,760 --> 00:03:22,720 Once a path is created, it shouldn't change. 57 00:03:22,720 --> 00:03:24,990 Paths in the real world don't just get up and move. 58 00:03:26,220 --> 00:03:30,800 Right now we have our path implemented as an array of map locations. 59 00:03:30,800 --> 00:03:35,150 The array class provides lots of methods for manipulating arrays. 60 00:03:35,150 --> 00:03:37,220 But we don't need all of that. 61 00:03:37,220 --> 00:03:42,320 We can restrict what can be done with our array of map locations by encapsulating it 62 00:03:42,320 --> 00:03:44,420 or wrapping it in a class, and 63 00:03:44,420 --> 00:03:48,520 then expose those small sort of operations that can be done with the path. 64 00:03:49,960 --> 00:03:53,110 Back in code, let's put together our path class. 65 00:03:53,110 --> 00:03:55,270 Let's start by writing the constructor. 66 00:03:55,270 --> 00:03:58,520 When writing the constructor, we need to think about the absolute 67 00:03:58,520 --> 00:04:03,090 bare minimum amount of information that every Path object needs. 68 00:04:03,090 --> 00:04:06,230 That becomes the parameters to the constructor. 69 00:04:06,230 --> 00:04:10,040 I'd say it's the locations on the map that the path uses. 70 00:04:10,040 --> 00:04:12,878 We can pass those in as an array of map locations. 71 00:04:16,008 --> 00:04:19,538 We'll store these locations as a field in the class, 72 00:04:19,538 --> 00:04:25,410 only this time we aren't going to let users of this class access them directly. 73 00:04:25,410 --> 00:04:29,070 So instead of typing public here like we've done with other fields, 74 00:04:29,070 --> 00:04:29,980 we'll type private. 75 00:04:31,300 --> 00:04:34,900 We're going to store this as a MapLocation array. 76 00:04:34,900 --> 00:04:35,895 And we'll call it _path. 77 00:04:38,130 --> 00:04:41,240 Notice that I prefixed the field name with an underscore. 78 00:04:41,240 --> 00:04:44,160 We only use it for the names of private fields. 79 00:04:44,160 --> 00:04:48,030 It isn't required, and you may see other code that doesn't have this underscore. 80 00:04:48,030 --> 00:04:50,650 This is a common convention in C# though. 81 00:04:50,650 --> 00:04:55,220 It's helpful for distinguishing between instance variables and method variables. 82 00:04:55,220 --> 00:04:59,300 If we didn't have this underscore, we'd either have to name these two variables 83 00:04:59,300 --> 00:05:04,050 differently, oop, put my semicolon there, or 84 00:05:04,050 --> 00:05:09,520 we'd have to type this.path = path. 85 00:05:09,520 --> 00:05:13,490 So this refers to the current object's .path, 86 00:05:13,490 --> 00:05:19,030 which is the current object's field path, and assigns 87 00:05:19,030 --> 00:05:23,630 the path variable that's being passed in to this.path. 88 00:05:23,630 --> 00:05:30,510 But because we're using the underscore, we'll just say _path equals path. 89 00:05:32,000 --> 00:05:33,140 And that gives us the same effect. 90 00:05:34,270 --> 00:05:37,730 You can decide whether or not you want to use this convention. 91 00:05:37,730 --> 00:05:41,480 When programming at a company, there will often be a document called a coding 92 00:05:41,480 --> 00:05:46,190 standard which specifies how to name different types of variables. 93 00:05:46,190 --> 00:05:49,850 That way, everyone working on the code uses the same conventions. 94 00:05:50,940 --> 00:05:53,880 We should also make sure that the path field isn't accidentally 95 00:05:53,880 --> 00:05:56,700 overwritten by code that's within the path class. 96 00:05:56,700 --> 00:05:58,510 So we'll type readonly here. 97 00:06:00,690 --> 00:06:04,820 The thing you should know about readonly is that it only prohibits 98 00:06:04,820 --> 00:06:07,590 overriding the field with a different value. 99 00:06:07,590 --> 00:06:11,600 It doesn't prohibit anyone from changing the individual items in the array. 100 00:06:12,610 --> 00:06:16,100 That's one of the reasons we're wrapping this array in a class in the first place. 101 00:06:17,220 --> 00:06:21,680 We've now used encapsulation to hide the array of map locations. 102 00:06:21,680 --> 00:06:25,705 By wrapping the array in a class and using private to restrict access, 103 00:06:25,705 --> 00:06:29,900 there's no way for users of this class to alter the path. 104 00:06:29,900 --> 00:06:33,750 In fact, users of this class can't do much of anything. 105 00:06:33,750 --> 00:06:38,580 Next we'll start to open it up by adding behaviors we need the path class to have.