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
Kjetil Korsveien
1,713 PointsInfinite loop in Tower video.
The code in the Tower - video example s working, but gives me an infinite loop of "Enemy vanquished!" Is this correct?
3 Answers
Martin Wildfeuer
Courses Plus Student 11,071 PointsAs far as it concerns the infinite loop, the problem is the decreaseHealth method:
class Enemy {
//...
func decreaseHealth(factor: Int) {
// Was: life -= life - factor
// You can either write life = life - factor or
life -= factor
}
}
life -= life - factor: If the factor was more than the remaining life, life - factor turned negative. Consequently, the enemies life was increased instead of decreased. That solves your infinite loop problem, not sure if this turns out as you wanted neither if the rest works as expected.
Hope that helps :)
Kjetil Korsveien
1,713 PointsHere is the code, FYI I've commented out to stop the looping:
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)
coordinatePoint.surroundingPoints()
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 -= life - factor
}
}
class Tower {
let position: Point
var range: Int = 1
var strenght: 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.strenght)
print("Enemy vanquished!")
}
} else {
print("Darn! The enemy is out of range!")
}
}
func inRange(position: Point, range: Int, target: Point) -> Bool {
let availablePositions = position.surroundingPoints(withRange: range)
for point in availablePositions {
if (point.x == target.x) && (point.y == target.y) {
return true
}
}
return false
}
}
// Inheretance
class SuperEnemy: Enemy {
let isSuper: Bool = true
override init(x: Int, y: Int) {
super.init(x: x, y: y)
self.life = 50
}
}
class LaserTower: Tower {
override init(x: Int, y: Int) {
super.init(x: x, y: y)
self.range = 100
self.strenght = 100
}
override func fireAtEnemy(enemy: Enemy) {
while enemy.life >= 0 {
enemy.decreaseHealth(strenght)
}
print("Enemy vanquished!")
}
}
let tower = Tower(x: 0, y: 0)
let enemy1 = Enemy(x: 1, y: 1)
//let superEnemy = SuperEnemy(x: 20, y: 20)
//let laserTower = LaserTower(x: 0, y: 0)
//tower.fireAtEnemy(enemy1)
Kjetil Korsveien
1,713 PointsHow do I get my code in one block?
Alan Guilfoyle
10,875 Pointsuse ` character three times which is located below the escape key. Add three before the code and three after the code to close it
Bill Merickel
11,420 PointsCan you show me your code? Thanks!
Alan Guilfoyle
10,875 PointsAlan Guilfoyle
10,875 PointsI'm having trouble finding the video. Would you mind posting a link to the video?