Create your functions (JavaScript)

Create your functions

> Tutorial

Creating a function

We have already used several functions and methods (setWeapon, moveToward, etc.). You can also create your own functions, with the following syntax:

function sum(x, y) {
	return x + y
}

The optional return keyword makes the function return a value. In the previous example, the function takes the parameters x and y and returns their sum: x + y.

Creating a canFinish(enemy) function

Our previous code for the "finish" condition on the enemy is rather long and hard to reuse, so we'll put it in a function.

We'll create our own function, canFinish(enemy), which takes an enemy and returns a boolean telling whether we can finish it off or not.

We copy the finisher code into a function, written like this:

function canFinish(enemy) {
	const effects = me.weapon.features // list of features
	let total = 0 // We'll add up all the damage
	for (const effect of effects) { // we iterate over all the features
		if (effect.type == Effect.DAMAGE) { // if it's damage
			const minDamage = effect.minValue // minimum damage of the weapon
			const boostedDamage = minDamage * (1 + me.strength / 100) // increased by strength
			const finalDamage = boostedDamage * (1 - enemy.relativeShield / 100) - enemy.absoluteShield // reduced by shields
			total += finalDamage
		}
	}
	const totalDamage = total * Math.floor(me.tp / me.weapon.cost) // number of possible uses (TP / weapon cost)
	return totalDamage >= enemy.life
}

Now we can use our canFinish function in our main code, which makes it much simpler:

// [...]

if (!canFinish(enemy)) {
	// We can't finish it :( Boosts, shield and heal
	// [...]
}

Did you know? Moving code that is repeated in several places into a single function is called factoring.

Creating a getClosestCellToUseWeapon(enemy) function

You may have noticed that to use the Machine Gun or, later on, the Laser, you need to be in line with the enemy.

So you'll have to move to a cell from which you can use your weapon, instead of simply moving toward the enemy as with the Pistol.

For this, the me.weaponCells(enemy) method returns all the cells from which you can use your weapon on a target. We'll have to pick one of them: the closest!

So we'll create a getClosestCellToUseWeapon function and use a for of loop to find the cell that is the shortest distance from us:

function getClosestCellToUseWeapon(enemy) {
	let closest = null
	let minDistance = 9999
	for (const cell of me.weaponCells(enemy)) {
		const distance = me.cell.distance(cell)
		if (distance != null && distance < minDistance) {
			closest = cell
			minDistance = distance
		}
	}
	return closest
}

We use it to replace the movement:

// We move toward the closest cell from which we can use the weapon
const cell = getClosestCellToUseWeapon(enemy)
me.moveToward(cell)

To get a better view of the terrain and its cells, feel free to use tactical mode by pressing T during a fight.

In the next chapters, we'll tackle harder topics, so take the time to play around with your functions and the concepts we've covered so far: they're already enough to climb the ranking!

❓ Quiz

Which of these functions correctly returns the square of a number?

function square(x) { return x * x } function square(x) { x * x } function square() { return x * x } fonction square(x) { return x * x }

Which function computes the maximum of a list?

function maximum(list) { let maxi = -99999 for (const e of list) { if (e > maxi) { maxi = e } } return maxi } function maximum(list) { let maxi = 99999 for (const e of list) { if (e &lt; maxi) { maxi = e } } return maxi } function maximum(list) { let maxi = 99999 for (const e of list) { if (e > maxi) { maxi = e } } return maxi } function maximum(list) { let maxi = -99999 for (const e of list) { if (e &lt; maxi) { maxi = e } } return maxi }

Full AI

const me = Fight.me

// We take the machine gun if it is not equipped
if (me.weapon == null) {
	me.setWeapon(Weapon.machineGun)
}

// We get the nearest enemy
const enemy = Fight.getNearestEnemy()

// We greet our opponent
if (Fight.turn == 1) {
	me.say("Hi " + enemy.name + "!")
}

if (!canFinish(enemy)) {
	// We can't finish it :( Boosts, shield and heal
	me.useChip(Chip.motivation)
	me.useChip(Chip.protein)
	me.useChip(Chip.helmet)

	// We heal ourselves if our life is below our max life.
	if (me.life < me.maxLife) {
		me.useChip(Chip.bandage)
	}
}

// We move toward the closest cell from which we can use the weapon
const cell = getClosestCellToUseWeapon(enemy)
me.moveToward(cell)

// We try to shoot at it 4 times!
for (let i = 0; i < 4; i++) {
	me.useWeapon(enemy)
}

///////////////
// Functions //
///////////////

function canFinish(enemy) {
	const effects = me.weapon.features // list of features
	let total = 0 // We'll add up all the damage
	for (const effect of effects) { // we iterate over all the features
		if (effect.type == Effect.DAMAGE) { // if it's damage
			const minDamage = effect.minValue // minimum damage of the weapon
			const boostedDamage = minDamage * (1 + me.strength / 100) // increased by strength
			const finalDamage = boostedDamage * (1 - enemy.relativeShield / 100) - enemy.absoluteShield // reduced by shields
			total += finalDamage
		}
	}
	const totalDamage = total * Math.floor(me.tp / me.weapon.cost) // number of possible uses (TP / weapon cost)
	return totalDamage >= enemy.life
}

function getClosestCellToUseWeapon(enemy) {
	let closest = null
	let minDistance = 9999
	for (const cell of me.weaponCells(enemy)) {
		const distance = me.cell.distance(cell)
		if (distance != null && distance < minDistance) {
			closest = cell
			minDistance = distance
		}
	}
	return closest
}