-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmap.py
More file actions
103 lines (81 loc) · 2.89 KB
/
map.py
File metadata and controls
103 lines (81 loc) · 2.89 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
import math
import heapq
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from time import time
from PIL import Image, ImageDraw
from heapq import heappop, heappush
class Map:
def __init__(self, manhattan=False):
'''
Default constructor
'''
self.manhattan = manhattan
self._width = 0
self._height = 0
self._cells = []
def read_from_string(self, cell_str, width, height):
'''
Converting a string (with '#' representing obstacles and '.' representing free cells) to a grid
'''
self._width = width
self._height = height
self._cells = [[0 for _ in range(width)] for _ in range(height)]
cell_lines = cell_str.split("\n")
i = 0
j = 0
for l in cell_lines:
if len(l) != 0:
j = 0
for c in l:
if c == '.':
self._cells[i][j] = 0
elif c == '#':
self._cells[i][j] = 1
else:
continue
j += 1
if j != width:
raise Exception("Size Error. Map width = ", j, ", but must be", width )
i += 1
if i != height:
raise Exception("Size Error. Map height = ", i, ", but must be", height )
def set_grid_cells(self, width, height, grid_cells):
'''
Initialization of map by list of cells.
'''
self._width = width
self._height = height
self._cells = grid_cells
def in_bounds(self, i, j):
'''
Check if the cell is on a grid.
'''
return (0 <= j < self._width) and (0 <= i < self._height)
def traversable(self, i, j):
'''
Check if the cell is not an obstacle.
'''
return not self._cells[i][j]
def get_neighbors(self, i, j):
'''
Get a list of neighbouring cells as (i,j) tuples.
It's assumed that grid is 4-connected (i.e. only moves into cardinal directions are allowed)
'''
neighbors = []
delta = [[0, 1], [1, 0], [0, -1], [-1, 0]]
# neighbors_2 = []
# delta_2 = [[1, 1], [1, -1], [-1, -1], [-1, 1]]
for d in delta:
if self.in_bounds(i + d[0], j + d[1]) and self.traversable(i + d[0], j + d[1]):
neighbors.append((i + d[0], j + d[1]))
# for d in delta_2:
# if self.in_bounds(i + d[0], j + d[1]) and self.traversable(i + d[0], j + d[1]) and (i + d[0], j) in neighbors and (i, j + d[1]) in neighbors:
# neighbors_2.append((i + d[0], j + d[1]))
neighbors.append((i, j))
# return neighbors + neighbors_2
return neighbors
def get_size(self):
return (self._height, self._width)