forked from exercism/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
53 lines (40 loc) · 1.25 KB
/
example.py
File metadata and controls
53 lines (40 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
NORTH, EAST, SOUTH, WEST = range(4)
class Compass:
compass = [NORTH, EAST, SOUTH, WEST]
def __init__(self, direction=NORTH):
self.direction = direction
def left(self):
self.direction = self.compass[self.direction - 1]
def right(self):
self.direction = self.compass[(self.direction + 1) % 4]
class Robot:
def __init__(self, direction=NORTH, x=0, y=0):
self.compass = Compass(direction)
self.x = x
self.y = y
def advance(self):
if self.direction == NORTH:
self.y += 1
elif self.direction == SOUTH:
self.y -= 1
elif self.direction == EAST:
self.x += 1
elif self.direction == WEST:
self.x -= 1
def turn_left(self):
self.compass.left()
def turn_right(self):
self.compass.right()
def move(self, commands):
instructions = {'A': self.advance,
'R': self.turn_right,
'L': self.turn_left}
for cmd in commands:
if cmd in instructions:
instructions[cmd]()
@property
def direction(self):
return self.compass.direction
@property
def coordinates(self):
return (self.x, self.y)