「網易官方」極客戰記(codecombat)攻略-沙漠-潛伏

「網易官方」極客戰記(codecombat)攻略-沙漠-潛伏

殺掉犛牛群中的食人魔

簡介

「網易官方」極客戰記(codecombat)攻略-沙漠-潛伏

findEnemies() 給你一個包含你的英雄可以看到的所有敵人的數組:

enemies = hero.findEnemies()

循環所有敵人,並攻擊任何類型為 "shaman" 的人。

默認代碼

# 用findEnemies把敵人存在數組enemies中

# 只攻擊薩滿巫師,不要攻擊犛牛!

enemies = hero.findEnemies()

enemyIndex = 0

# 把這段代碼用一個while loop 功能循環遍歷所有的敵人

# 當 enemyIndex 小於 enemies 的長度時:

enemy = enemies[enemyIndex]

if enemy.type == 'shaman':

while enemy.health > 0:

hero.attack(enemy)

# 記得增加enemyIndex

概覽

既然你熟悉數組,你可以使用方法 findEnemies() 來獲得一個數組,包括你的英雄可以看到的所有敵人。

請注意,示例代碼使用另一個 “while” 循環來確保你的英雄在 health 大於 0 時持續攻擊 shaman。

敵人攻擊的算法邏輯已經提供給你,你需要把它放在一個 while 循環中,在那裡你可以遍歷 enemies找到所有的 "shaman" 。

while enemyIndex < len(enemies):

enemy = enemies[enemyIndex]

if enemy.type == 'shaman':

while enemy.health > 0:

hero.attack(enemy)

enemyIndex += 1

重點:確保每次循環運行時都增加 enemyIndex ,即使敵人不是巫師!

潛伏解法

# 用findEnemies把敵人存在數組enemies中

# 只攻擊薩滿巫師,不要攻擊犛牛!

enemies = hero.findEnemies()

enemyIndex = 0

# 把這段代碼用一個while loop 功能循環遍歷所有的敵人

# 當 enemyIndex 小於 enemies 的長度時:

while enemyIndex < len(enemies):

enemy = enemies[enemyIndex]

if enemy.type == 'shaman':

while enemy.health > 0:

hero.attack(enemy)

# 記得增加enemyIndex

enemyIndex += 1


本攻略發於極客戰記官方教學欄目,原文地址為:

https://codecombat.163.com/news/jikezhanji-qianfu

極客戰記——學編程,用玩的!


分享到:


相關文章: