-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot_simulator_beta.py
100 lines (91 loc) · 3.16 KB
/
robot_simulator_beta.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 21 13:29:39 2016
@author: Ken
"""
#==============================================================================
# Write a robot simulator.
#
# A robot factory's test facility needs a program to verify robot movements.
# The robots have three possible movements:
# turn right
# turn left
# advance
#
# Robots are placed on a hypothetical infinite grid,
# facing a particular direction (north, east, south, or west)
# at a set of {x,y} coordinates, e.g., {3,8},
# with coordinates increasing to the north and east.
#
# The robot then receives a number of instructions, at which point
# the testing facility verifies the robot's new position,
# and in which direction it is pointing.
#
# The letter-string "RAALAL" means:
# Turn right
# Advance twice
# Turn left
# Advance once
# Turn left yet again
#
# Say a robot starts at {7, 3} facing north. Then running this stream of
# instructions should leave it at {9, 4} facing west.
#==============================================================================
bearing_dict = {'N' : 'NORTH',
'S' : 'SOUTH',
'W' : 'WEST',
'E' : 'EAST'}
class Robot(object):
def __init__(self, bearing='NORTH', x=0, y=0):
if bearing in bearing_dict.keys():
self.bearing = bearing_dict[bearing] # To handle NSWE inputs
else:
self.bearing = bearing.upper() # Will handle lowercase
self.x = x
self.y = y
self.coordinates = (self.x, self.y)
def advance(self):
if self.bearing == 'NORTH':
self.y += 1
self.coordinates = (self.x, self.y)
elif self.bearing == 'SOUTH':
self.y -= 1
self.coordinates = (self.x, self.y)
elif self.bearing == 'EAST':
self.x += 1
self.coordinates = (self.x, self.y)
elif self.bearing == 'WEST':
self.x -= 1
self.coordinates = (self.x, self.y)
else:
raise ValueError
def turn_left(self):
if self.bearing == 'NORTH':
self.bearing = 'WEST'
elif self.bearing == 'SOUTH':
self.bearing = 'EAST'
elif self.bearing == 'EAST':
self.bearing = 'NORTH'
elif self.bearing == 'WEST':
self.bearing = 'SOUTH'
else:
raise ValueError
def turn_right(self):
if self.bearing == 'NORTH':
self.bearing = 'EAST'
elif self.bearing == 'SOUTH':
self.bearing = 'WEST'
elif self.bearing == 'EAST':
self.bearing = 'SOUTH'
elif self.bearing == 'WEST':
self.bearing = 'NORTH'
else:
raise ValueError
def simulate(self, commands):
for char in commands:
if char == 'A':
self.advance()
elif char == 'L':
self.turn_left
elif char == 'R':
self.turn_right