Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Daniel W
Daniel W
1,341 Points

How to check if Tower is on the Path?

The Tower class has a private readonly MapLocation _location;

In the constructor where we specify a location, we would like a check to see if such a location is on the path.

What is the best way to do this? Is it viable to g

My initial thought was to rework the code

namespace TreehouseDefense 
{
  class Path
  {
    private readonly MapLocation[] _path; //Needs to be a property? 

into:

public MapLocation PathArray {get; private set;} //PathArray can't be named Path, can it?

and in the constructor

 public Path(MapLocation[] path)
    {
      PathArray = path;
    }

Then we could access the PathArray property from the Tower class, so that in the tower's constructor, we could loop through the PathArray to see if it's on there or not..

Is there an easier solution?

1 Answer

Steven Parker
Steven Parker
230,274 Points

If the path is guaranteed to be straight, you can check the bounds of the path without looping. But if the path could possibly wander around, then the loop is probably the best way to go.