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 trialSacha Karsenty
3,903 PointsWhy does it mean that the target position is in range? Sorry for the long explanation but I hope you guys can help me.
func inRange(position: Point, range: Int, target: Point) -> Bool { let availablePostions = position.surrondingPoints(withRange: range) for point in availablePostions { if (point.x == target.x) && (point.y == target.y) { return true } } return false }
so I understand position is the initial coordinates of the tower, range is just the range between initial position and a target on the map and finally target are the coordinates of the target.
Now for me the first contant created (availablePositions) creates an array of points containing ALL points around the tower at all ranges. So basically all the other points on the map except the initial position. Then the for in loop iterates over all the points in the arrays of points that are created with the availablePositions constant.
Now Pasan says if a point as the same x and y value than the target than the target is in range. For me that is wrong as my understanding of that would just mean that the target is on the map somewhere at ANY range since the points created in availablePositions are at all ranges. What am I missing?
Thank you and sorry for the wall of text....
2 Answers
Jens Hagfeldt
16,548 PointsHi Sacha
Remember that the towers range property was initialised with the value 1 as shown below?!
range: Int = 1
And then when you want to calculate if an enemy is in range of the tower you use the towers member function inRange().
There you call upon the towers own range by setting self.range as the value of the function parameter range:
Doin so also means that therefore you will only get points one step from the tower as targets not the whole map (but you do hit all the points one step arount the tower off course)
To sum it all up for you the target (the enemy) is in range only when it stands on one of the points one step from the tower.
Hope this explanation helped!
Happy coding / Jeppe_07
Sacha Karsenty
3,903 PointsHi Jens,
Thank you very much yeah it makes sense to me I think. The helper method inRange() could determine any points on the grid that are around a certain point it just depends on the range that is specified. But because we use self.range (which equals 1) in the fireAtEnemy instance method the specified range is 1 not anything else.
Thanks again.
Boris Likhobabin
3,581 PointsBoris Likhobabin
3,581 PointsHey, could you please explain why the position of the Tower say(0,0) is also included in the results of the surroundingPoints method. the point (0,0) is included in the results, however this point is ""surrounded"" by others. How would exclude this point from the results we get from calling this method?