From the course: Object-Oriented Programming with C#

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Calculating neighbors

Calculating neighbors

- [Instructor] Now that our room has a unique name, let's go through and do a little bit of math to calculate each of its neighbors. First, we're going to check that the column is less than the width minus one. That'll let us know that it's within the width of the grid. Next, we're going to set the neighbor to the east by calculating the rooms index of the current column plus one and the current row. We're going to do something similar for west, south, and north as well. Let's take a look. For the neighbor to the left, we're going to calculate the room index by subtracting one from the column. Now let's look at the south and north neighbors. In order to calculate the neighbor to the south, we want to make sure that the row is less than the height minus one and then we'll use the calculated room index with the current column and add one to the current row. And last, we can calculate the north neighbor. Now that we've…

Contents