-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robot.py
164 lines (137 loc) · 4.11 KB
/
Robot.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 17 12:37:06 2018
"""
import numpy as np
import pandas as pd
import sys
import os
class Robot():
def __init__(self,x,y,dir):
self.x = x
self.y = y
self.dir = dir
def place(self,x,y,dir):
if x in range(0,5) and y in range(0,5):
self.x = x
self.y = y
self.dir = dir
else:
print("Robot is not placed in the right bounds of the table")
def move(self):
if self.dir == 'NORTH':
if self.y in range(0,4):
self.y += 1
else:
print("Robot will fall off the table on this move")
elif self.dir == 'SOUTH':
if self.y in range(1,5):
self.y -= 1
else:
print("Robot will fall off the table on this move")
elif self.dir == 'EAST':
if self.x in range(0,4):
self.x += 1
else:
print("Robot will fall off the table on this move")
elif self.dir == 'WEST':
if self.x in range(1,5):
self.x -= 1
else:
print("Robot will fall off the table on this move")
def left(self):
if self.dir == 'NORTH':
self.dir = 'WEST'
elif self.dir == 'SOUTH':
self.dir = 'EAST'
elif self.dir == 'WEST':
self.dir = 'SOUTH'
elif self.dir == 'EAST':
self.dir = 'NORTH'
def right(self):
if self.dir == 'NORTH':
self.dir = 'EAST'
elif self.dir == 'SOUTH':
self.dir = 'WEST'
elif self.dir == 'WEST':
self.dir = 'NORTH'
elif self.dir == 'EAST':
self.dir = 'SOUTH'
def report(self):
print(self.x,self.y,self.dir)
return {'x':self.x, 'y':self.y, 'dir': self.dir}
'''
robot = Robot(0,0,'NORTH')
current_position = robot.report()
robot.move()
current_position = robot.report()
robot.left()
current_position = robot.report()
robot.move()
current_position = robot.report()
robot.right()
current_position = robot.report()
robot.move()
current_position = robot.report()
robot.place(-1,-1,'WEST')
current_position = robot.report()
robot.right()
current_position = robot.report()'''
'''
commands = open("commands.txt","r")
for line in commands.readlines():
if line == 'REPORT':
print('1')
current_position = robot.report()
elif line == 'MOVE':
print('2')
robot.move()
elif line == 'RIGHT':
print('3')
robot.right()
elif line == 'LEFT':
print('4')
robot.left()
robot.report()'''
'''
def main():
filepath = sys.argv[1]
if not os.path.isfile(filepath):
print("File path {} does not exist. Exiting...".format(filepath))
sys.exit()
robot = Robot(0,0,'NORTH')
current_position = robot.report()
with open(filepath) as fp:
for line in enumerate(fp):
if line[1].rstrip() == 'REPORT':
current_position = robot.report()
elif line[1].rstrip() == 'MOVE':
robot.move()
elif line[1].rstrip() == 'RIGHT':
robot.right()
elif line[1].rstrip() == 'LEFT':
robot.left()
elif line[1].rstrip().find("PLACE"):
print(line)
if __name__ == '__main__':
main()
'''
'''
robot = Robot(0,0,'NORTH')
filepath = 'commands.txt'
with open(filepath) as fp:
for line in enumerate(fp):
if line[1].rstrip() == 'REPORT':
current_position = robot.report()
elif line[1].rstrip() == 'MOVE':
robot.move()
elif line[1].rstrip() == 'RIGHT':
robot.right()
elif line[1].rstrip() == 'LEFT':
robot.left()
elif line[1].rstrip().split(' ')[0] == 'PLACE':
placebot = line[1].rstrip().split(' ')[1]
robot.place(int(placebot.split(',')[0]),int(placebot.split(',')[1]),
placebot.split(',')[2])
current_position = robot.report()
'''