Spaces in the map layout now mean "e" or "0" (context dependent). The testing maps have been changed to reflect this.
This commit is contained in:
parent
3dc9b3b2bb
commit
b5930957e5
5 changed files with 130 additions and 120 deletions
46
gamemap.py
46
gamemap.py
|
@ -137,7 +137,8 @@ class GameMap(object):
|
|||
# p: player start (up to one per level)
|
||||
|
||||
# regular expressions
|
||||
matrixRegex = re.compile(r'([ \t]*([a-z][0-9]*)+(\n))+')
|
||||
tileRegex = re.compile(r'([a-z ])([0-9]+|[ ])')
|
||||
matrixRegex = re.compile(r'(?:[ \t]*(?:[a-z ](?:[0-9]+|[ ]))+(\n))+')
|
||||
|
||||
def __init__(self, name, graph, matrix, dimensions):
|
||||
self.name = name
|
||||
|
@ -165,7 +166,8 @@ Entering a map through stdin will be obsolete once testing is over."""
|
|||
with open(infile, 'r') as f:
|
||||
data = f.read()
|
||||
except OSError as e:
|
||||
print("The file could not be read. Falling back to stdin...")
|
||||
print("The file could not be read.")
|
||||
return None
|
||||
else:
|
||||
|
||||
while tryToRead:
|
||||
|
@ -205,28 +207,36 @@ list of lists of tuples."""
|
|||
mat = [[]]
|
||||
x = 0
|
||||
y = 0
|
||||
i = 0
|
||||
l = 0
|
||||
while i < len(matrixStr):
|
||||
if matrixStr[i].isalpha():
|
||||
j = i+1
|
||||
while j < len(matrixStr) and matrixStr[j].isdecimal():
|
||||
j += 1
|
||||
if j == i+1: # no number
|
||||
mat[l].append((matrixStr[i], 0))
|
||||
else:
|
||||
mat[l].append((matrixStr[i], int(matrixStr[i+1:j])))
|
||||
i = j
|
||||
elif matrixStr[i] == '\n':
|
||||
while len(matrixStr) > 0:
|
||||
tile = GameMap.tileRegex.match(matrixStr)
|
||||
if tile != None:
|
||||
tileType = tile.group(1)
|
||||
tileNum = tile.group(2)
|
||||
if tileType == ' ':
|
||||
tileType = 'e'
|
||||
if tileNum == ' ':
|
||||
tileNum = '0'
|
||||
mat[l].append((tileType, int(tileNum)))
|
||||
#x += 1
|
||||
matrixStr = matrixStr[len(tile.group()):]
|
||||
elif matrixStr[0] == '\n':
|
||||
if x == 0:
|
||||
x = len(mat[l])
|
||||
elif x != len(mat[l]):
|
||||
raise RuntimeError('Map matrix has jagged edges.')
|
||||
raise RuntimeError("Map matrix has jagged edges.")
|
||||
l += 1
|
||||
i += 1
|
||||
#x = 0
|
||||
mat.append([])
|
||||
else: # assume it was a whitespace character.
|
||||
i += 1
|
||||
i = 1
|
||||
while i < len(matrixStr) and matrixStr[i] in ' \t\n':
|
||||
i += 1
|
||||
if i == len(matrixStr):
|
||||
matrixStr = ''
|
||||
else:
|
||||
matrixStr = matrixStr[i:]
|
||||
else: # This shouldn't happen, so if it does, there was an error.
|
||||
raise RuntimeError("Unexpected token in map matrix: '{0}'".format(matrixStr))
|
||||
y = len(mat) - 1
|
||||
|
||||
# Now for the graph
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue