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 trialJack Colosky
Courses Plus Student 2,263 PointsWhen I call the fireAtEnemy method on the instance tower, I get the error expected declaration
Here is my code : struct Point { let x:Int let y:Int
init (x: Int, y: Int) {
self.x = x
self.y = y
}
func surroundingPoints(withRange range: Int = 1) -> [Point] {
var results:[Point] = []
for xCoord in (x-range)...(x+range) {
for yCoord in (y-range)...(y+range) {
let coordinatePoint = Point(x: xCoord, y:yCoord)
results.append(coordinatePoint)
}
}
return results
}
}
let coordinatePoint = Point (x: 2, y: 2) // Point instance
coordinatePoint.surroundingPoints() // We call the instance coordinatePoint of the function surroundingPoint that gives us all of our values
class Enemy { var life: Int = 2 let position: Point
init(x: Int, y: Int) {
self.position = Point(x: x, y: y)
}
func decreaseHealth(factor: Int) {
life -= factor
}
class Tower { let position : Point var range : Int = 1 var strength : Int = 1
init(x: Int, y: Int) {
self.position = Point(x: x, y: y)
}
func fireAtEnemy(enemy: Enemy) {
if inRange(self.position, range: self.range, target: enemy.position) {
while enemy.life > 0 {
enemy.decreaseHealth(self.strength)
print("Enemy dead!")
}
} else {
print("Enemy is out of range")
}
}
func inRange(position: Point, range: Int, target: Point) -> Bool {
let availblePositions = position.surroundingPoints(withRange: range)
//This is an instance method
for point in availblePositions {
if (point.x == target.x) && (point.y == target.y) {
return true
}
}
return false
}
}
let tower = Tower(x: 0, y: 0) let enemy = Enemy(x: 1, y: 1)
tower.fireAtEnemy(enemy)
// This is the part that isn't working, pls help
1 Answer
Alia Khan
3,733 Pointslet tower = Tower(x: 0, y: 0)
let enemy = Enemy(x: 1, y: 1)
tower.fireAtEnemy(enemy: enemy)