Skip to content

Instantly share code, notes, and snippets.

@GantMan
Last active August 6, 2021 01:09
Show Gist options
  • Save GantMan/9100477 to your computer and use it in GitHub Desktop.
Save GantMan/9100477 to your computer and use it in GitHub Desktop.
Gist of code for CodeCombat - http://codecombat.com/

CodeCombat.com Solutions

Here's a simple gist of my Code Combat solutions. May it be useful to anyone who is teaching, playing, or learning Javascript.

Code Combat Site

// Tharin is a fierce melee fighter with shield, warcry, and terrify skills.
// Shield gives him mighty defense while he is defending, letting him take one-third damage.
// Warcry gives all friendly units within 10m 30% haste for 5s, every 10s.
// Once per match, terrify can send all enemies within 20m running in fear for 4s.
var friends = this.getFriends();
var enemies = this.getEnemies();
if (enemies.length === 0) return; // Do nothing if all enemies are dead.
var enemy = this.getNearest(enemies);
if (!this.getCooldown('warcry') && friends.length > 1) {
this.warcry();
}
else {
this.attack(enemy);
}
if (this.health < (this.maxHealth / 4)) {
this.shield();
}
// You can also command your troops with this.say():
//this.say("Defend!", {targetPos: {x: 30, y: 30}}));
//this.say("Attack!", {target: enemy});
//this.say("Move!", {targetPos: {x: 40, y: 40});
this.moveDown();
this.moveDown();
this.moveRight();
this.moveUp();
this.moveUp();
this.moveRight();
this.moveRight();
this.moveDown();
this.attackNearbyEnemy();
// Tharin might consider taking a detour after line 3.
this.moveUp();
this.moveRight();
this.moveLeft();
this.moveUp();
this.attackNearbyEnemy();
this.moveRight();
this.attackNearbyEnemy();
this.moveRight();
this.moveDown();
this.moveUp();
this.moveRight();
this.attackNearbyEnemy();
this.moveRight();
this.bustDownDoor();
// Delete the "//" in front of each line below.
this.moveRight();
this.say("Hey there!");
this.moveLeft();
this.moveLeft();
this.say("Attack!");
// Now get Phoebe to follow through the dungeon.
this.moveRight();
this.say("Follow me.");
this.moveRight();
this.moveRight();
this.moveUp();
this.moveUp();
this.moveRight();
this.moveRight();
this.bustDownDoor();
// Delete the "//" in front of each line below.
this.moveRight();
this.say("Hey there!");
this.moveLeft();
this.moveLeft();
this.say("Attack!");
// Now get Phoebe to follow through the dungeon.
this.moveRight();
this.say("Follow me.");
this.moveRight();
this.moveRight();
this.moveUp();
this.moveUp();
this.moveRight();
this.say("Hey!");
// Lure the ogre right into your arrow trap
// by saying a few more things at him.
// Anything works!
this.say("Hey silly head!");
this.say("Cmon you big bully!");
this.say("I'll show you!");
this.moveXY(50, 16);
this.moveXY(63, 20);
this.moveXY(70, 10); // This is a safe spot.
this.say("I can lure them in here.");
// Run your soldier out where the ogres can hear you
this.moveXY(54,34);
// Say something!
this.say("Hey you mean heads!");
// Then run back behind the arrow towers for safety
this.moveXY(70, 10);
this.moveXY(49, 66);
this.moveXY(60, 63);
this.moveXY(75, 63);
this.say("Hail, friends!");
// Saying anything with "follow" in it will get nearby soldiers
// to follow you
this.say("Follow me comrads!");
// Saying anything with "attack" will send nearby troops into
// a battle mode.
this.say("It's time to ATTACK!");
// Make sure Tharin is safe!
this.moveXY(66, 44);
this.say("Come get some!");
this.moveXY(75, 63);
this.moveXY(30, 26); // Move into range
this.attackXY(46, 5); // Shoot once in the middle of the ogres
// If they survive a hit, ogres retaliate!
// Larger ogres take more than one hit...
// So, position and aim carefully.
this.moveXY(44,43);
this.attackXY(50,63);
this.attackXY(48,56);
this.attackXY(69,54);
this.attackXY(57,47);
this.say("Let's plan ahead!");
//We must sort the soldiers in order ,from strongest to weakest
//Use the this.getFriends() to get all the soldiers
//and compare them with the .scaleFactor property
//ex : if(friend[1].scaleFactor < friend[5].scaleFactor) doSmth();
//also to swap them on the battlefield use smth like
//this.say("<random>",[friend[2].id,friend[4].id]);
//after they are sorted just say "ATTACK!"
friends = this.getFriends(); // this never updates, so we have to handle this manually
var swapped;
do {
swapped = false;
for (var i = 0; i < friends.length - 1; i++)
{
if (friends[i].scaleFactor > friends[i + 1].scaleFactor)
{
this.say("Switch'em Up! " + friends[i] + " AND " + friends[i+1] + " with i == " + i + " And friends == " + friends, [
friends[i].id,
friends[i + 1].id
]);
// update the order of friends
var temp = friends[i];
friends[i] = friends[i+1];
friends[i+1] = temp;
swapped = true;
}
}
} while (swapped);
// FINALLY!
this.say("ATTACK!");
// The following runs whenever the tower needs
// something to do.
var unit = this.getNearestCombatant();
team = (unit == null) ? ("none") : (unit.team);
if (team == "ogres") {
this.say('Perish, ' + unit.id + ' of the ' + unit.team);
this.attack(unit);
}
else {
this.say('All clear.');
}
// This tower is too bloodthirsty!
// Have it check the unit's team before opening fire.
// There are three teams: "ogres", "allies", and "humans".
// If you need help, press Guide at the top.
// This spell runs once per frame.
var enemy = this.getNearestEnemy();
if (!enemy) return;
this.say("Die, " + enemy.id + "!");
this.setTarget(enemy);
if (this.distanceTo(enemy) < this.attackRange)
{
this.setAction("attack");
}
else
{
this.setAction("move"); // Charge!
}
@Xotabu4
Copy link

Xotabu4 commented Jan 23, 2015

Wow, looks cool!

@sukmoi
Copy link

sukmoi commented Aug 5, 2021

gg but why, Codecombat is to teach a skill if you cheat in it it would be pointless.

@GantMan
Copy link
Author

GantMan commented Aug 6, 2021

I made this like 6 years ago for a teaching workshop to help check answers quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment