Skip to content

Instantly share code, notes, and snippets.

@a1ip
Last active April 14, 2020 11:04
Show Gist options
  • Save a1ip/2bfd9270c0f838b1cee01b2b7be56551 to your computer and use it in GitHub Desktop.
Save a1ip/2bfd9270c0f838b1cee01b2b7be56551 to your computer and use it in GitHub Desktop.
Some useful tips for CodeCombat players https://git.io/cocotips

Some useful tips for CodeCombat players, mainly taken from the official forum

Finding units stats

All stats can be find here

Finding enemy hero in CodeCombat multiplayer

Enemy hero id can be "Hero Placeholder" or "Hero Placeholder 1".

Examples for finding enemy hero (best to just add these to the top of the code, outside the main loop, and forget):

In Python:

enemyHero = [e for e in self.findEnemies() if e.id in ["Hero Placeholder", "Hero Placeholder 1"]][0]

In JavaScript:

var enemyHero = this.findEnemies().filter(function (e) {
    return e.id === 'Hero Placeholder' || e.id === 'Hero Placeholder 1';
})[0];

In mirror matches (Zero Sum, Ace of Coders) it's easier to just do

In Python:

enemyHero = self.findByType(self.type)[0] or self.findByType('knight')[0]

In JavaScript:

var enemyHero = this.findByType(this.type)[0] || this.findByType('knight')[0];

as both players have the same hero type (or Tharin if their game session is bugged out).

Getting list of a certain type of enemies

If you have good enough glasses to use findByType() method, which allows you to find a unit by type, then for "archer" type in Python it will be:

enemyArchers = self.findByType("archer")

But on the level Clash of Clones the enemy has the same type of units as you, so you would first need to find your enemies, and then identify the different types. Fortunately findByType() method also accepts a second argument to filter the enemies from, so in Python it will be:

enemyArchers = self.findByType("archer", self.findEnemies())

If you don't have the findByType() method, you can do it this way in Python:

enemies = self.findEnemies()
enemyArchers = []
for enemy in enemies:
    if enemy.type == "archer":
        # append it to the list
        enemyArchers.append(enemy)

Or much shorter with Python's list comprehension:

enemyArchers = [enemy for enemy in self.findEnemies() if enemy.type == "archer"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment