Collecting Exercises

Collecting Exercises

> Community

This page aims to collect exercises that will then be integrated into the LeekScript tutorial.

Thanks in advance to everyone who will contribute!

You can propose exercises related to Leek Wars as well as exercises that do not.

Keep in mind that you need simple exercises in order to understand a concept. But you can also suggest exercises that require more thought, for those who want a little challenge ;)

Variables

Native functions

interactions with the outside world

Salute your opponent

Write a piece of code that allows, using the say and getName functions, to say "Hi LEEK, nice to fight you today.", with LEEK the name of an enemy leek.

Trick

It is possible to "add" character strings, called string, like this:

var MyString = "A string" + ", then another string"; debug(MyString); // Displays: A string, then another string var MyNumber = 5; MyString = MyNumber + ", this number has been transformed into a character string!"; debug(MyString); // Displays: 5, this number has been transformed into a character string!

I advise you to look at the doc or the wiki for more information on the functions.

Solution

Not that complicated, right? Here is one possible solution:

say("Hi " + getName(getNearestEnemy()) + ", nice to fight you today."; // Attention the "say" function costs 1PT, so it's not the priority ;)

Terms

The essentials for an almost efficient and effective AI

Equip your weapon on turn 1

Create a conditional statement that allows, using setWeapon and getTurn, to equip his weapon only on turn 1.

Boolean operators

Two giant monsters, Frigodzilla and Godzillasagne, try to terrorize the small village of Tiercelieux by wreaking havoc. They arrive and leave in the village each at a specific time of day. The only hope of survival for Tiercelieux resides in the fact that if the two monsters are in the city at the same time, they see in their competitor an adversary and kill each other savagely, giving the village some respite. Knowing the arrival and departure times of each of the monsters, you must find out if there is a crossing time during which the two giants are in the village, in which case the latter is saved! For example, if Frigodzilla arrives in the village at 2 a.m. and leaves at 3 p.m., and Godzillasagne arrives at 10 a.m. and leaves at 6 p.m., then from 10 a.m. to 3 p.m. both monsters are in the village at the same time. Warning! If the two monsters only pass each other, they still engage in combat! That is to say that if one of the monsters arrives at the precise moment when the other leaves they kill each other and spare Tiercelieux.

Loops

Loops for(var i; i<n; i++){}

display all odd numbers from 3 to 99 included in a loop.

foreach loops

Count the bulbs

It can be useful to know how many bulbs there are currently in your team (reminder: if there are already 6, you will not be able to summon them again). So try to count the allied bulbs!

For this, you can use the function getAliveAllies, which gives an array of living allies (bulbs included), and the function isSummon which tests if an entity is a bulb.

Solution to the exercise

var nb_bulbs = 0; //We start by creating a variable that will be used to count the bulbs for(var entity in getAliveAllies()){ // For each ally... if(isSummon(entity){// if this ally is a bulb... nb_bulbs+=1; // we increase the counter by 1. } }

While Loops

The objective is to calculate the maximum height achievable with a certain number of discs, knowing that to stack a disc it must be placed on two other discs located on the lower floor. For example, with three discs it is possible to make an arrangement with a height of 2, with two discs on the ground floor and the third above the first two. With four discs the maximum height remains 2 because the fourth disc cannot stack on the one on the first floor and therefore remains on the ground floor.

More complicated exercises

Visually impaired soldiers

This is the story of a unit of half-blind soldiers. I know, it sounds ridiculous like that, but tell yourself that if the military were unable to aim, there would be a lot less deaths during wars. In short, let's admit, we have a group of soldiers who can only see a meter and a half away, and it's time for the review. The soldiers therefore line up as well as