Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,293 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.