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 trialRafik B
8,036 PointsCan you help me out with the for in loop??
I thought we were checking if the enemy is in range of the tower but doesn't this loop checks if the tower and enemy are standing on the same spot??
func isInRange(of enemy: Enemy) -> Bool{
let availablePosition = position.points(inRange: range)
for point in availablePosition{
if point.x == enemy.position.x && point.y == enemy.position.y{
return true
}
}
return false
}
}
4 Answers
Steven Deutsch
21,046 PointsHey Rafik B,
This function doesn't tell us that the enemy and tower are standing at the same position, it tells us whether or not an enemy is in range by returning a bool value.
If you look at the code below:
func isInRange(of enemy: Enemy) -> Bool{
// Here we have an array of points populated from a range, [Point]
// We'll call it availablePositions... note I made it plural since its a collection
let availablePositions = position.points(inRange: range)
// Here we iterate over the points that are in range and check if any match
// the position of the enemy
for point in availablePositions {
if point.x == enemy.position.x && point.y == enemy.position.y{
return true
}
}
return false
}
Good Luck
Andrew Shook
31,709 PointsI haven't watched this course, but my best guess would be that the tower and an enemy can never occupy the same position at the same time. Therefore, there is no need to check whether or not they are at the same position.
Herman Kwan
5,062 PointsI am having trouble on this problem as well. It iterates over the possible positions in Enemy from the points method which gives 9 possible positions which is where the tower is. In the isInRange method where we iterate each possible positions from availablePositions, it says "if point.x == enemy.position.x && point.y == enemy.position.y, return true. Doesn't that mean if the x position of Tower's location equal to x position of Enemy's location AND y position of Tower's location equal to y position of Enemy's location return true? It just seems like we're checking to see if Enemy is in the same location of Tower. I am stuck please help!
Jon Barnett
2,004 PointsWithin the loop, the iterator has been given the name point. I get confused by similar names that have little to do with each other sometimes, no criticism of the course intended, it's just me. In this case the loop iterator can be called anything eg-
'''
func isInRange(of enemy: Enemy) -> Bool {
let availablePositions = position.points(inRange: rangee)
for i in availablePositions {
if i.x == enemy.position.x && i.y == enemy.position.y {
return true
}
}
return false
}
}
'''
I'm not suggesting this is better, but it helps me to identify the iterator as just the iterator, and nuttn' to do with all the stuff named point Point points etc elsewhere. Similarly the range property in the Tower class is nuttn' to do with the range parameter to the points function in the Points struct. So I changed that as well. Once I'm happy I understand it, I'll probably change it back.
Rafik B
8,036 PointsRafik B
8,036 PointsThanks, i understand it now!!!