Lists

Lists

> Tutorial

A list is used to store several values in a specific order.

Creating and using a list

You can create a list with the following syntax:

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

Many functions are available for handling lists, in the "Lists" part of the Documentation. The most notable are:

Iterating over a list with the for in loop

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

But there is a variant of the for loop which makes it possible to iterate over each of the values of a list more simply, the for in loop. The following code is equivalent to the previous one but is more concise:

Weapon damage rating and finisher condition

Our current AI has a problem that you may have noticed: when the enemy is almost dead, our leek will use its boosts, heals, shields before using its weapon, whereas it could finish the enemy!

To improve this situation at the end of the fight, we will estimate the damage that we can inflict on the enemy to know if we can finish it.

We start by using the getWeaponEffects function which lists the effects of a weapon:

An effect is itself a list containing the following 6 elements: [type, min, max, turns, targets, modifiers]. The final damage for this effect is calculated:

Add the damage to the total:

We end with a condition: if the total damage is greater than the life of the enemy, we will kill him: we attack! Otherwise, we wait with chips:

> Tip: you can access a range of a list using the list[2:8] syntax.

> Tip: you can nest lists: list in a list [ [1, 2, 3] ] and access to a list in a list: liste[0][0]

❓ Quiz

What is a list for?

Store multiple values Sort values Execute instructions in parallel Perform multiple calculations

Which codes are valid?

var a = [1, 2, 3] a[0] + a[2] var [a] = [1, 2] var[1, 2, 3] count([1, 2, 3]) [ [1, 2], [3, 4], [5, 6] ] list[5][10]

Full AI