Skip to content

Instantly share code, notes, and snippets.

@DGAcode
Created July 18, 2016 13:27
Show Gist options
  • Save DGAcode/2b4360468270d49a90cbe9efc5dd6327 to your computer and use it in GitHub Desktop.
Save DGAcode/2b4360468270d49a90cbe9efc5dd6327 to your computer and use it in GitHub Desktop.
CodeCombat.com Solutions, Cloudrip Mountain. Here's a simple gist of my Code Combat solutions. Javascript. Object literals, remote method invocation, for-loops, functions, drawing, modulo
// Summon some soldiers, then direct them to your base.
// Each soldier costs 20 gold.
while (hero.gold > hero.costOf("soldier")) {
hero.summon("soldier");
}
var soldiers = hero.findFriends();
var soldierIndex = 0;
// Add a while loop to command all the soldiers.
for (var i = 0; soldierIndex < soldiers.length; i++) {
hero.command(soldiers[i], "move", {
x: 50,
y: 40
});
}
// Go join your comrades!
hero.moveXY(51, 41);
// Collect 80 gold
while (true) {
var item = hero.findNearest(hero.findEnemies());
if (this.gold < 80) {
hero.move(item.pos);
} else {
break;
}
}
// Build 4 soldiers to use as bait
for (var z = 0; z < 4; z += 1) {
hero.summon('soldier');
continue;
}
// Send your soldiers into position
var points = [];
points[0] = {
x: 13,
y: 73
};
points[1] = {
x: 51,
y: 73
};
points[2] = {
x: 51,
y: 53
};
points[3] = {
x: 90,
y: 52
};
var friends = hero.findFriends();
// Use a for-loop to loop over i from 0 to 4
// Match the friends to the points and command them to move
for (var i = 0; i < friends.length; i += 1) {
hero.command(friends[i], 'move', points[i]);
}
// Use object literals to walk the safe path and collect the gems.
// You cannot use moveXY() on this level! Use move() to get around.
var gems = hero.findItems();
while (hero.pos.x < 20) {
// move() takes objects with x and y properties, not just numbers.
hero.move({
'x': 20,
'y': 35
});
}
while (hero.pos.x < 25) {
// A gem's position is an object with x and y properties.
var gem0 = gems[0];
hero.move(gem0.pos);
}
while (hero.pos.x < 30) {
// move() takes objects with x and y properties, not just numbers.
hero.move({
'x': 30,
'y': 35
});
}
while (hero.pos.x < 35) {
// A gem's position is an object with x and y properties.
var gem1 = gems[1];
hero.move(gem1.pos);
}
// While your x is less than 30,
// Use an object to move to 30, 35.
while (hero.pos.x < 40) {
// move() takes objects with x and y properties, not just numbers.
hero.move({
'x': 40,
'y': 35
});
}
while (hero.pos.x < 45) {
// A gem's position is an object with x and y properties.
var gem2 = gems[2];
hero.move(gem2.pos);
}
// While your x is less than 35,
// Move to the position of gems[1].
// Get to the last couple of gems yourself!
while (hero.pos.x < 50) {
// move() takes objects with x and y properties, not just numbers.
hero.move({
'x': 50,
'y': 35
});
}
while (hero.pos.x < 55) {
// A gem's position is an object with x and y properties.
var gem3 = gems[3];
hero.move(gem3.pos);
}
hero.moveXY(92, 55);
hero.attack('Catapult');
hero.attack('Catapult');
hero.moveXY(92, 12);
hero.attack('Catapult 1');
hero.attack('Catapult 1');
hero.moveXY(60, 34);
this.jumpBash = function () {
var enemy = hero.findNearest(hero.findEnemies());
if (enemy && hero.isReady("jump") && hero.isReady("bash")) {
hero.jumpTo(enemy);
hero.bash(enemy);
}
};
this.fuck = function () {
var enemy = hero.findNearest(hero.findEnemies());
if (enemy) {
hero.attack(enemy);
}
if (hero.canCast("chain-lightning", enemy)) {
hero.cast("chain-lightning", enemy);
}
};
this.summonSoldier = function () {
if (hero.gold >= hero.costOf("griffin-rider")) {
hero.summon("griffin-rider");
}
};
this.commandSoldier = function () {
var friend = hero.findFriends();
var enemy = hero.findNearest(hero.findEnemies());
if (friend) {
for (var i = 0; i < friend.length; i += 1) {
hero.command(friend[i], "attack", enemy);
}
}
};
this.commandPaladin = function () {
var friend = hero.findByType("paladin");
var enemy = hero.findNearest(hero.findEnemies());
if (friend) {
for (var s = 0; s < friend.length; s += 1) {
hero.command(friend[s], "cast", "heal", this);
}
}
};
this.PickupItems = function () {
var item = hero.findNearest(hero.findItems());
if (item) {
hero.move(item.pos);
}
};
this.Wait = function () {
if (this.pos.x > 216) {
hero.wait(10);
}
};
while (true) {
this.summonSoldier();
this.commandSoldier();
this.commandPaladin();
this.jumpBash();
this.fuck();
if (this.pos.x > 249) {
break;
}
}
var tt = true;
while (tt) {
var enemy = hero.findNearest(hero.findEnemies());
if (enemy && enemy.type === 'Warlock') {
hero.attack(enemy);
}
if (this.pos.x > 272) {
hero.moveXY(277, 57);
hero.attack('Vax');
hero.attack('Vax');
hero.moveXY(276, 8);
hero.attack('Vyrryx');
hero.attack('Vyrryx');
hero.attack('Vyrryx');
hero.attack('Yzzrith');
}
var tt = false;
}
while (true) {
this.PickupItems();
this.summonSoldier();
this.commandSoldier();
this.commandPaladin();
this.jumpBash();
this.fuck();
}
// Robobombs explode when they die or touch an enemy.
// Split up your soldiers so that they don't all get exploded together.
while (true) {
var enemies = hero.findEnemies();
var enemy = hero.findNearest(enemies);
var friends = hero.findFriends();
// Send the first soldier of the friends array towards the enemy.
hero.command(friends[0], "attack", enemy);
// i = 1 starts the index at the second element!
for (var i = 1; i < friends.length; i++) {
var friend = friends[i];
// Command the remaining soldiers to run away!
hero.command(friend, "move", {
x: 8,
y: 31
});
}
}
// If the peasant is damaged, the flowers will shrink!
hero.summonSoldiers = function () {
if (hero.gold >= hero.costOf("soldier")) {
hero.summon("soldier");
}
};
// Define the function: commandSoldiers
hero.commandSoldiers = function () {
var friend = hero.findByType("soldier");
for (var i = 0; i < friend.length; i++) {
var friends = friend[i];
var enemy = friends.findNearestEnemy();
if (enemy) {
hero.command(friends, "attack", enemy);
}
}
};
// Define the function: pickUpNearestCoin
hero.pickUpNearestCoin = function () {
var coin = hero.findNearest(hero.findItems());
var target = hero.findNearest(hero.findEnemies());
if (coin) {
hero.move(coin.pos);
}
};
hero.kill = function () {
var target = hero.findNearest(hero.findEnemies());
if (hero.canCast("chain-lightning", target) && target) {
hero.cast("chain-lightning", target);
}
};
var peasant = hero.findByType("peasant")[0];
while (true) {
hero.summonSoldiers();
hero.commandSoldiers();
hero.pickUpNearestCoin();
hero.kill();
}
this.items = function () {
var item = hero.findNearest(hero.findItems());
if (item) {
hero.move(item.pos);
}
};
this.attacks = function () {
var enemy = hero.findNearest(hero.findEnemies());
var friend = hero.findFriends();
if (enemy && enemy.health === 60 && hero.isReady("jump") && hero.isReady("bash")) {
hero.jumpTo(enemy);
hero.bash(enemy);
}
if (enemy && hero.distanceTo(enemy) < 5) {
hero.attack(enemy);
}
};
this.light = function () {
var enemy = hero.findNearest(hero.findEnemies());
if (enemy && hero.canCast('chain-lightning', enemy) && hero.distanceTo(enemy) < 17) {
hero.cast('chain-lightning', enemy);
}
};
this.summons = function () {
if (hero.gold >= hero.costOf("soldier") + hero.costOf("archer") + hero.costOf("griffin-rider")) {
hero.summon("soldier");
hero.summon("archer");
hero.summon("griffin-rider");
}
};
this.commandFriends = function () {
var friend = hero.findFriends();
var enemy = hero.findNearest(hero.findEnemies());
for (var i = 0; i < friend.length; i += 1) {
hero.command(friend[i], 'defend', {
x: 42,
y: 37
});
}
};
this.now1 = function () {
if (hero.now() > 53) {
hero.shield();
}
};
while (true) {
this.items();
this.summons();
this.commandFriends();
this.light();
this.attacks();
if (hero.now() > 53) {
break;
}
}
while (true) {
this.now1();
}
// Catch up to Pender Spellbane to learn her secrets.
while (true) {
// Pender is the only friend here, so she's always the nearest.
var pender = hero.findNearest(hero.findFriends());
if (pender) {
// moveXY() will move to where Pender is,
// but she'll have moved away by the time you get there.
hero.move(pender.pos); // move() only moves one step at a time,
// so you can use it to track your target.
//hero.move(pender.pos);
}
}
// Ogres are trying to kill your reindeer!
// Keep your archers back while summoning soldiers to attack.
hero.pickUpCoin = function () {
// Collect coins.
var item = hero.findNearest(hero.findItems());
if (item) {
hero.move(item.pos);
}
};
hero.summonTroops = function () {
// Summon soldiers if you have the gold.
if (this.gold >= this.costOf("soldier")) {
hero.summon("soldier");
}
};
// This function has an argument named soldier.
// Arguments are like variables.
// The value of an argument is determined when the function is called.
hero.commandSoldier = function (soldier) {
// Soldiers should attack enemies.
soldier = this.findByType("soldier");
var enemy = this.findNearest(this.findEnemies());
if (enemy) {
for (var ind = 0; ind < soldier.length; ind += 1) {
hero.command(soldier[ind], "attack", enemy);
}
}
};
// It should take one argument that will represent the archer passed to the function when it's called.
// Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
hero.commandArcher = function (archer) {
archer = this.findByType("archer");
var enemy = this.findNearest(this.findEnemies());
if (enemy) {
for (var ss = 0; ss < archer.length; ss += 1) {
hero.command(archer[ss], "move", archer.pos);
}
}
};
while (true) {
hero.pickUpCoin();
hero.summonTroops();
var friends = hero.findFriends();
var enemy = this.findNearest(this.findEnemies());
for (var i = 0; i < friends.length; i++) {
var friend = friends[i];
if (friend.type == "soldier") {
// This friend will be assigned to the variable soldier in commandSoldier
hero.commandSoldier(friend);
} else if (friend.type == "archer") {
// Be sure to command your archers.
hero.commandArcher();
}
}
}
// Command your troops to move east and attack any ogres they see.
// Use for-loops and findFriends.
// You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
hero.attacks = function () {
var friends = hero.findFriends();
var enemies = hero.findNearest(hero.findEnemies());
for (var i = 0; i < friends.length; i++) {
if (friends[i].pos.y > hero.pos.y) {
hero.command(friends[i], "defend", {
x: 55,
y: 62
});
} else if (friends[i].pos.y == hero.pos.y) {
hero.command(friends[i], "defend", {
x: 83,
y: 47
});
} else if (friends[i].pos.y < hero.pos.y) {
hero.command(friends[i], "defend", {
x: 62,
y: 30
});
}
}
};
hero.moveXY(80, 47);
while (true) {
var enemies = hero.findNearest(hero.findEnemies());
hero.attacks();
if (enemies) {
hero.attack(enemies);
}
}
// Define your own simple movement functions.
// Define moveRight
// Note: each function should move the hero 12 meters!
hero.moveRight = function () {
var target = {
"x": hero.pos.x + 12,
"y": hero.pos.y
};
while (hero.distanceTo(target) > 0.1) {
hero.move(target);
}
};
// Define moveDown
this.moveDown = function () {
var target = {
"x": hero.pos.x,
"y": hero.pos.y - 12
};
while (hero.distanceTo(target) > 0.1) {
hero.move(target);
}
};
// Define moveUp
this.moveUp = function () {
var target = {
"x": hero.pos.x,
"y": hero.pos.y + 12
};
while (hero.distanceTo(target) > 0.1) {
hero.move(target);
}
};
// Now, use those functions!
hero.moveRight();
hero.moveDown();
hero.moveUp();
hero.moveUp();
hero.moveRight();
// Gather coins to summon soldiers and have them attack the enemy.
while (true) {
var item = hero.findNearest(hero.findItems());
// Move to the nearest coin.
// Use move instead of moveXY so you can command constantly.
if (item) {
hero.move(item.pos);
}
// If you have funds for a soldier, summon one.
if (hero.gold > hero.costOf('soldier')) {
hero.summon('soldier');
}
var enemy = hero.findNearest(hero.findEnemies());
if (enemy) {
// Loop over all your soldiers and order them to attack.
var soldiers = hero.findFriends();
var soldierIndex = 0;
while (soldierIndex < soldiers.length) {
var soldier = soldiers[soldierIndex];
soldierIndex++;
// Use the 'attack' command to make your soldiers attack.
this.command(soldier, 'attack', enemy);
}
}
}
// You only have 20 seconds until the ogre horde arrives!
// Grab as much gold as you can, then retreat to your base and wall it off!
while (hero.now() < 20) {
// Collect coins
var item = hero.findNearest(hero.findItems());
if (item) {
hero.moveXY(item.pos.x, item.pos.y);
}
}
while (hero.pos.x > 16) {
// Retreat behind the fence
hero.move({
x: 16,
y: 37
});
}
// Build a fence to keep the ogres out.
hero.buildXY("fence", 20, 37);
hero.chooseStrategy = function () {
var enemies = hero.findEnemies();
var enemy = hero.findNearest(hero.findEnemies());
var fam = hero.findByType("fangrider");
if (hero.gold >= hero.costOf('griffin-rider')) {
hero.summon('griffin-rider');
}
if (enemy && enemy.pos.x < 37) {
hero.attack(enemy);
}
};
hero.commandAttack = function () {
var enemy = hero.findNearest(hero.findEnemies());
var fri = hero.findFriends();
for (var i = 0; i < fri.length; i++) {
if (enemy) {
hero.command(fri[i], "defend", {
x: 90,
y: 40
});
}
}
};
hero.pickUpCoin = function () {
var item = hero.findNearest(hero.findItems());
if (item) {
hero.move(item.pos);
}
};
hero.heroAttack = function () {
var enemy = hero.findNearest(hero.findEnemies());
if (enemy) {
hero.attack(enemy);
}
};
while (true) {
hero.commandAttack();
hero.pickUpCoin();
var strategy = hero.chooseStrategy();
}
// You now have the Ring of Flowers! You can do:
// toggleFlowers(true/false) - turns flowers on or off.
// setFlowerColor("random") - can also be "pink", "red", "blue", "purple", "yellow", or "white".
// Here are some functions for drawing shapes:
// x, y - center of the shape
// size - size of the shape (radius, side length)
hero.drawCircle = function (x, y, size) {
var angle = 0;
hero.toggleFlowers(false);
while (angle <= Math.PI * 2) {
var newX = x + size * Math.cos(angle);
var newY = y + size * Math.sin(angle);
hero.moveXY(newX, newY);
hero.toggleFlowers(true);
angle += 0.2;
}
};
hero.drawSquare = function (x, y, size) {
hero.toggleFlowers(false);
var cornerOffset = size / 2;
hero.moveXY(x - cornerOffset, y - cornerOffset);
hero.toggleFlowers(true);
hero.moveXY(x + cornerOffset, y - cornerOffset);
hero.moveXY(x + cornerOffset, y + cornerOffset);
hero.moveXY(x - cornerOffset, y + cornerOffset);
hero.moveXY(x - cornerOffset, y - cornerOffset);
};
var redX = {
x: 28,
y: 36
};
var whiteX = {
x: 44,
y: 36
};
// Pick a color.
hero.setFlowerColor("white");
// Draw a size 10 circle at the redX.
hero.drawCircle(28, 36, 10);
// Change the color!
hero.setFlowerColor("purple");
// Draw a size 10 square at the whiteX.
hero.drawSquare(44, 36, 10);
// Now experiment with drawing whatever you want!
hero.moveXY(28, 36);
hero.moveXY(44, 36);
hero.moveXY(36, 47);
hero.moveXY(28, 36);
hero.setFlowerColor("yellow");
hero.drawCircle(37, 37, 20);
// The inner gate can hold against ogres for a long time.
// But one of these peasants is an OGRE SPY!
// We have a hint: the spy's name contains the letter "z"
// This function should check if a word contains a certain letter
// A string is an array of characters! You can loop over it just like an array.
function letterInWord(word, letter) {
for (var i = 0; i < word.length; i++) {
var character = word[i];
// If character is equal to letter, return true
if (character === letter) {
return true;
}
}
// The letter isn't in the word, so return false
return false;
}
var spyLetter = "z";
var friends = hero.findFriends();
for (var j = 0; j < friends.length; j++) {
var friendName = friends[j].id;
if (letterInWord(friendName, spyLetter)) {
// Reveal the spy!
hero.say(friendName + " is a spy!!!");
} else {
hero.say(friendName + " is a friend.");
}
}
while (true) {
// Collect gold.
var coin = hero.findNearest(hero.findItems());
if (coin) {
hero.move(coin.pos);
}
// If you have enough gold, summon a soldier.
if (hero.gold >= hero.costOf("soldier")) {
hero.summon("soldier");
}
// Use a for-loop to command each soldier.
var friends = hero.findFriends();
// For-loops have 3 parts, separated by semicolons.
// for(initialization; condition; expression)
// Initialization is done at the start of the first loop.
// The loops continue while condition is true.
for (var friendIndex = 0; friendIndex < friends.length; friendIndex++) {
var friend = friends[friendIndex];
if (friend.type == "soldier") {
var enemy = friend.findNearestEnemy();
// If there's an enemy, command her to attack.
// Otherwise, move her to the right side of the map.
hero.command(friend, "defend", {
x: 90,
y: 44
});
}
}
}
// Protect the cage.
// Put a soldier at each X.
var points = [];
points[0] = {
x: 33,
y: 42
};
points[1] = {
x: 47,
y: 42
};
points[2] = {
x: 33,
y: 26
};
points[3] = {
x: 47,
y: 26
};
// 1. Collect 80 gold.
while (true) {
var item = hero.findNearest(hero.findItems());
if (item && hero.gold < 80) {
hero.move(item.pos);
} else {
break;
}
}
// 2. Build 4 soldiers.
for (var i = 0; i < 4; i++) {
hero.summon("soldier");
}
// 3. Send your soldiers into position.
while (true) {
var friends = hero.findFriends();
for (var j = 0; j < friends.length; j++) {
var point = points[j];
var friend = friends[j];
var enemy = friend.findNearestEnemy();
if (enemy && enemy.team == "ogres" && friend.distanceTo(enemy) < 5) {
// Command friend to attack.
hero.command(friend, "attack", enemy);
} else {
// Command friend to move to point.
hero.command(friend, "move", point);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment