Hello, here comes some errors about types during I write the code. I have been searching for the solution for a few months, but still couldnβt get the solutionπ. Here is my code:
exitPosition = {βxβ: 150, βyβ: 120}
distanceBetweenRooms = 16
zeroShift = {βxβ: 10, βyβ: 10}
inSearch = True
width = 19
height = 15
def coorConvert(i, dist):
return i * distanceBetweenRooms / 2 + zeroShift.x
wall = βXβ
unknown = β?β
empty = β.β
maze =
for i in range(height):
maze.append([wall])
for i in range(height):
if i % 2:
for j in range(width - 1):
maze[i].append(unknown)
else:
for j in range(width - 1):
if j % 2:
maze[i].append(wall)
else:
maze[i].append(unknown)
def checkPaths(cMap):
cMap[hero.row][hero.col] = β.β
dirs = [[1, 0, north], [-1, 0, south], [0, 1, west], [0, -1, east]]
for d in range(len(dirs)):
dx = dirs[d][0]
dy = dirs[d][1]
dPosFunc = dirs[d][2]
if (cMap[hero.row+dx][hero.col+dy] == β?β
and hero.isPathClear(hero.pos, dPosFunc())):
cMap[hero.row+dx][hero.col+dy] = β.β
return cMap
def findPath(m, needExit):
stack = [[hero.row, hero.col, ββ]]
visited =
index = 0
while len(stack):
current = stack[index]
index += 1
row = current[0]
col = current[1]
path = current[2]
data = m[row-1][col] + " "
strPos = row + β-β + col
if visited.indexOf(strPos) != 1:
continue
visited.append(strPos)
if not needExit and m[row][col] == β?β:
return path
if needExit and row == exitRow and col == exitCol:
return path + β!β
if m[row-1][col] == β.β:
stack.append([row-2, col, path + βSβ])
if m[row+1][col] == β.β:
stack.append([row+2, col, path + βNβ])
if m[row][col+1] == β.β:
stack.append([row, col + 2, path + βWβ])
if m[row][col-1] == β.β:
stack.append([row, col - 2, path + βEβ])
for p in range(len(path)):
moveDir(path[p])
def moveDir(direction):
newPosFunc = {
βNβ : north,
βSβ : south,
βEβ : east,
βWβ : west,
β!β : exit
}[direction]
newPos = newPosFunc()
hero.moveXY(newPos.x, newPos.y)
if direction == β!β:
inSearch = False
hero.row = newPos.row
hero.col = newPos.col
def east():
return {βxβ : coorConvert(hero.col - 2),
βyβ : coorConvert(hero.row),
βrowβ : hero.row, βcolβ : hero.col - 2}
def west():
return {βxβ : coorConvert(hero.col + 2),
βyβ : coorConvert(hero.row),
βrowβ : hero.row, βcolβ : hero.col + 2}
def south():
return {βxβ : coorConvert(hero.col),
βyβ : coorConvert(hero.row - 2),
βrowβ : hero.row - 2, βcolβ : hero.col}
def north():
return {βxβ : coorConvert(hero.col),
βyβ : coorConvert(hero.row + 2),
βrowβ : hero.row + 2, βcolβ : hero.col}
def exit():
return exitPosition
path = ββ
treasureFound = False
while inSearch:
maze = checkPaths(maze)
if hero.gold > 0:
treasureFound = True
inSearch = False
path = findPath(maze, treasureFound)
if not path:
pass
else:
for p in range(len(path)):
moveDir(path[p])