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

Why are we creating instances of Point in the switch statement instead of using position.x += 1 or position.y -= 1?

why doesn't this work?:

func move(_ direction: Direction, by distance: Int) {
        switch direction{
        case .up: position.y += 1
        case .down: position.y -= 1
        case .left: position.x -= 1
        case .right: position.x += 1
        }
    }
Simon Di Giovanni
Simon Di Giovanni
8,429 Points

Could you add the rest of the code? It’s a bit hard to establish what you’re trying to achieve without the right context

Simon Di Giovanni heres the rest of the code:

enum Direction{
    case up, down, left, right
}

protocol Moveable{
    func move(_ direction: Direction, by distance: Int)
}

protocol Destrucable {
    func decreaseLife(by factor: Int)
}
protocol Player: Destrucable {
    var position: Point { get set }
    var life: Int { get set}

    init(x: Int, y: Int)
}
protocol Attacker{
    var strength: Int { get }
    var range: Int { get }

    func attack(player: Player)
}

struct Point {
    let x: Int
    let y: Int
    init(x: Int, y: Int) {
        self.x = x
        self.y = y
    }
    /// Returns the surrounding points in range of
    /// the current one
    func points(inRange range: Int = 1) -> [Point] {
        var results = [Point]()
        let lowerBoundOfXRange = x - range
        let upperBoundOfXRange = x + range
        let lowerBoundOfYRange = y - range
        let upperBoundOfYRange = y + range
        for xCoordinate in lowerBoundOfXRange...upperBoundOfXRange {
            for yCoordinate in lowerBoundOfYRange...upperBoundOfYRange {
                let coordinatePoint = Point(x: xCoordinate, y: yCoordinate)
                results.append(coordinatePoint)
            }
        }
        return results
    }
}
// Enemy
class Enemy: Player, Attacker, Moveable {
    var life: Int = 2
    var  position: Point
    var strength: Int = 1
    var range: Int = 3
    required init(x: Int, y: Int) {
        self.position = Point(x: x, y: y)
    }
    func decreaseLife(by factor: Int) {
        life -= factor
    }

    func attack(player: Player) {
        player.decreaseLife(by: strength)
    }
    func move(_ direction: Direction, by distance: Int) {
        switch direction{
        case .up: position.y += 1
        case .down: position.y -= 1
        case .left: position.x -= 1
        case .right: position.x += 1
        }
    }
}
// Tower
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 fire(at enemy: Enemy) {
        if isInRange(of: enemy) {
            enemy.decreaseLife(by: strength)
            print("Gotcha")
        } else {
            print("Darn! Out of range!")
        }
    }
    func isInRange(of enemy: Enemy) -> Bool {
        let availablePositions = position.points(inRange: range)
        for point in availablePositions {
            if point.x == enemy.position.x && point.y == enemy.position.y {
                return true
            }
        }
        return false
    }
}

1 Answer

Simon Di Giovanni
Simon Di Giovanni
8,429 Points

Ok so the reason that your code doesn't compile, is that in the struct 'Point' ( the original definition), both x and y have been defined as 'let' (constants)

You have set position to be 'var', however this means that position can just be another instance of Point.

X and Y cannot be changed in an instance of 'Point' because they are LET

To fix this, change 'let' to 'var' in the struct, and the code compiles.