> Tutorial
We have already used several standard functions (setWeapon, moveToward, etc.). You can also create your own custom functions, with the following syntax:
function keywordsum( x, y ){ x + y }The return keyword, which is optional, makes the function return a value. In the previous example, the function takes the parameters x and y and returns their sum: x + y.
canFinish(enemy) functionOur previous code for the "finish" condition on the enemy is quite long and hard to reuse, so we are going to put it in a function.
We will create a custom 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:
Note: remember to specify which weapon you mean so that the calculation works. Also, the function returns true if you have enough action points to fire enough shots to kill the enemy, and false if, on the contrary, the enemy will survive (in which case you might want to do something else).
Now we can use our canFinish function in our main code, which makes it much simpler to write:
Did you know? Moving code that is repeated in several places into a single function is called factoring.
getClosestCellToUseWeapon(enemy) functionYou may have noticed that to use the Machine Gun, or later the Laser, you need to be in line with the enemy.
So you will 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, there is the standard function getCellsToUseWeapon, which returns all the cells from which a weapon can be used on a target. You then have to pick one: the closest!
So we will create a getClosestCellToUseWeapon function and use a for in loop to find the cell with the shortest distance from us:
We use it to replace the moveToward movement:
If you feel comfortable, you can write these functions:
estimateDamage(weapon, enemy): calculates the damage the weapon weapon will deal to enemy.getBestWeapon(enemy): finds, among your weapons, the one that deals the most damage to enemyYou can also:
To get a better view of the map and its cells, feel free to use tactical mode by pressing T during a fight.
The next chapters cover harder topics, so take the time to play around with your functions and the concepts we have seen so far, which are already enough to climb the rankings!
Which function 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 calculates the maximum of a list?
function maximum(list) { var maxValue = -99999 for (var e in list) { if (e > maxValue) { maxValue = e } } return maxValue } function maximum(list) { var maxValue = 99999 for (var e in list) { if (e < maxValue) { maxValue = e } } return maxValue } function maximum(list) { var maxValue = 99999 for (var e in list) { if (e > maxValue) { maxValue = e } } return maxValue } function maximum(list) { var maxValue = -99999 for (var e in list) { if (e < maxValue) { maxValue = e } } return maxValue }
Impossible de charger les données du jeu.
Vérifiez votre connexion et réessayez.