Pretty simple: I wrothe the following if/else statement:
if (cr1.ins <= cr2.ins) {
console.log("[ROUND 1] Creature 1 (#" + cr1.num + ") is attacking first.");
cr2.hlt = cr2.hlt - (cr1.atk + cr2.ins);
console.log("[HIT] Creature #" + cr2.num + " health reduced to " + cr2.hlt);
if (cr2.hlt <= 0) {
console.log("[DEATH] Creature #" + cr2.num + " Eliminated");
remove(cr2);
} else {
console.log("[ROUND 2] Creature 2 (#" + cr2.num + ") is attacking second.");
cr1.hlt = cr1.hlt - (cr2.atk + cr1.ins);
console.log("[HIT] Creature #" + cr1.num + " health reduced to " + cr1.hlt);
if (cr1.hlt <= 0) {
console.log("[DEATH] Creature #" + cr1.num + " Eliminated");
remove(cr1);
}
}
} else {
cr1.hlt = cr1.hlt - (cr2.atk + cr1.ins);
console.log("[ROUND 1] Creature 2 (#" + cr2.num + ") is going first.");
console.log("[HIT] Creature #" + cr1.num + " health reduced to " + cr1.hlt);
if (cr1.hlt <= 0) {
console.log("[DEATH] Creature #" + cr1.num + " Eliminated");
remove(cr1);
} else {
console.log("[ROUND 2] Creature 1 (#" + cr1.num + ") is going second.");
cr2.hlt = cr2.hlt - (cr1.atk + cr2.ins);
console.log("[HIT] Creature #" + cr2.num + " health reduced to " + cr2.hlt);
if (cr2.hlt <= 0) {
console.log("[DEATH] Creature #" + cr2.num + " Eliminated");
remove(cr2);
}
}
}
I know there's probably a better way to do this, as the code in else{ } is basically the same as if{ } with some variable name changes, so, any suggestions for changes or refactoring? I'd like to improve readability and speed while accomplishing the same task as it performs currently.
Aucun commentaire:
Enregistrer un commentaire