Player.js script, the WarriorJS system will run your script in set levels to battle enemies and climb towers in a 2d ASCII art way.
First, create a new directory to hold the project with
mkdir warriorjs-example. Then install the CLI via NPM with
npm install -g @warriorjs/cli. To start up a new session run
warriorjs and then follow the prompts. After it is started it will generate a directory like this:
warriorjs | the-first-steps | - .profile #tracks user progress | - Player.js # file that adds action to the player | - README.md # directions for the current level
To get the directions for what to do next read the
README.md file inside the
the-first-steps directory that was created. Every time that you solve a level the CLI will update the
README.md file with the next instructions for that level. This article will not be duplicating the instructions but will be explaining some other concepts.
Note: This article will show an example of how to solve the tutorial levels. I would recommend that you try and solve the levels on your own before reading farther to get the whole experience.
Level 1 - The Basics
Player.js file with the following, and then running
warriorjs again will run the player through the first level. This is the most simple script for this game as it walks forward until the character hits the exit.
Level 2 - Learn to Fight
if else statement is the simplest way to get started. While this technique can get out of hand (as we will see later in this article) for a basic level such as this its a good starting place.
Level 3 - Time to Heal
The next ability our entrepot explorer gains is the ability to heal himself. In adding to the
if else statement that was started in Level 2, we can add another if else in the middle with a condition for when to start healing with the
rest() command. Order now matters and putting the
rest() command above the
attack() command may mean that an enemy will be attacking while you are healing, which is an issue.
Level 4 - Archers Attack
Focus on movement if taking damage to rush archers. To determine if the character is taking damage we can set up a class variable
this.health and update it at the end of every turn. If
this.health is greater than
warrior.health() (which is the current health of the character on that turn) then the character is taking damage. At this stage, the reaction to taking damage is to rush the archer and start attacking. Later on, this strategy will stop working so it will have to be updated but in this early level its enough.
Level 5 - Free the Captives!
At this level the
if else statement strategy is starting to get out of control. Now we have to not only check for enemies to attack, but also make sure that the character does not attack any friendly captives but instead frees them. The
rescue() command has to come before the
attack() command then so in this example it is the first check-in the logic check.
Level 6 - To the Back
Now direction matters which throws a wrench in our current implementation. Most of the actions can take in an option string parameter of
backward to denote a direction. While we could type out this string all over the code this is a good opportunity to introduce a constant object for these static values. Using constant objects is useful so that you, or other developers if on a team, do not have to remember the capitalization or exact terms.
Introduce constant objects for static values
At this level, the enemies are too spread out to rush the archers when taking damage. To resolve this we can move the
Level 7 - Pivot
While the character can indeed attack in the
backward direction, there is a penalty in doing so. One of the actions
feel() can tell if a character is facing a wall. So at this point, we can use the
pivot() action if the character is facing a wall to turn around making
forward and vice versa.
Level 8 - Bow and Arrows
playTurn(warrior) method that is provided. In level 8 we can add an
isEnemyInSight() method to check if an enemy is anywhere within a 3 space array that the main character can see using the
find(space => space.isUnit()) method to search through the array for an object that is a unit.
Level 9 - Put it Together
The previous level example works for Level 9 but is not ideal as it leaves one of the two captives on the map. This means that while the level is beaten the character does not achieve the highest score possible. In a future article, we will cover "AI" methods for improving level 9.