1 00:00:00,400 --> 00:00:03,870 Let's take another look at the on map method we just wrote. 2 00:00:03,870 --> 00:00:07,860 The result of these condition checks is stored in the inBounds variable. 3 00:00:09,960 --> 00:00:12,880 Then the value stored in the inBounds variable is returned. 4 00:00:14,180 --> 00:00:17,230 So if point is found to be inside the boundaries of the map, 5 00:00:17,230 --> 00:00:19,790 then the inBounds variable will be true. 6 00:00:19,790 --> 00:00:23,110 And the value true will be returned from this method to the caller. 7 00:00:24,212 --> 00:00:27,840 Otherwise, if the point was found to be outside the boundaries of the map, 8 00:00:27,840 --> 00:00:30,260 this method will return false. 9 00:00:30,260 --> 00:00:32,790 I should mention that we can actually shorten this code a bit and 10 00:00:32,790 --> 00:00:36,010 avoid using the inBounds variable entirely. 11 00:00:36,010 --> 00:00:40,190 The way we can do this is to return the result of this expression right away. 12 00:00:40,190 --> 00:00:43,310 Instead of first storing it in a variable and then returning it. 13 00:00:44,560 --> 00:00:49,231 We can type return right here instead and delete the other return statement. 14 00:00:53,154 --> 00:00:54,260 See how that works? 15 00:00:55,410 --> 00:00:58,270 There's now one less variable in our code. 16 00:00:58,270 --> 00:01:01,000 Also stringing a bunch of operations together like this 17 00:01:01,000 --> 00:01:04,020 can quickly make the code difficult to understand. 18 00:01:04,020 --> 00:01:08,647 We can alleviate some of this by splitting the code up into multiple lines like so. 19 00:01:11,742 --> 00:01:15,000 I can even indent it to line up with the line above it. 20 00:01:15,000 --> 00:01:17,620 This is possible to do because the semicolon 21 00:01:17,620 --> 00:01:19,810 marks the end of the statement of code. 22 00:01:19,810 --> 00:01:22,856 So I can put this statement on as many lines as I want and 23 00:01:22,856 --> 00:01:25,725 C# still knows to treat it as a single statement. 24 00:01:30,584 --> 00:01:34,595 Deciding if and when to split statements into multiple lines really just 25 00:01:34,595 --> 00:01:39,080 depends on what you think makes the code more readable and clearer to the reader. 26 00:01:40,140 --> 00:01:44,080 I decided to put operations that deal with the width on one line and 27 00:01:44,080 --> 00:01:46,080 the height on the other line. 28 00:01:46,080 --> 00:01:49,090 Indenting so that things line up vertically can also help. 29 00:01:49,090 --> 00:01:52,940 All right, we've completed this method. 30 00:01:52,940 --> 00:01:56,710 Let's go back to the main method in the game class to try out our new method. 31 00:01:57,780 --> 00:01:59,760 Don't forget to save the map.cs file. 32 00:02:01,070 --> 00:02:04,781 We can delete this line and declare a new point that should be inside our map. 33 00:02:08,542 --> 00:02:12,300 Our map is eight units wide and five units high. 34 00:02:12,300 --> 00:02:15,161 So let's create a point that's in square 4, 2. 35 00:02:20,270 --> 00:02:24,424 Now let's call our on map method to determine if this point is on the map and 36 00:02:24,424 --> 00:02:27,310 assign the value returned to a variable. 37 00:02:27,310 --> 00:02:33,322 So I'll say bool isOnMap = map.OnMap and 38 00:02:33,322 --> 00:02:36,499 then pass it to point. 39 00:02:38,673 --> 00:02:43,730 Just for fun, let's call OnMap with a point that we know isn't on the map. 40 00:02:43,730 --> 00:02:46,140 Just to make sure that we get the right answer back. 41 00:02:46,140 --> 00:02:49,000 I'll overwrite the point variable we just created with the new point. 42 00:02:50,130 --> 00:02:52,750 I don't have to declare the variable again because it's 43 00:02:52,750 --> 00:02:54,920 already declared as a point up here. 44 00:02:56,270 --> 00:02:58,780 I just want to assign it a new value. 45 00:02:58,780 --> 00:03:01,528 The same goes for the isOnMap variable. 46 00:03:11,792 --> 00:03:14,743 I intentionally chose to make a point in square 8, 47 00:03:14,743 --> 00:03:18,050 5 in order to illustrate a common programming mistake. 48 00:03:19,090 --> 00:03:22,510 At first glance, one might think that this point is on the map. 49 00:03:22,510 --> 00:03:25,890 Because the map has a width of eight and a height of five. 50 00:03:25,890 --> 00:03:30,670 However, in programming, by convention we start counting from zero. 51 00:03:30,670 --> 00:03:36,850 This means that the point on the bottom left corner of the map is 0, 0. 52 00:03:36,850 --> 00:03:41,560 And the rightmost point has an x value of one less than the width. 53 00:03:42,572 --> 00:03:46,540 The topmost point has a y value of one less than the height. 54 00:03:47,660 --> 00:03:50,560 This is called zero base counting. 55 00:03:50,560 --> 00:03:52,510 This throws a lot of people off. 56 00:03:52,510 --> 00:03:55,090 Occasionally even experienced programmers get bitten 57 00:03:55,090 --> 00:03:57,020 by what's called an off by one error. 58 00:03:58,330 --> 00:04:01,780 Let's print out the value of isOnMap to the Console so 59 00:04:01,780 --> 00:04:04,660 we can see the result of calling OnMap. 60 00:04:04,660 --> 00:04:06,820 I'll add the system namespace up here and 61 00:04:06,820 --> 00:04:10,858 then we'll use Console.WriteLine to print the value of isOnMap. 62 00:04:10,858 --> 00:04:19,950 So type Console.WriteLine Is OnMap. 63 00:04:22,965 --> 00:04:27,240 Copy that and do it again down here. 64 00:04:28,360 --> 00:04:31,740 All right, when we run this we should see the word true 65 00:04:31,740 --> 00:04:35,010 followed by the word false printed to the Console. 66 00:04:35,010 --> 00:04:37,687 Let's open the Console and compile and run this code. 67 00:04:41,574 --> 00:04:42,730 Looks good. 68 00:04:42,730 --> 00:04:45,820 When we come back we'll learn more interesting things about methods.