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 Protocols in Swift Creating Flexible Objects Using Protocols Protocol Oriented Programming Part 2

Error: redeclaration

I keep on getting the error: invalid redeclaration of 'decreaseLife(by):'. I don't understand why, since it was originally declared within a protocol. Here is my code:

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

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

protocol Destructable { func decreaseLife(by factor: Int) }

protocol Player: Destructable { 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 { var life: Int = 2 var position: Point var strength: Int = 5 var range: Int = 2

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

func decreaseLife(by factor: Int) {
    life -= factor
}

//MARK: - Destructable
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 = Point(x: position.x, y: position.y + 1)
        case .down: position = Point(x: position.x, y: position.y - 1)
        case .right: position = Point(x: position.x + 1, y: position.y)
        case .left: position = Point(x: position.x - 1, y: position.y)

    }
}

}

// 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

Chris Stromberg
PLUS
Chris Stromberg
Courses Plus Student 13,389 Points

You have declared this method twice with the same signature:

func decreaseLife(by factor: Int) {
    life -= factor
}

//MARK: - Destructable
func decreaseLife(by factor: Int) {
    life -= factor
}

You can't do that. Delete one, change the func name, or change the parameter name to fix the problem.

Otto linden
Otto linden
5,857 Points

To make Charlotte Dorn feel better he made the mistake in the video, but he corrected him self!