-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze_parser.py
90 lines (72 loc) · 2.98 KB
/
maze_parser.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
from nodeClasses import GraphNode
from printAdjacency import printAdjacency
def readMaze(file, printMatrix = False):
avMoves = [ # (vertical, horizontal)
(0, 1), # Move to the right
(0, -1), # Move to the left
(1, 0), # Move up
(-1, 0) # Move down
]
adjList = []
posMatrix = []
nodeList = []
with open(file, "r") as f: # Read the file into lines
lines = f.readlines()
newlines = [] # The new lines
for line in lines:
newlines.append(line.replace('\n', '')) # Delete the new lines symbol
# Some uninteresting stuff
height = len(newlines)
#width = len(newlines[0])
# Fill up the matrix
for i in range(height):
posMatrix.append([])
# Creation of the actual list of faking adjacency list - reading each node without neighbours
for yindex, line in enumerate(newlines): # For each line in the maze
for xindex, char in enumerate(line): # For each position in each line
nodeInfo = None
isGoal = False # Keep register of if it's the goal node
isStart = False
if (char == 'B'):
isGoal = True
if (char == 'A'):
isStart = True
if (char == ' ' or char == 'A' or char == 'B'):
nodeInfo = GraphNode(len(nodeList) + 1, isGoal, isStart)
# TODO: Stuff the adjacency shit
# Add the created note into the matrix
posMatrix[yindex].append((nodeInfo.getNumber() if nodeInfo is not None else None))
if (nodeInfo is not None):
nodeList.append((nodeInfo, (yindex, xindex)))
# Reading the neighbours for each read node
for nodeInfo in nodeList:
neighbours = []
currentPos = nodeInfo[1]
# Check if there are any nodes on the neighbourhood
for i in range(0, len(avMoves)):
try:
rValue = posMatrix[currentPos[0] + avMoves[i][0]][currentPos[1] + avMoves[i][1]]
if (rValue is not None):
for el in nodeList:
if el[0].getNumber() == rValue:
neighbours.append(el[0])
except Exception:
continue
# Add the found neighbours into the node
node = nodeInfo[0]
for neigh in neighbours:
node.pushNeighbour(neigh)
# Add the node to the adjacency list
adjList.append(node)
"""
# Add the found neighbours into the adjacency list
newAdjacency = [nodeInfo[0]]
neighTuple = []
for el in neighbours:
neighTuple.append(el) # Add the found neighbour to the adjacent list
newAdjacency.append(neighTuple)
adjList.append(newAdjacency)
"""
if printMatrix:
printAdjacency(posMatrix)
return adjList