The War Room

Melaka Empire Defense

Write real-time JavaScript to command Rentaka swivel guns and Meriam cannons.

Preferred: define cannon1(radar, artillery, command), cannon2(...), cannon3(...)
radar.getShips() -> [{ id, type, distance, hp, maxHp }]
artillery.fireRentaka(shipId) -> 10 damage, max 3 bullets per cannon per tick
artillery.fireMeriam(shipId) -> 50 damage, max 1 shot per cannon per tick, 3 tick cooldown
command.log(message) -> print to terminal
Learning Guide

Simple Command Call

Call one API method to make one cannon action.

artillery.fireRentaka(ship.id);
command.log("Rentaka fired.");

For Loop

Repeat an action. Rentaka allows 3 bullets per cannon per tick.

for (let bullet = 0; bullet < 3; bullet++) {
  artillery.fireRentaka(ship.id);
}

If Else

Choose different weapons based on ship type or HP.

if (ship.type === "Colonizer Galleon") {
  artillery.fireMeriam(ship.id);
} else {
  artillery.fireRentaka(ship.id);
}

Recursion

A function can call itself, but always include a base case.

const countShips = (ships, index = 0) => {
  if (index >= ships.length) return 0;
  return 1 + countShips(ships, index + 1);
};

Best Practice

  • - Use early returns when there is no target.
  • - Keep repeated logic in helper functions.
  • - Avoid infinite loops like while (true).
  • - Prefer clear names such as nearestShip and weakestShip.
Loading...

Strait of Melaka Radar

Fort at X = 0. Hostile ships enter from X = 1000.

Fort HP

100

Score

0

Wave

1/10

1/5 ships

Reloading

C1
Rentaka x3Meriam Ready
C2
Rentaka x3Meriam Ready
C3
Rentaka x3Meriam Ready

Melaka command console online. Write JavaScript to defend the fort.

Slot 1
Slot 2
Slot 3
Fort
Junk

50/50 HP | 1000m