// Goal: build three rows of nine fire-traps. // Returns "retreat", "attack", "start-next-trap-column", or "build-next-trap-in-column" this.chooseStrategy = function() { var enemies = this.findEnemies(); // If there are overwhelming ogre forces, return the "retreat" strategy. if(enemies.length > 20) { return "retreat"; } // If there are some ogres, return the "attack" strategy. else if (enemies.length > 0 && enemies.length <= 20) { return "attack"; } // Use x % 9 === 0 to see if x is divisible by 9. // Use this.built.length to see how many traps you have built. // If you have finished a column of 9 traps, return "start-next-trap-column" else if (this.built.length % 9 === 0) { return "start-next-trap-column"; } // Otherwise, return "build-next-trap-in-column" else { return "build-next-trap-in-column"; } }; var trapsInColumn = 9; var startX = 40; var columnX = startX; // Build the next trap in a column in the correct place. this.buildNextTrapInColumn = function(columnX,numTraps) { // Change newY to use % to wrap around and only build trapsInColumn (9) traps per column var newY = numTraps % trapsInColumn * 7 + 10; if (this.pos.y < newY) { this.move({"x": columnX - 5, "y": newY}); } else { this.buildTrap(columnX,newY); } }; // Start a new column of traps. this.startNextTrapColumn = function(columnX, numTraps) { var newX = startX - (Math.floor(numTraps / trapsInColumn) * 10); if (this.pos.y > 10) { this.move({"x": newX - 5, "y": 10}); return columnX; } else { this.buildTrap(newX,10); return newX; } }; this.buildTrap = function(x, y) { this.buildXY("fire-trap", x, y); }; this.commandAttack = function() { // Have your griffin riders fend off the attackers. var friends = this.findFriends(); for(var i = 0; i< friends.length; i++){ var friend = friends[i]; var enemy = friend.findNearestEnemy(); if (enemy) { this.command(friend, "attack", enemy); } } }; this.commandRetreat = function() { // You and your griffin riders retreat to safety behind the traps. var friends = this.findFriends(); for(var i = 0; i< friends.length; i++){ var friend = friends[i]; this.command(friend, "move", {x:4,y:42}); } this.moveXY(4, 42); };
// The goal is to survive for 30 seconds, and keep the mines intact for at least 30 seconds. this.chooseStrategy = function() { var fang = this.findByType("fangrider"); if (this.gold > this.costOf("griffin-rider")) { return "griffin-rider"; } // If you can summon a griffin-rider, return "griffin-rider" // If there is a fangrider on your side of the mines, return "fight-back" else if (fang) { for(var i = 0; i < fang.length; i++){ var fa = fang[i]; if (fa.pos.x <= 38) { return "fight-back"; } } } // Otherwise, return "collect-coins" else { return "collect-coins"; } };
this.commandAttack = function() { // Command your griffin riders to attack ogres. var friends = this.findFriends(); if (friends) { for(var i = 0; i< friends.length; i++){ var friend = friends[i]; var enemy = friend.findNearestEnemy(); if (enemy) { this.command(friend, "attack", enemy); } } } };
this.pickUpCoin = function() { // Collect coins var coin = this.findNearest(this.findItems()); if (coin) { this.move(coin.pos); } };
this.heroAttack = function() { // Your hero should attack fang riders that cross the minefield. var en = this.findNearest(this.findByType("fangrider")); if (en) { this.attack(en); } };
loop { this.commandAttack(); var strategy = this.chooseStrategy(); // Call a function, depending on what the current strategy is. if (strategy == "griffin-rider") { this.summon("griffin-rider"); } else if (strategy == "fight-back") { this.heroAttack(); } else if ("collect-coins") { this.pickUpCoin(); } }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34、Toil and Trouble
// Ogre Witches have some unpleasant surprises ready for you. // Define a chooseTarget function which takes a friend argument // Returns the a target to attack, depending on the type of friend. // Soldiers should attack the witches, archers should attack nearest enemy. this.chooseTarget = function(soldier){ if (soldier.type == "soldier") { return "witches"; } if (soldier.type == "archers") { return "enemy"; } }; this.attackEnemy = function(solider){ var enemy = solider.findNearestEnemy(); if (enemy) { this.command(solider, "attack", enemy); } };
loop { var friends = this.findFriends(); for(var i=0; i < friends.length; i++) { // Use your chooseTarget function to decide what to attack. var friend = friends[i]; var choose = this.chooseTarget(friend); if (choose == "witches") { var enemy = this.findNearest(this.findByType("witches")); if (enemy) { this.command(friend, "attack", enemy); } else { this.attackEnemy(friend); } } if (choose == "enemy") { this.attackEnemy(friend); } } }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35、Raiders of the Long Dark
//需多提交几次才可能通关 // Your goal is to protect the peasant and move to the right. // Arryn Stonewall will defend the front, and command the soldiers. // You need to cover the rear and command the peasant. var arryn = this.findByType("raider")[0]; var peasant = this.findByType("peasant")[0]; this.chooseHeroStrategy = function() { // Return either "fight" or "advance". if (this.distanceTo(peasant) > 15) { return "advance"; } var enemy = this.findNearest(this.findEnemies()); if (enemy && this.distanceTo(enemy) < 10) { return "fight"; } else { return "advance"; } // Try to stay 5m behind the peasant when not fighting. // Don‘t get more than 15m away from the peasant. };
this.heroFight = function() { // Stop the ogres from rushing past you to get to the peasant! // Hint: try to slow them down if you can var enemy = this.findNearest(this.findEnemies()); if (enemy) { if (this.isReady("bash")) { this.bash(enemy); } else { this.attack(enemy); } } };