-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_controller.py
More file actions
142 lines (124 loc) · 4.64 KB
/
bot_controller.py
File metadata and controls
142 lines (124 loc) · 4.64 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
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
__author__ = "Abhimanyu Dogra and Niharika Dutta"
#! /usr/bin/env python
from app import App
GRID_SIZE = 5
class BOT:
def __init__(self, grid, source, destination, current_direction):
self.direction = current_direction
self.GRID = grid
self.current_position = source
self.destination = destination
self.app = App(grid, source, destination)
def main(self):
#app = App(self.GRID)
#self.printGrid(GRID_SIZE)
while(self.current_position != self.destination):
next_x,next_y = self.app.next_step(self.current_position[0],self.current_position[1])
if(self.is_safe(next_x,next_y)):
print "Next x = %d, Next y = %d is safe" %(next_x,next_y)
self.move_straight(next_x,next_y)
else:
print "Encountered obstacle at Next x = %d, Next y = %d" %(next_x,next_y)
self.app.mark_obstacle(next_x,next_y)
#self.app.update_grid(self.current_position[0],self.current_position[1])
#import ipdb; ipdb.set_trace()
def printGrid(self,n):
for i in range(n):
for j in range(n):
print self.GRID[i][j],
print
print self.current_position
print self.destination
def is_safe(self,next_x,next_y):
print "current direction %s" %self.direction
if(self.current_position[0] < next_x):
self.turn_north()
elif(self.current_position[0] > next_x):
self.turn_south()
elif(self.current_position[1] < next_y):
self.turn_west()
else:
self.turn_east()
if(next_x == 2 and next_y == 1):
return False
if(self.GRID[next_x][next_y] == "obstacle"):
return False
else:
return True
def turn_south(self):
print "Moving south now"
if(self.direction == "north"):
self.turn_180_deg()
elif(self.direction == "east"):
self.turn_90_deg_clock_wise()
elif(self.direction == "west"):
self.turn_90_deg_anti_clock_wise()
else:
pass
self.direction = "south"
def turn_north(self):
print "Moving north now"
if(self.direction == "south"):
self.turn_180_deg()
elif(self.direction == "west"):
self.turn_90_deg_clock_wise()
elif(self.direction == "east"):
self.turn_90_deg_anti_clock_wise()
else:
pass
self.direction = "north"
def turn_east(self):
print "Moving east now"
if(self.direction == "west"):
self.turn_180_deg()
elif(self.direction == "south"):
self.turn_90_deg_anti_clock_wise()
elif(self.direction == "north"):
self.turn_90_deg_clock_wise()
else:
pass
self.direction = "east"
def turn_west(self):
print "Moving west now"
if(self.direction == "east"):
self.turn_180_deg()
elif(self.direction == "north"):
self.turn_90_deg_anti_clock_wise()
elif(self.direction == "south"):
self.turn_90_deg_clock_wise()
else:
pass
self.direction = "west"
def turn_180_deg(self):
print "Turning 180 degree"
self.turn_90_deg_clock_wise()
self.turn_90_deg_clock_wise()
def turn_90_deg_clock_wise(self):
print "Turning 90 degree clockwise"
def turn_90_deg_anti_clock_wise(self):
print "Turning 90 degree anticlockwise"
def move_straight(self,next_x,next_y):
print "Moving straight now"
self.app.moved(next_x, next_y)
self.current_position = (next_x, next_y)
def make_grid(n):
grid = [["free"]*n for __ in xrange(n)]
blocked = [(0, 3), (1, 3), (2, 2), (2, 3)]
preferred = [(0, 1), (0, 2), (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)] #(0, 0), removed
for b in blocked:
grid[b[0]][b[1]] = "obstacle"
for p in preferred:
grid[p[0]][p[1]] = "preferred"
return grid
if __name__ == '__main__':
n = input()
input_grid = [ [int(x) for x in raw_input().split()] for __ in xrange(n) ]
grid_values = ['obstacle', 'free', 'preferred']
grid = [ [grid_values[i] for i in row] for row in input_grid]
source = tuple(map(int, raw_input().split()))
destination = tuple(map(int, raw_input().split()))
grid[source[0]][source[1]] = 'free'
cur_dir = raw_input()
bot = BOT(grid, source, destination, cur_dir)
bot.printGrid(n)
bot.main()