Lists

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:

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

Many functions for working with lists are available in the "Lists" section of the Documentation. The most notable ones are:

Iterating over a list with the for in loop

You can iterate over each value of a list with a simple for loop, like this:

But there's a variant of the for loop that makes iterating over each value of a list simpler: the for in loop. The following code does the same thing as the previous one, but more concisely:

Estimating a weapon's damage and the "finisher" condition

Our current AI has a problem you may have noticed: when the enemy is almost dead, our leek uses its boosts, heals and shields before using its weapon, even though 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 find out whether we can finish it off.

We start with the getWeaponEffects function, which gives the list of a weapon's effects:

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

We add the damage to the total:

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

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

> Tip: you can nest lists, with a list inside a list [ [1, 2, 3] ], and 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 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