Skip to content

Instantly share code, notes, and snippets.

@a1ip
Last active August 18, 2018 19:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a1ip/6d3e7cf0c39b21c25e22 to your computer and use it in GitHub Desktop.
Save a1ip/6d3e7cf0c39b21c25e22 to your computer and use it in GitHub Desktop.
CodeCombat Random Harder Levels solutions http://codecombat.com/play-old

Hunter Triplets

Three soldiers go ogre hunting.

Difficulty: ★★


Code for chooseAction() spell is here:

# This spell runs once per frame.
enemy = @getNearestEnemy()
return unless enemy
@say "Die, " + enemy.id + "!"
@setTarget enemy
if @distanceTo(enemy) > @attackRange
  @setAction "move"
else
  @setAction "attack"

Emphasis on Aim

Chose your targets carefully.

Difficulty: ★★


Code for chooseAction() spell is here:

# The following runs whenever the tower needs
# something to do.

unit = @getNearestCombatant()
if unit and unit.team is "ogres"
  @say 'Perish, ' + unit.id + ' of the ' + unit.team
  @attack unit
else
  @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.

Zone of Danger

Target the ogres swarming into arrow range.

Difficulty: ★★★


Code for chooseAction() spell is here:

# This spell runs once per frame.
nearestEnemy = @getNearestEnemy()

# Until getNearestEnemy() is implemented, nearestEnemy will be null.
if nearestEnemy
  @say "Die, " + nearestEnemy.id + "!"
  @attack nearestEnemy
else

  # Can't find an enemy! Let's just shoot somewhere.
  @say "Suppressing fire!"
  theta = Math.random() * 2 * Math.PI
  @attackXY @pos.x + Math.cos(theta) * @attackRange, @pos.y + Math.sin(theta) * @attackRange
return

Code for distanceTo(target):

# TODO: Find the distance between this.pos and target.pos.
Math.sqrt Math.pow((target.pos.x - @pos.x), 2) + Math.pow((target.pos.y - @pos.y), 2) if target

Code for getNearestEnemy():

enemies = @getEnemies()
nearestEnemy = null
range = @attackRange + 3

# TODO: Find the nearest enemy with the distanceTo method.
#this.getNearestEnemy();

for i in [0...enemies.length]
  enemy = enemies[i]
  dist = @distanceTo(enemy)
  if dist <= range
    continue  if nearestEnemy and dist > @distanceTo(nearestEnemy)
    nearestEnemy = enemy
nearestEnemy

MOLOTOV MEDIC

Tharin must play support in this dungeon battle.

Difficulty: ★★


Code for chooseAction() spell is here:

# Support the troops by throwing health potions
friends = @getFriends()
target = @getNearestFriend()
minHealth = @getNearestFriend().health
for friend in friends
  if friend.health < minHealth
    minHealth = friend.health
    target = friend
@say "Here, #{target.id}!"
@attack target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment