Useful code clips

Number 1: Object to String converter:
Python:


def strObj (obj):
    str = "{"
    for t in obj: str = str + t + ": " + obj[t] + ", "
    return str.substring(0, str.length - 2) + "}"

Javascript:


function strObj (obj) {
    let str = "{";
    for (let t in obj) str = str + t + ": " + obj[t] + ", ";
    return str.substring(0, str.length - 2) + "}";
}

Number 2: hero type detector (useful in dueling grounds or stuff like that)
Python:
I’m too lazy to type it, just use a converter, lol.
Javascript:


hero.findOpponent = function () {
    for (let enemy of hero.findEnemies()) if (enemy.id.includes("Hero Placeholder")) return enemy;
    return undefined;
};
hero.findData = function (u) {
    let unit = u;
    var unitData = {hero: null, type: null};
    switch (unit.type) {
        case "assassin": 
            unitData.hero = "Ritic";
            break;
        case "intellect": 
            unitData.hero = "Armando";
            break;
        case "librarian": 
            unitData.hero = "Hushbaum";
            break;
        case "trapper": 
            unitData.hero = "Senick";
            break;
        case "knight": 
            unitData.hero = "Tharin";
            break;
        case "sorcerer": 
            unitData.hero = "Pender";
            break;
        case "goliath": 
            unitData.hero = "Okar";
            break;
        case "duelist": 
            unitData.hero = "Alejandro";
            break;
        case "champion": 
            unitData.hero = "Ida";
            break;
        case "necromancer": 
            unitData.hero = "Nalfar";
            break;
        case "captain": 
            unitData.hero = "Anya";
            break;
        case "samurai": 
            unitData.hero = "Hattori";
            break;
        case "guardian": 
            unitData.hero = "Illia";
            break;
        case "pixie": 
            unitData.hero = "Zana";
            break;
        case "forest-archer": 
            unitData.hero = "Naria";
            break;
        case "potion-master": 
            unitData.hero = "Omarn";
            break;
        case "master-wizard": 
            unitData.hero = "Usara";
            break;
    }
    unit = unitData.hero;
    if (unit === "Illia" || unit === "Anya" || unit === "Tharin" || unit === "Okar" || unit === "Alejandro" || unit === "Ida" || unit === "Hattori" || unit === "Armando" || unit === "Arryn" || unit === "Gordon") unitData.type = "warrior";
    if (unit === "Zana" || unit === "Senick" || unit === "Ritic" || unit === "Naria" || unit === "Amara") unitData.type = "ranger";
    if (unit === "Usara" || unit === "Hushbaum" || unit === "Nalfar" || unit === "Pender" || unit === "Omarn") unitData.type = "wizard";
    return unitData;
    // IKR, I had to write all of that. xD
};

If these simple code clips that don’t solve any levels aren’t allowed, please let me know. :slight_smile:
Also, just a side note: don’t ask how I formatted Javascript code properly :slight_smile:

Lol, funny thing to add.

2 Likes

Number 1: Object to String converter:
CodeCombat exposes JSON.stringify

Number 2: hero type detector
Useful! Perhaps look up the name in an object rather than a switch statement?

1 Like

It creates a different string though, the conversion mine does is easier to read. (in my opinion)

Good idea.

Is this better?


hero.findOpponent = function () {
    for (let enemy of hero.findEnemies()) if (enemy.id.includes("Hero Placeholder")) return enemy;
    return undefined;
};
hero.findData = function (u) {
    let types = {assassin: "Ritic", intellectual: "Armando/Ned", knight: "Tharin", librarian: "Hushbaum", trapper: "Senick", sorcerer: "Pender", goliath: "Okar", duelist: "Alejandro", champion: "Ida", necromancer: "Nalfar", captain: "Anya", samurai: "Hattori", guardian: "Illia", pixie: "Zana"};
    let uData = {hero: types[u.type], type: null};
    if (!(uData.hero)) {
        switch (u.type) {
            case "forest-archer": return {hero: "Naria", type: "Ranger"};
            case "potion-master": return {hero: "Omarn", type: "Wizard"};
            case "master-wizard": return {hero: "Usara", type: "Wizard"};
        }
    }
    u = uData.hero;
    if (u === "Illia" || u === "Anya" || u === "Tharin" || u === "Okar" || u === "Alejandro" || u === "Ida" || u === "Hattori" || u === "Arryn" || u === "Gordon" || u === "Armando/Ned") uData.type = "Warrior";
    if (u === "Zana" || u === "Senick" || u === "Ritic" || u === "Amara") uData.type = "Ranger";
    if (u === "Hushbaum" || u === "Nalfar" || u === "Pender") uData.type = "Wizard";
    return uData;
};

Sorry about the delay :sweat_smile: I had to go to school…

Number 3: distance between points
Python:

def distanceBetweenPoints (p1, p2):
    return Math.sqrt(Math.pow(Math.abs(p1.x-p2.x), 2) + Math.pow(Math.abs(p1.y-p2.y), 2));

Javascript:


hero.distanceBetweenPoints = (p1, p2) => Math.sqrt(Math.pow(Math.abs(p1.x-p2.x), 2) + Math.pow(Math.abs(p1.y-p2.y), 2));

Number 3: distance between points
Assuming you’re working with vectors (such as unit positions), you can use CodeCombat’s Vector.distance. If you want to define it manually, the Math.abs calls are redundant as squaring always results in a nonnegative number.

1 Like

Heh, I forgot to add a String to Object converter (please don’t tell me that there is a JSON.parse() method, I already know, Hydrobolic)
Javascript:


function objStr (str) {
    let rO = {};
    for (let sOP of str.split(", ")) {
        let sOD = (sOP.replace("{", "").replace("}", "")).split(": ");
        rO[sOD[0]] = sOD[1];
    }
    return rO;
}

Python:

def objStr (str):
    rO = {};
    for sOP in str.split(", "):
        sOD = (sOP.replace("{", "").replace("}", "")).split(": ");
        rO[sOD[0]] = sOD[1];
    return rO;

Also, any object with more than 1 dimension or a comma in the string breaks my program… maybe I could use JSON.parse() and JSON.stringify() to solve it. >:)

Careful: you’re referencing an undefined variable obj that probably existed out of scope in your testing.
Also, be wary that you lose data types with strObj, so numbers are converted to strings in objStr and may have unexpected results down the line.

Oops, yeah, I changed “obj” to “str”, but I forgot to change it in the function too. :grimacing:

Fixed. (20 chars verifies)

Armando’s type is intellectual not intellect

This was 10 months ago, so maybe it updated now