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
Nils Garland
18,416 Points[SOLVED MYSELF]Something wrong with the so called map in the helper methods video
Okay so everything in my code works. And I was pleased when it all worked out and I felt happy about it. This was the example in the helper methods video in the swift object course. But there is a problem. The map of the game has different coordinates and the bottom left of the map should have the x and y value of 0. But it seems as though it is the top right corner? Because to get the Enemy in range (you sould understand when you see my code) is at x: -1 and y: -1. But, in the video it's x: 1, y: 1. What have I done wrong? It might be a small error but I can't find it ;(
Note: The error should be somewhere around the the "surroundingPoints()" method in the Point struct.
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)...(y-range) {
for yCoord in (y-range)...(y+range) {
let coordinatePoint = Point(x: xCoord, y: yCoord)
results.append (coordinatePoint)
}
}
return results
}
}
class Enemy{
var life: Int = 2
var position: Point
init(x: Int, y: Int){
self.position = Point(x: x, y: y)
}
func decreaseHealth(factor: Int) -> Void{
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) -> Void {
if inRange(self.position, range: self.range, target: enemy.position) {
while enemy.life > 0 {
enemy.decreaseHealth(self.strength)
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
}
}
let tower = Tower(x: 0, y: 0)
let enemy = Enemy(x: -1, y: -1)
Thank you for taking the long time to read through all of this! :D