# Ogres mined the field to protect their Chieftain.
# But we can use the "domino" effect get our target.
# The scout has prepared the map of the minefield.
# All mines are placed the same distance apart.
# The map is an array of strings, where "x" is a mine and "." is nothing.
# The first row in the array is the row nearest to the hero.
# The map and helpful constants are listed below.
fieldMap = hero.findFriends()[0].getMap()
mine = "x"
empty = "."
mineDistance = 5
firstXPos = 15
firstYPos = 40
# Find which starting mine connects to the ogre Chieftain.
def findAdjacentMines(row, column):
return [{'y': row + 1, 'x': column, "type": fieldMap[row + 1][column]}, {'y': row - 1, 'x': column, "type": fieldMap[row - 1][column]}, {'y': row, 'x': column + 1, "type": fieldMap[row][column + 1]}, {'y': row, 'x': column - 1, "type": fieldMap[row][column - 1]}]
mines = [{'y': 15, 'x': 13}]
result = -1
while True:
storage = []
for i in range(len(mines)):
mine = mines[i]
adjacentMines = findAdjacentMines(mine.y, mine.x)
for adjacentMine in adjacentMines:
if adjacentMine.type == mine:
if adjacentMine.y < 3:
result = adjacentMine.x
fieldMap[mine.y][mine.x] = empty
storage.append({'y': adjacentMine.y, 'x': adjacentMine.x})
mines = []
mines = storage
if result > 0:
break
resultColumn = result # ∆ Change this to your actual result!
hero.say("I think it's column number: " + resultColumn)
resultX = resultColumn * mineDistance + firstXPos
hero.moveXY(resultX, hero.pos.y)
hero.moveXY(resultX, firstYPos)
the code keeps saying error message:
Looks like you are trying to go outside of the range allowed. The fieldMap has a set amount of rows and columns, but you keep looking for more. Add a check before the return line to make sure the rows and columns don’t try to go outside of the range.
# Ogres mined the field to protect their Chieftain.
# But we can use the "domino" effect get our target.
# The scout has prepared the map of the minefield.
# All mines are placed the same distance apart.
# The map is an array of strings, where "x" is a mine and "." is nothing.
# The first row in the array is the row nearest to the hero.
# The map and helpful constants are listed below.
fieldMap = hero.findFriends()[0].getMap()
mine = "x"
empty = "."
mineDistance = 5
firstXPos = 15
firstYPos = 40
# Find which starting mine connects to the ogre Chieftain.
def findAdjacentMines(row, column):
return [{'y': row + 1, 'x': column, "type": fieldMap[row + 1][column]}, {'y': row - 1, 'x': column, "type": fieldMap[row - 1][column]}, {'y': row, 'x': column + 1, "type": fieldMap[row][column + 1]}, {'y': row, 'x': column - 1, "type": fieldMap[row][column - 1]}]
mines = [{'y': 14, 'x': 13}]
result = 0
while True:
storage = []
for i in range(len(mines)):
mine = mines[i]
adjacentMines = findAdjacentMines(mine.y, mine.x)
for adjacentMine in adjacentMines:
if adjacentMine.type == mine:
if adjacentMine.y == 0:
result = adjacentMine.x
storage.append({'y': adjacentMine.y, 'x': adjacentMine.x})
for i in range(len(mines)):
mine = mines[i]
fieldMap[mine.y][mine.x] = empty
mines = []
mines = storage
if result > 0:
break
resultColumn = result # ∆ Change this to your actual result!
hero.say("I think it's column number: " + resultColumn)
resultX = resultColumn * mineDistance + firstXPos
hero.moveXY(resultX, hero.pos.y)
hero.moveXY(resultX, firstYPos)
ok, so I've changed the code a bit but now it does nothing, the hero doesn't move
Made final touches
# Ogres mined the field to protect their Chieftain.
# But we can use the "domino" effect get our target.
# The scout has prepared the map of the minefield.
# All mines are placed the same distance apart.
# The map is an array of strings, where "x" is a mine and "." is nothing.
# The first row in the array is the row nearest to the hero.
# The map and helpful constants are listed below.
fieldMap = hero.findFriends()[0].getMap()
mine = "x"
empty = "."
mineDistance = 5
firstXPos = 15
firstYPos = 40
# Find which starting mine connects to the ogre Chieftain.
def findAdjacentMines(row, column):
return [{"y": row + 1, "x": column, "type": fieldMap[row + 1][column]}, {"y": row - 1, "x": column, "type": fieldMap[row - 1][column]}, {"y": row, "x": column + 1, "type": fieldMap[row][column + 1]}, {"y": row, "x": column - 1, "type": fieldMap[row][column - 1]}]
mines = [{"y": 0, "x": 1}, {"y": 0, "x": 5}, {"y": 0, "x": 9}, {"y": 0, "x": 13}, {"y": 0, "x": 17}, {"y": 0, "x": 21}, {"y": 0, "x": 25}]
result = 0
while True:
storage = []
for i in range(len(mines)):
mineY = mines[i].y
mineX = mines[i].x
adjacentMines = findAdjacentMines(mineY, mineX)
for adjacentMine in adjacentMines:
if adjacentMine.type == mine:
if adjacentMine.y == 15:
result = adjacentMine.x
storage.append({"y": adjacentMine.y, "x": adjacentMine.x})
for i in range(len(mines)):
mineY = mines[i].y
mineX = mines[i].x
fieldMap[mineY][mineX] = empty
mines = []
mines = storage
if result > 0:
return False
resultColumn = result # ∆ Change this to your actual result!
hero.say("I think it's column number: " + resultColumn)
resultX = resultColumn * mineDistance + firstXPos
hero.moveXY(resultX, hero.pos.y)
hero.moveXY(resultX, firstYPos)
except now it infinitely loops?
# Ogres mined the field to protect their Chieftain.
# But we can use the "domino" effect get our target.
# The scout has prepared the map of the minefield.
# All mines are placed the same distance apart.
# The map is an array of strings, where "x" is a mine and "." is nothing.
# The first row in the array is the row nearest to the hero.
# The map and helpful constants are listed below.
fieldMap = hero.findFriends()[0].getMap()
mine = "x"
empty = "."
mineDistance = 5
firstXPos = 15
firstYPos = 40
# Find which starting mine connects to the ogre Chieftain.
mines = [{"y": 15, "x": 13}]
result = 0
while True:
for i in range(len(mines)):
row = mines[i].y
column = mines[i].x
adjacentMines = []
if row < 15 and fieldMap[row + 1][column] == mine:
adjacentMines.append({"y": row + 1, "x": column})
if fieldMap[row - 1][column] == mine:
adjacentMines.append({"y": row - 1, "x": column})
if fieldMap[row][column + 1] == mine:
adjacentMines.append({"y": row, "x": column + 1})
if fieldMap[row][column - 1] == mine:
adjacentMines.append({"y": row, "x": column - 1})
for adjacentMine in adjacentMines:
if adjacentMine.y == 0:
result = adjacentMine.x
break
fieldMap[row][column] = empty
mines = []
mines = adjacentMines
if result > 0:
break
resultColumn = result # ∆ Change this to your actual result!
hero.say("I think it's column number: " + resultColumn)
resultX = resultColumn * mineDistance + firstXPos
hero.moveXY(resultX, hero.pos.y)
hero.moveXY(resultX, firstYPos)
now it doesn’t do anything?
like the code runs without error and there is no infinite loop but nothing happens, the hero just stays there
So I’ve tried lots of things but the code is either too slow or doesn’t work
K, so modified the code even more and found my mistakes.
But no matter what, the code is still too slow and displays an error.
Any way of making my code cleaner and faster?