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 trialSaleh Bubishate
1,613 Pointsi got confused with this challenge
How can I increase / decrease x or y coordinates by 1
class Point {
var x: Int
var y: Int
init(x: Int, y: Int) {
self.x = x
self.y = y
}
}
enum Direction {
case left
case right
case up
case down
}
class Robot {
var location: Point
init() {
self.location = Point(x: 0, y: 0)
}
func move(_ direction: Direction) -> Point {
switch direction {
case Direction.left : return Point(x: -1, y: 0 )
case Direction.right : return Point (x: 1, y: 0 )
case Direction.up : return Point(x: 0, y: 1)
case Direction.down : return Point(x: 0, y: -1)
}
}
}
2 Answers
Magnus Hållberg
17,232 PointsYou are almost there. To increase or decrease these values you use the "location property" with dot notation. Location is an instance of Point witch holds the coordinate values. Like this:
switch direction {
case Direction.left : return location.x -= 1
case Direction.right : return location.x += 1
case Direction.up : return location.y += 1
case Direction.down : return location.y -= 1
}
Saleh Bubishate
1,613 Pointsbut how can we return a value without typing a "->" in the instance ?
Magnus Hållberg
17,232 PointsI'm not sure I understand the question. The method is returning a value of point through the switch, just as specified in the method declaration.
Saleh Bubishate
1,613 PointsSaleh Bubishate
1,613 Pointsthanks a lot