从本章开始之后,我们代码中的所有提示全部都为英语翻译,为什么呢?因为我发现到了这种高级关卡后,中文翻译后的很多提示内容转换为中文后都存在异议,这样可能会干扰我们的思路!!!!!
snowdrops 雪滴
# We need to clear the forest of traps!
# The scout prepared a map of the forest.
# But be careful where you shoot! Don't start a fire.
# Get the map of the forest.
forestMap = hero.findNearest(hero.findFriends()).forestMap
# The map is a 2D array where 0 is a trap.
# The first sure shot.
hero.say("Row " + 0 + " Column " + 1 + " Fire!")
#hero.say("Row " + 2 + " Column " + 2 + " Fire!")
# But for the next points, check before shooting.
# There are an array of points to check.
cells = [{"row": 0, "col": 4}, {"row": 1, "col": 0}, {"row": 1, "col": 2}, {"row": 1, "col": 4},
{"row": 2, "col": 1}, {"row": 2, "col": 3}, {"row": 2, "col": 5}, {"row": 3, "col": 0},
{"row": 3, "col": 2}, {"row": 3, "col": 4}, {"row": 4, "col": 1}, {"row": 4, "col": 2},
{"row": 4, "col": 3}, {"row": 5, "col": 0}, {"row": 5, "col": 3}, {"row": 5, "col": 5},
{"row": 6, "col": 1}, {"row": 6, "col": 3}, {"row": 6, "col": 4}, {"row": 7, "col": 0}];
for cell in cells:
row = cell["row"]
col = cell["col"]
# If row is less than forestMap length:
if row < forestMap.length:
# If col is less than forestMap[row] length:
if col < forestMap[row].length:
# Now, we know the cell exists.
# If it is 0, say where to shoot:
if forestMap[row][col] == 0:
hero.say("Row " + row + " Column " + col + " Fire!")
Subarray Retreat 退寻子数组
# Find the subarray of gems with the best summary value.
# This function solves the problem in linear time.
def dynamicMaxSum (gems):
cycles = 0
maxStartIndex = 0
maxEndIndex = 0
maxEndHere = 0
currentStartIndex = 0
maxBest = 0
for i in range(len(gems)):
cycles += 1
maxEndHere += gems[i].value
if maxEndHere < 0:
maxEndHere = 0
currentStartIndex = i + 1
if maxEndHere > maxBest:
maxStartIndex = currentStartIndex
maxEndIndex = i
maxBest = maxEndHere
hero.say("I's taken " + cycles + " cycles.")
return [maxStartIndex, maxEndIndex]
# This function solves the problem in quadratic time.
def naiveMaxSum(gems):
cycles = 0
maxStartIndex = 0
maxEndIndex = 0
maxBest = 0
for i in range(len(gems)):
sum = 0
for j in range(i, len(gems)):
cycles += 1
if cycles > 104:
hero.say("I fed up of calculations.")
return [i, j]
sum += gems[j].value
if sum > maxBest:
maxStartIndex = i
maxEndIndex = j
maxBest = sum
hero.say("I's taken " + cycles + " cycles.")
return [maxStartIndex, maxEndIndex]
# Don't worry "findItems" sort out gems by X coordinate.
items = hero.findItems()
# ∆: Maybe we should change this function?
edges = dynamicMaxSum(items)
x1 = edges[0] * 4 + 4
x2 = edges[1] * 4 + 4
# Collect gems from x1 to x2 and escape:
for x in range(x1, x2):
hero.moveXY(x, 41)
hero.moveXY(44, 64)
知识扩充:
用法如: for i in range (1,3)。语法格式:range(start, stop[, step]),分别是起始、终止和步长。如果step不设置,默认为1。
Northwest 西北(我觉得转义为一路向西是不是更贴合各位童鞋的心意呢VV)
# Your pet should find and then bring the potion to the hero.
# This function checks if the word is in the text.
def wordInText(text, word):
# Iterate through each character in the text.
for i in range(len(text) - len(word) + 1):
# For each of them loop through each character in word.
for j in range(len(word)):
# Store the shifted index i + j.
shiftedIndex = i + j
# If a character within the shifted index.
# isn't equal to the character in word at the index "j"
if text[shiftedIndex] != word[j]:
# Break the loop.
break
# If j is equal to the index of the last letter in word
if j == len(word) -1:
# Then the entire word is in the text.
# Return True.
return True
# The word was not found in text. Return False.
return False
# Follow the guides directions where to run.
def onHear(event):
# If "west" is in the phrase, the pet should run left.
if wordInText(event.message, "west"):
pet.moveXY(pet.pos.x - 28, pet.pos.y)
# If "north" is in the phrase, the pet should run up.
elif wordInText(event.message, "north"):
pet.moveXY(pet.pos.x, pet.pos.y + 24)
# Else the pet should try to fetch the potion.
else:
potion = pet.findNearestByType("potion")
if potion:
pet.fetch(potion)
pet.on("hear", onHear)
Triage 急诊
# Triage the wounded soldiers.
doctor = hero.findByType("paladin")[0]
mage = hero.findByType("pixie")[0]
helper = hero.findByType("peasant")[0]
soldiers = hero.findByType("soldier")
# Initialize patient arrays.
doctorPatients = []
magePatients = []
helperPatients = []
# Iterate all the soldiers:
for soldier in soldiers:
# If soldier is slowed:
if soldier.maxSpeed < 6:
# Add them to the 'mage's array of patients.
magePatients.append(soldier)
# Else if soldier.health is less than half of maxHealth:
elif soldier.health < soldier.maxHealth / 2:
# Add them to the 'doctor's array of patients.
doctorPatients.append(soldier)
# Else:
else:
# Add soldier to the 'helper's array of patients.
helperPatients.append(soldier)
# Now assign the patient lists to the appropriate person.
mage["patients"] = magePatients
doctor["patients"] = doctorPatients
helper["patients"] = helperPatients
Echo of War 战争的回声
# Destroy 5 robobombs. Some of them are old and safe.
# Old (safe) bombs have the certain letter in their id.
# This function checks if searchLetter is in searchWord.
def isLetterInWord(searchWord, searchLetter):
# Complete the function.
index = len(searchWord) - len(searchLetter)
for i in range(index + 1):
for j in range(len(searchLetter)):
wordIndex = i + j
if searchWord[wordIndex] != searchLetter[j]:
break
if j == len(searchLetter) - 1:
return True
return False
# The engineer knows how the old robots are marked.
engineer = hero.findFriends()[0]
safeLetter = engineer.safeLetter
enemies = hero.findEnemies()
for index in range(len(enemies)):
enemy = enemies[index]
if isLetterInWord(enemy.id, safeLetter):
# Destroy the enemy if it's safe.
while enemy.health > 0:
hero.attack(enemy)