Created
October 21, 2016 10:21
-
-
Save mikintosh/f7dd6041df8f3e3dab38491cccbf6535 to your computer and use it in GitHub Desktop.
Returning a function (CodeCombat) / helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Your goal is to keep at least one flower alive for 60 seconds. | |
| // set 3 new locations | |
| var LOCATIONS = [ | |
| {x: 72, y: 44}, | |
| {x: 49, y: 59}, | |
| {x: 50, y: 25}, | |
| {x: 23, y: 43} | |
| ]; | |
| // loop through all locations | |
| // build a tower on each of them | |
| hero.findLocationsAndBuildTowers = function (nextTower) { | |
| for (var pos; pos = nextTower(); ) hero.buildXY("arrow-tower", pos.x, pos.y); | |
| }; | |
| // summon new soldiers if there is enough money | |
| hero.summonSoldiers = function() { | |
| if (hero.gold > hero.costOf("soldier")) { | |
| hero.summon("soldier"); | |
| } | |
| }; | |
| hero.commandSoldiers = function(nextSoldier, go) { | |
| for (var soldier; soldier = nextSoldier(); ) { | |
| if (!soldier.spot) hero.command(soldier, "defend", soldier.spot = go() || go()); | |
| } | |
| }; | |
| // collect gold | |
| hero.collectGoldForSoldiers = function() { | |
| var items = hero.findItems(); | |
| var item = hero.findNearest(items); | |
| if (item) { | |
| var itemPos = item.pos; | |
| hero.move(itemPos); | |
| } | |
| }; | |
| // STRATEGY | |
| var nextPosition = makeGetNext(LOCATIONS); | |
| var nextTower = makeGetNext(LOCATIONS); | |
| while (true) { | |
| var enemies = hero.findEnemies(); | |
| hero.collectGoldForSoldiers(); | |
| hero.summonSoldiers(); | |
| var soldiers = hero.findByType("soldier"); | |
| hero.commandSoldiers(makeGetNext(soldiers), nextPosition); | |
| if (soldiers.length >= 12) { | |
| hero.findLocationsAndBuildTowers(nextTower); | |
| } | |
| } | |
| // Helper | |
| function makeGetNext (path) { | |
| var len = path.length + 1; | |
| var position = 0; | |
| return function getNext () { | |
| var location = path[position%len]; | |
| position += 1; | |
| return location; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment