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

iOS Object-Oriented Swift 2.0 Classes Helper Methods

Going a bit nuts here...

Whats wrong with my code? I keep getting this error:

Type '(Int) -> [Point]' does not conform to protocol 'Sequence'

on this line of code:

for point in availablePositions {

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)
coordinatePoint.surroundingPoints()

let coordinatePoint2 = Point(x: 3, y: 3)

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 strength: Int = 1

    init (x: Int, y: Int) {
        self.position = Point(x: x, y: y)


    func fireAtEnemy (enemy: Enemy) {
        if inRange(position: self.position, range: self.range, target: enemy.position) {
            while enemy.life > 0 {
                enemy.decreaseHealth(factor: self.strength)
                print("Enemy Vanquished!")

                }
            } else {
                print("Darn!! Missed")

            }
        }

        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

    }
}

let tower = Tower(x: 0, y: 0)
let enemy = Enemy(x: 1, y: 1)

tower.fireAtEnemy(enemy)

1 Answer

try replacing your code with this:

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 = 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(initialPosition: self.position, range: self.range, target: enemy.position) {
        while enemy.life > 0 {
            enemy.decreaseHealth(factor: self.strength)
            print("Enemy vanquished!")
        }
    } else {
        print("Darn! The enemy is out of range!")
    }

}

func inRange(initialPosition: 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

}


}