Lists (JavaScript)

Lists

> Tutorial

A list lets you store several values in a specific order.

Creating and using a list

You can create a list with the following syntax:

let list = [1, 2, 3, 4, 5]

The first position in a list is position 0. You access a specific element of the list like this:

list[0] // Gets the first element
list[2] = 10 // Assigns the value 10 to the 3rd element
let a = list[3] + list[4] // adds the 4th and 5th elements and stores the result in the variable a

Many operations are available for working with lists. The most notable ones are:

list.length             // size of the list
list.push(x)            // adds an element x at the end
list.splice(pos, 0, x)  // inserts an element x at a position
list.splice(pos, 1)     // removes the element at a position
list.includes(x)        // checks whether the element x is in the list
list.sort((a, b) => a - b) // sorts the list (ascending numerical order)

Iterating over a list with the for of loop

You can iterate over each value in a list using a simple for loop, like this:

let list = [10, 20, 30, 40, 50]
for (let i = 0; i < list.length; i++) {
	console.log(list[i])
}

But there is a variant of the for loop that makes iterating over each value of a list simpler: the for of loop. The following code is equivalent to the previous one, but more concise:

let list = [10, 20, 30, 40, 50]
for (const element of list) {
	console.log(element)
}

Estimating weapon damage and the "finisher" condition

Our current AI has a problem: when the enemy is almost dead, our leek uses its boosts, heals and shields before using its weapon, when it could have finished the enemy off!

To handle this end-of-fight situation better, we'll estimate the damage we can deal to the enemy, to know whether we can finish it off.

We start by getting the list of our weapon's features with me.weapon.features:

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

Each feature has properties: .type (the effect type), .minValue (minimum value), .maxValue (maximum value)... When the feature is damage, we calculate the final damage:

	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

We add the damage to the total:

		total += finalDamage
	} // end of the if
} // end of the for loop

We finish with a condition: if the total damage is greater than the enemy's life, we'll kill it, so we attack! Otherwise, we wait and use chips:

const totalDamage = total * Math.floor(me.tp / me.weapon.cost) // multiplied by the number of possible uses (TP / weapon cost)
if (totalDamage < enemy.life) {
	// We can't finish it :( heals, boosts, shields
} else {
	// We can finish it! Attack
}

> Tip: you can extract a range from a list with list.slice(2, 8).

> Tip: lists can be nested, with a list inside a list [ [1, 2, 3] ], and you access a list inside a list with list[0][0].

❓ Quiz

What can a list be used for?

Storing several values Sorting values Running instructions in parallel Performing several calculations

Which pieces of code are valid?

let a = [1, 2, 3] a[0] + a[2] let [1, 2, 3] [1, 2, 3].length [ [1, 2], [3, 4], [5, 6] ] list[5][10]

Full AI

const me = Fight.me

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

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

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

// We move toward the enemy
me.moveToward(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)
if (totalDamage < enemy.life) {
	// 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 try to shoot at it 4 times!
for (let i = 0; i < 4; i++) {
	me.useWeapon(enemy)
}