Skip to content

Instantly share code, notes, and snippets.

@DGAcode
Last active September 22, 2016 22:05
Show Gist options
  • Save DGAcode/1b4bd60035e0295091bcae8a09882124 to your computer and use it in GitHub Desktop.
Save DGAcode/1b4bd60035e0295091bcae8a09882124 to your computer and use it in GitHub Desktop.
CodeCombat.com Solutions, Sarven Desert. Here's a simple gist of my Code Combat solutions. Javascript. 4-11 hours: arithmetic, counters, while-loops, break, arrays, string comparison, finding min/max
// Ogres are assaulting the Crux of the Desert!
// Defend against the waves by checking which direction the ogres are coming from.
// Store the results of comparisons as variables to make your code easy to read.
while (true) {
var enemy = hero.findNearestEnemy();
if (enemy) {
// An enemy is to the left if the hero's x is greater than the enemy's.
var isLeft = hero.pos.x > enemy.pos.x;
// An enemy is above if the enemy's y is greater than the hero's.
var isAbove = hero.pos.y < enemy.pos.y;
// Check if the enemy is to the right of the hero:
var isRight = enemy.pos.x > this.pos.y;
// Check if the enemy is below the hero.
var isBelow = enemy.pos.y < this.pos.y;
// If an enemy is above and to the left, build a fire-trap accordingly.
if (isLeft && isAbove) {
hero.buildXY("fire-trap", 40 - 20, 34 + 17);
}
// Check if an enemy is above and to the right:
if (isAbove && isRight) {
hero.buildXY("fire-trap", 60, 51);
}
// Check if an enemy is below and to the left:
if (isBelow && isLeft) {
hero.buildXY("fire-trap", 20, 17);
}
// Check if an enemy is below and to the right:
if (isBelow && isRight) {
hero.buildXY("fire-trap", 60, 17);
}
hero.moveXY(40, 34);
} else {
hero.moveXY(40, 34);
}
}
// Use while to loop until you have counted 10 attacks.
var attacks = 0;
while (attacks < 10) {
// Attack the nearest enemy!
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
attacks += 1;
} // Incrementing means to increase by 1.
// Increment the attacks count.
}
// When you're done, retreat to the ambush point.
hero.say("retreat!!!");
//∆ Don't just stand there blabbering!
hero.moveXY(79, 32);
var peasants = this.findByType("peasant");
loop {
var ball = this.findNearest(this.findByType("ball"));
if (ball) {
var yPos = ball.pos.y;
if (ball.pos.y > 45) {
yPos = 45 - (ball.pos.y - 45) / 2;
}
if (ball.pos.y < 30) {
yPos = 30 - (ball.pos.y - 30) / 2;
}
if (ball.velocity.y > 0) {
yPos += 2;
} else if (ball.velocity.y < 0) {
yPos -= 2;
}
this.command(peasants[0], "move", {
x: 16,
y: yPos
});
}
this.command(peasants[1], "move", {
x: peasants[0].pos.x,
y: peasants[0].pos.y - 6
});
}
// Collect 25 gold, and then tell Naria the total.
// Use break to stop collecting when totalGold >= 25.
var totalGold = 0;
while (true) {
var coin = hero.findNearestItem();
if (coin) {
// Pick up the coin.
hero.moveXY(coin.pos.x, coin.pos.y);
// Add the coin's value to totalGold. (See the guide for more.)
// Get its value with: coin.value
totalGold += coin.value;
}
if (totalGold >= 25) {
// >= means totalGold is greater than or equal to 25.
// This breaks out of the loop to run code at the bottom.
break;
}
}
// Done collecting gold!
hero.moveXY(58, 33);
// Go to Naria and say how much gold you collected.
hero.say("take " + totalGold + " you whore!");
// Protect the nearby peasant long enough to gather all the coins!
// Move to the point between their position and the tower's position!
while (true) {
var enemy = hero.findNearestEnemy();
var friend = hero.findNearest(hero.findFriends());
// Find the point between the enemy's position and your friend's position.
// Check the guide if you need more help!
if (enemy) {
hero.moveXY(friend.pos.x / 2 + enemy.pos.x / 2, friend.pos.y / 2 + enemy.pos.y / 2);
}
}
// Lure the ogres into a trap.
// But, they are careful.
// These ogres will only follow the hero if the hero is injured.
// This function checks the hero's health and returns a Boolean value.
function shouldRun() {
if (hero.health < hero.maxHealth / 2) {
return true;
} else {
return false;
}
}
while (true) {
// Run to the X only if the hero shouldRun().
var run = shouldRun();
if (run === true) {
hero.moveXY(75, 37);
}
var enemy = hero.findNearestEnemy();
if (shouldRun() === false) {
hero.attack(enemy);
} // Otherwise, fight!
}
// Use different colored flags to perform different tasks.
while (true) {
var flagGreen = hero.findFlag("green");
var flagBlack = hero.findFlag("black");
// If there's a flagGreen...
if (flagGreen) {
hero.buildXY("fence", flagGreen.pos.x, flagGreen.pos.y);
}
if (flagGreen) {
hero.pickUpFlag(flagGreen);
}
// Build a fence at flagGreen's position.
// Pick up the flag!
if (flagBlack) {
hero.buildXY("fire-trap", flagBlack.pos.x, flagBlack.pos.y);
}
// If there's a flagBlack...
if (flagBlack) {
hero.pickUpFlag(flagBlack);
} // Build a fire-trap at flagBlack's position.
// Pick up the flag!
// Move back to the center.
else {
hero.moveXY(43, 31);
}
}
// Use while loops to pick out the ogre
while (true) {
var enemies = hero.findEnemies();
var enemyIndex = 0;
// Wrap this logic in a while loop to attack all enemies.
// Find the array's length with: enemies.length
while (enemyIndex < enemies.length) {
var enemy = enemies[enemyIndex];
// "!=" means "not equal to."
if (enemy.type != "sand-yak") {
// While the enemy's health is greater than 0, attack it!
while (enemy.health > 0) {
hero.attack(enemy);
}
}
enemyIndex++;
// Between waves, move back to the center.
hero.moveXY(40, 32);
}
}
# The Mighty Sand Yak
// Let yaks get close, then move 10m right to dodge.
// Dodge 4 yaks to complete the level.
while(true) {
// Get your current x and y position using your Sense Stone.
var x = hero.pos.x;
var y = hero.pos.y;
// Find the nearest yak.
var yak = hero.findNearestEnemy();
// Use "if" to only move when a yak is less than 10m away.
if (hero.distanceTo(yak) < 10) {
// To move right, add 10 to your x position.
hero.moveXY(this.pos.x +10, this.pos.y);
// Use moveXY to move!
}
}
// Fight enemies for 15 seconds.
// Keep count whenever an enemy is defeated.
var dead = 0;
while (true) {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
dead++;
}
if (hero.now() > 15) {
break;
}
}
// Tell Naria how many enemies you defeated.
this.moveXY(58, 33);
this.say(dead);
// Collect coins until the clock reaches 30 seconds.
while (true) {
var item = hero.findNearestItem();
if (item) {
hero.move(item.pos);
}
if (hero.now() > 30) {
break;
}
}
// Tell Naria how much gold you collected.
this.moveXY(58, 33);
this.say(this.gold);
// Fight enemies until the clock reaches 45 seconds.
// Remember to reset the count of defeated enemies!
var deadTWO = 0;
while (true) {
enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
deadTWO++;
}
if (hero.now() > 45) {
break;
}
}
// Tell Naria how many enemies you defeated.
this.moveXY(58, 33);
this.say(deadTWO);
// This function should return the enemy with the most health.
function findStrongestEnemy(enemies) {
var strongest = null;
var strongestHealth = 0;
var enemyIndex = 0;
// Iterate over all the ogres to find the one with the most health.
while (enemyIndex < enemies.length) {
if (enemies[enemyIndex].health > strongestHealth) {
strongest = enemies[enemyIndex];
strongestHealth = strongest.health;
enemyIndex += 1;
}
return strongest;
}
}
var enemies = hero.findEnemies();
if (enemies) {
hero.say(findStrongestEnemy(enemies));
}
while (true) {
var ene = hero.findNearest(hero.findEnemies());
if (ene) {
hero.attack(ene);
}
}
// A desert storm is coming!
// Yaks can detect when a storm is coming.
// This variable will be used as a condition.
var yak = hero.findNearest(hero.findEnemies());
// While there is a sand yak:
while (yak) {
var item = hero.findNearest(hero.findItems());
if (item) {
hero.moveXY(item.pos.x, item.pos.y);
}
// Update the value of the variable "yak".
// Use "findNearestEnemy" and assign it to "yak".
yak = hero.findNearest(hero.findEnemies());
}
// The yaks have hidden.
// Move to the hideout.
hero.moveXY(38, 58);
while (true) {
var enemy = hero.findNearest(hero.findEnemies());
if (enemy && enemy.type != "sand-yak" && hero.canCast("chain-lightning", enemy)) {
hero.cast("chain-lightning", enemy);
}
if (enemy && enemy.type == "archer") {
hero.attack(enemy);
}
if (enemy && enemy.type != "sand-yak" && hero.isReady("jump") && hero.isReady("bash")) {
hero.jumpTo(enemy);
hero.bash(enemy);
} else if (enemy && enemy.type != "sand-yak") {
hero.attack(enemy);
}
}
// Race munchkins to the water distilled by Omarn Brewstone!
// The continue statement is powerful for managing complicated logic.
// When the program uses the continue statement, the rest of the loop is skipped.
// However, unlike with "break", the loop repeats instead of stopping.
// Use "continue" to verify the conditions of the ambush.
while (true) {
var enemy = hero.findNearestEnemy();
var item = hero.findNearestItem();
// If there is no enemy, continue out of the loop.
if (!enemy) {
continue;
}
// If there is an enemy, but no item, ask for a potion and continue out of the loop.
if (!item) {
hero.say("Give me a drink!");
continue;
}
// Use an if-statement to check the item's type. If the type is "poison", continue out of the loop.
if (item.type == "poison") {
continue;
}
// If it is not, the potion must be a bottle of water, so walk to it and return to the starting position!
if (item.type != "poison") {
hero.moveXY(item.pos.x, item.pos.y);
hero.moveXY(35, 45);
}
}
// Protect peasants from ogres.
while (true) {
// Get an array of enemies.
var enemies = hero.findEnemies();
// If the array is not empty.
if (enemies.length > 0) {
// Attack the first enemy from "enemies" array.
hero.attack(enemies[0]);
// Return to the start position.
hero.moveXY(40, 20);
}
}
// Use fire-traps to defeat the ogres attacking the trading post.
while(true) {
var enemy = hero.findNearestEnemy();
if(enemy) {
if(enemy.pos.x < hero.pos.x) {
// If the enemy is to the left, build a fire-trap to the left.
hero.buildXY("fire-trap", 25, 34);
} else if (enemy.pos.x > hero.pos.x) {
// If the enemy is to the right, build a fire-trap to the right.
hero.buildXY("fire-trap", 55, 34);
} else if (enemy.pos.y < hero.pos.y) {
// If the enemy is below the hero, build a fire-trap below.
hero.buildXY("fire-trap", 40, 19);
} else if (enemy.pos.y > hero.pos.y) {
// If the enemy is above the hero, build a fire-trap above.
hero.buildXY("fire-trap", 40, 49);
}
}
hero.moveXY(40, 34);
}
// Walk through the minefield
// To find the path use a cubic equation
// You can find coefficients for the equation on the tower
// This function returns the number multiplied by the times
function mult(number, times) {
var total = 0;
while (times > 0) {
total += number;
times--;
}
return total;
}
// This function returns the number to the exponent power.
function power(number, exponent) {
var total = 1;
// Complete the function. It must return the 'base' raised to the power 'exponent'.
while (exponent > 0) {
total *= number;
exponent--;
}
return total;
}
// Don't change the follow code
var tower = this.findFriends()[0];
var a = tower.a;
var b = tower.b;
var c = tower.c;
var d = tower.d;
var x = hero.pos.x;
while (true) {
var y = a * power(x, 3) + b * power(x, 2) + c * power(x, 1) + d * power(x, 0);
hero.moveXY(x, y);
x = x + 5;
}
// It's too hot out here! Each second you lose you health.
// You need to kill 3 enemy skeletons.
// You can only drink one potion. Choose your time wisely.
// Graverobbing is bad luck! Do not steal the coins.
while (true) {
var enemy = hero.findNearest(hero.findEnemies());
// Attack only skeletons AND if they are on the "ogres" team.
if (enemy && enemy.team === "ogres" && enemy.type === "skeleton") {
hero.attack(enemy);
}
var item =hero.findNearest(hero.findItems());
// Take only the "potion" type AND when your health less than quarter of the maxHealth.
if(hero.health < hero.maxHealth/4 && item.type==="potion") {
hero.moveXY(item.pos.x, item.pos.y);
}
}
// We are field testing a new battle unit: the decoy.
// Build 4 decoys, then report the total to Naria.
var decoysBuilt = 0;
while (true) {
var coin = hero.findNearestItem();
if (coin) {
// Loot the coin!
hero.move(coin.pos);
}
// Each decoy costs 25 gold.
// Know when you have more than 25 gold with hero.gold
if (hero.gold >= 25) {
hero.buildXY("decoy", this.pos.x, this.pos.y);
decoysBuilt++;
}
// Keep a count of decoys you built as you go along.
if (decoysBuilt == 4) {
// Break out of the loop when you have built 4.
break;
}
}
hero.say("Done building decoys!");
hero.moveXY(14, 36);
// Go to Naria and say how many decoys you built.
hero.say(decoysBuilt);
// while-loops repeat until the condition is false.
// Always take an action inside a while loop, or it'll go infinite!
var ordersGiven = 0;
while (ordersGiven < 5) {
// Move down 10m and order each of your allies into battle. (They can only hear you if you are standing directly in front of them.)
hero.moveXY(this.pos.x, this.pos.y - 10);
hero.say("Attack!");
// Be sure to increment ordersGiven!
ordersGiven++;
}
while (true) {
var enemy = hero.findNearestEnemy();
// When you're done giving orders, join the attack.
if (enemy) {
hero.attack(enemy);
}
}
// Dodge the cannons and collect 8 gems.
// Watch out, cannons are ready to fire!
// Move slow along a special pattern to confuse them.
// This function returns a value from 0 to 30:
function mod30(n) {
if (n >= 30) {
return n - 30;
} else {
return n;
}
}
// This function should return a value from 0 to 40:
function mod40(n) {
// Use an if-statement to return the correct value.
if (n >= 40) {
return n - 40;
} else {
return n;
}
}
// You don't need to change the following code:
while (true) {
var time = hero.now();
var x = mod30(time) + 25;
var y = mod40(time) + 10;
hero.moveXY(x, y);
}
//Commented out to stop infinite loop.
// Kill at least 6 ogres on the left side.
// Then, collect at least 30 gold on the right.
// This variable is used for counting ogres.
var defeatedOgres = 0;
// This loop is executed while "defeatedOgres" is less than 6.
while (defeatedOgres < 6) {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
defeatedOgres += 1;
} else {
hero.say("Ogres!");
}
}
// Move to the right part of the map.
hero.moveXY(49, 36);
// This loop is executed while you have less than 30 gold.
while (hero.gold < 30) {
var item = hero.findNearest(hero.findItems());
hero.moveXY(item.pos.x, item.pos.y);
}
// Move to the exit.
hero.moveXY(76, 32);
pet.itMoves = function () {
if (pet.pos.x < 50) {
pet.moveXY(49, 21);
}
};
hero.heMoves = function () {
if (hero.isReady("jump")) {
hero.jumpTo({
x: 50,
y: 12
});
hero.moveXY(50, 12);
}
};
pet.on('spawn', pet.itMoves);
hero.heMoves();
// Use your new skill to choose what to do: hero.now()
while (true) {
var enemy = hero.findNearestEnemy();
// If it's the first 10 seconds, fight.
if (hero.now() < 10) {
hero.attack(enemy); // Else, if it's the first 30 seconds, collect coins.
} else if (hero.now() < 35) {
var item = hero.findNearestItem();
hero.move(item.pos); // After 30 seconds, join the raid!
} else {
hero.attack(enemy);
}
}
// findEnemies returns a list of all your enemies.
// Only attack shamans. Don't attack yaks!
var enemies = hero.findEnemies();
var enemyIndex = 0;
// Wrap this section in a while loop to iterate over all enemies.
// While the enemyIndex is less than the length of enemies
while (enemyIndex < enemies.length) {
var enemy = enemies[enemyIndex];
if (enemy.type == 'shaman') {
while (enemy.health > 0) {
hero.attack(enemy);
}
}
// Remember to increment enemyIndex
enemyIndex++;
}
// Kill the enemy that's farthest away first.
loop {
var farthest = null;
var maxDistance = 0;
var enemyIndex = 0;
var enemies = hero.findEnemies();
// Look at all the enemies to figure out which one is farthest away.
while (enemyIndex < enemies.length) {
var target = enemies[enemyIndex];
enemyIndex += 1;
// Is this enemy farther than the farthest we've seen so far?
var distance = hero.distanceTo(target);
if (distance > maxDistance) {
maxDistance = distance;
farthest = target;
}
}
if (farthest) {
// Take out the farthest enemy!
// Keep attacking the enemy while its health is greater than 0.
hero.attack(farthest);
}
}
// Destroy mechs and collect gold from them.
while (true) {
var coin = hero.findNearest(hero.findItems());
// While a coin exists:
if (coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
// Collect the coin.
// Reassign the variable "coin" to the nearest item.
coin = hero.findNearest(hero.findItems());
var enemy = hero.findNearest(hero.findEnemies());
if (enemy) {
if (enemy && hero.canCast("chain-lightning", enemy)) {
hero.cast("chain-lightning", enemy);
}
// While the enemy's health greater than 0.
while (enemy && enemy.health > 0) {
if (enemy && enemy.pos.x > hero.pos.x && hero.isReady("bash")) {
hero.bash(enemy);
}
hero.attack(enemy);
} // Attack it.
}
}
// Ask the healer for help when you're under one-half health.
while (true) {
var currentHealth = hero.health;
var healingThreshold = hero.maxHealth / 2;
var enemy = hero.findNearestEnemy();
// If your current health is less than the threshold,
// move to the healing point and say, "heal me".
if (currentHealth <= healingThreshold) {
hero.moveXY(64, 46);
hero.say("heal me");
}
// Otherwise, attack. You'll need to fight hard!
if (enemy) {
hero.attack(enemy);
}
}
// Run while you can, but watch your health.
// While hero's health greater than 200:
while (true && hero.health >= 200) {
// Δ Change this!
hero.moveXY(48, 24);
hero.moveXY(16, 24);
}
// Move to Okar.
hero.moveXY(32, 40);
// Move right to reach the oasis,
// but move left to avoid nearby yaks.
while(true) {
var x = hero.pos.x;
var y = hero.pos.y;
var enemy = hero.findNearestEnemy();
if (enemy && hero.distanceTo(enemy) < 10) {
// Move to the left by subtracting 10 from your X coordinate.
hero.moveXY(this.pos.x -10, this.pos.y);
} else {
// Move to the right by adding 10 to your X coordinate.
hero.moveXY(this.pos.x +10, this.pos.y);
}
}
// Wait for ogres, defeat them and collect gold.
while (true) {
var enemies = hero.findEnemies();
// That variable is used to iterate "enemies" array.
var enemyIndex = 0;
// While it less than the array length:
while (enemyIndex < enemies.length) {
// Get an enemy from the array.
var enemy = enemies[enemyIndex];
enemyIndex++;
hero.attack(enemy);
}
var coins = hero.findItems();
// That variable is used to iterate "coins" array.
var coinIndex = 0;
while (coinIndex < coins.length) {
// Get a coin from the "coins" array at coinIndex.
var coin = coins[coinIndex];
// Collect that coin.
coinIndex++;
hero.moveXY(coin.pos.x, coin.pos.y);
}
}
// Call peasants one after another.
// Neutral units are detected as enemies.
var neutrals = hero.findEnemies();
while (true) {
neutrals = hero.findEnemies();
if (neutrals.length) {
// Say the first peasant in the array of neutral units.
hero.say(neutrals[0]);
} else {
hero.say("Nobody here");
}
// Reassign "neutrals" with "findEnemies" method again.
neutrals++;
}
// This field is covered in firetraps. Thankfully we've sent a scout ahead to find a path. He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.
// This canyon seems to interfere with your findNearest glasses!
// You'll need to find the nearest coins on your own.
loop {
var coins = hero.findItems();
var coinIndex = 0;
var nearest = null;
var nearestDistance = 9999;
// Loop through all the coins to find the nearest one.
while (coinIndex < coins.length) {
var coin = coins[coinIndex];
coinIndex++;
var distance = hero.distanceTo(coin);
// If this coin's distance is less than the nearestDistance
if (distance < nearestDistance) {
// Set nearest to coin
nearest = coin;
// Set nearestDistance to distance
nearestDistance = distance;
}
}
// If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
if (nearest) {
hero.moveXY(nearest.pos.x, nearest.pos.y);
}
}
// Get to the oasis. Watch out for new enemies: ogre scouts!
// Go up and right by adding to your current X and Y position.
while(true) {
// Attack any enemies you see.
var enemy = hero.findNearestEnemy();
if(enemy){
hero.attack(enemy);
}
// Or, if there are no enemies in sight, keep moving up and to the right.
else {
hero.moveXY(this.pos.x +4, this.pos.y +4);
}
}
// An ARRAY is a list of items.
// This array is a list of your friends' names.
// Array indices start at 0, not 1!
var friendNames = [
'Joan',
'Ronan',
'Nikita',
'Augustus'
];
var friendIndex = 0;
// Loop over each name in the array.
// The .length property gets the length of the array.
while (friendIndex < friendNames.length) {
// Use square brackets to get a name from the array.
var friendName = friendNames[friendIndex];
// Tell your friend to go home.
// Use + to connect two strings.
hero.say(friendName + ', retreat!!!');
// Increment the index to get the next name from the array.
friendIndex++;
}
// Retreat to the oasi
hero.moveXY(27, 30);
hero.buildXY("fence", 30, 30);
// One gem is safe, others are bombs.
// But you know the answer: always take the second.
while (true) {
var items = hero.findItems();
var gem2 = items[1];
// If there are gems:
if (gem2) {
hero.moveXY(gem2.pos.x, gem2.pos.y); // Collect the second from "items".
} // Else:
// Move to the center mark.
else {
hero.moveXY(40, 34);
}
}
loop {
var coins = this.findItems();
var coinIndex = 0;
// Wrap this into a loop that iterates over all coins.
while (coinIndex < coins.length) {
var coin = coins[coinIndex];
// Gold coins are worth 3.
if (coin.value == 3) {
// Only pick up gold coins.
this.moveXY(coin.pos.x, coin.pos.y);
}
coinIndex += 1;
}
}
// Collect exactly no more than 7 spinach potions.
// With these potions you'll be strong enough to kill ogres.
var potionCount = 0;
// Use while with a condition to check the number of collected potions.
// Δ Wrap the next code block into while loop.
while (potionCount <= 6) {
var item = hero.findNearest(hero.findItems());
if (item) {
hero.moveXY(item.pos.x, item.pos.y);
potionCount++;
}
}
// When the while loop is finished.
// Go and fight!.
while (true) {
var enemy = hero.findNearest(hero.findEnemies());
if (enemy && hero.canCast("chain-lightning", enemy)) {
hero.cast("chain-lightning", enemy);
}
if (enemy && hero.isReady("electrocute")) {
hero.electrocute(enemy);
}
if (enemy) {
hero.attack(enemy);
}
}
// Gems will disappear soon. Hurry!
// "findItems" returns an array of items.
var items = hero.findItems();
// Get the first gem from the array.
// Don't forget that the first index is 0.
var gem0 = items[0];
// Say Bruno to take it.
hero.say("Bruno " + gem0);
// Or you can use it without a variable.
hero.say("Matilda " + items[1]);
// Create a variable, and set it to items[2]:
var gem2 = items[2];
// Move to that variable's position using moveXY()
hero.moveXY(gem2.pos.x, gem2.pos.y);
// This level is intended to be for advanced players. The solution should be pretty complex with a lot of moving parts. It might also be a bit of a gear check unless you use "creative" methods.
// You need to make your way to the first trial (Oasis of Marr) killing enemies along the way. When you reach it, pick all the mushrooms to trigger the trial to begin. If you survive the onslaught, make your way to the next Oasis for the second trial, then the Temple. When all trials are complete you will have to face the final boss. Good luck!
// HINT: Glasses with a high visual range help tremendously on this level so buy the best you can get.
// HINT: the unit 'type' for the oasis guardians is 'oasis-guardian'
while (true) {
var enemy = hero.findNearest(hero.findEnemies());
var item = hero.findNearest(hero.findItems());
var flag = hero.findFlag();
if (enemy && hero.canCast("chain-lightning", enemy)) {
hero.cast("chain-lightning", enemy);
}
if (enemy && hero.canElectrocute(enemy)) {
hero.electrocute(enemy);
}
if (enemy) {
hero.attack(enemy);
}
if (item) {
hero.moveXY(item.pos.x, item.pos.y);
}
if (flag) {
hero.pickUpFlag(flag);
}
}
// Get to the oasis,
// fencing off paths with randomized yaks on them as you go.
while (true) {
var yak = hero.findNearestEnemy();
if (yak) {
// A yak is above you if yak.pos.y is greater than your pos.y.
if (this.pos.y < yak.pos.y) {
hero.buildXY("fence", yak.pos.x, yak.pos.y - 10);
}
// If the yak is above you, build a fence 10m below it.
// If the yak is below you, build a fence 10m above it.
if (this.pos.y > yak.pos.y) {
hero.buildXY("fence", yak.pos.x, yak.pos.y + 10);
}
} else {
// Move right 10m towards the oasis.
hero.moveXY(this.pos.x + 10, this.pos.y);
}
}
// Defeat skeletons and collect lightstones.
while (true) {
var enemies = hero.findEnemies();
var enemyIndex = 0;
while (enemyIndex < enemies.length) {
var enemy = enemies[enemyIndex];
// Hit it while it has health
while (enemy.health > 0) {
hero.attack(enemy);
}
enemyIndex += 1;
}
var items = hero.findItems();
var itemIndex = 0;
// Iterate over all items.
while (itemIndex < items.length) {
var item = items[itemIndex];
// While the distance greater than 4:
while (hero.distanceTo(item) > 4) {
hero.moveXY(item.pos.x, item.pos.y);
}
// Try to take the item.
// Don't forget to increase "itemIndex".
itemIndex += 1;
}
}
// Enemies are sleeping. It's the perfect time for sabotage!
// Be careful and stay on the route.
// Kill the weak ogres with a deadly strike!
// Collect only the cheap coin. Don't be greedy!
var step = 0;
// This is how the hero moves around the level:
function moveHero(stage) {
if (stage === 0) {
hero.moveXY(9 + step * 12, 8);
} else if (stage == 1) {
hero.moveXY(68, 8 + (step - 5) * 10);
} else if (stage === 2) {
hero.moveXY(68 - (step - 10) * 12, 58);
}
return step + 1;
}
while (step < 5) {
step = moveHero(0);
var enemy = hero.findNearestEnemy();
// If the enemy is an ogre and has less than 10 health, attack it!
if (enemy.team === "ogres" && enemy.health < 10) {
hero.attack(enemy);
}
}
while (step < 10) {
step = moveHero(1);
var coin = hero.findNearest(hero.findItems());
// If the coin's value is less than 5 and is closer than 7 meters, collect it!
if (coin.value < 5 && hero.distanceTo(coin) < 7) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}
while (step < 15) {
step = moveHero(2);
enemy = hero.findNearest(hero.findEnemies());
// If the enemy has less than 10 health and is closer than 7 meters, attack it!
if (enemy.health < 10 && hero.distanceTo(enemy) < 7) {
hero.attack(enemy);
}
}
hero.moveXY(10, 60);
// Escape from Death Valley!
// The archers firing at you are not your allies! Dodge the arrows!
// Move by with a zigzag pattern using real modulo functions.
// This function returns a value from 0 to 15:
function mod15(n) {
while (n >= 15) {
n -= 15;
}
return n;
}
// This function should return a value from 0 to 9:
function mod9(n) {
// Use a while loop to modify the parameter before returning
while (n >= 9) {
n -= 9;
}
return n;
}
// Don't change the following code:
while (true) {
var time = hero.now();
var x, y;
if (time < 30) {
y = 10 + 3 * mod15(time);
} else {
y = 20 + 3 * mod9(time);
}
x = 10 + time;
this.moveXY(x, y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment