Coder Social home page Coder Social logo

js-poo-heracles-labour-1's Introduction

The labours of Heracles - First of the labours: the Nemean lion

Prerequisite: clone this repository.

Groundwork

Heracles must defeat the fierce Nemean lion, known for its impenetrable skin, making it very difficult to hurt. Before starting their quest, every hero should prepare a little:

In the empty src/Fighter.js file, create:

  • a constant MAX_LIFE = 100: fighters have 100 max life points

  • a Fighter class with the properties:

    • name: name of the fighter.
    • strength: will be used to calculate the damage points during an attack.
    • dexterity: will be used to calculate the defense points that will limit the damage received.
    • life: initialized to MAX_LIFE, so they all start with 100 life points.

There would potentially be plenty of other things to add, but that's a good start!

Then, in the supplied index.js file, create two instances of the Fighter class, for:

  • ๐Ÿง” Heracles, strength 20, dexterity 6

  • ๐Ÿฆ Nemean Lion, strength 11, dexterity 13

To have a nice look even if you're working in the console, you can add an icon in the name string (https://emojipedia.org/). You can also have fun varying the values, but the ones provided will give you relatively balanced fights.

๐Ÿ’กHINT: a constructor will be needed in your class to initialize these properties with different values for Heracles and the Lion.

๐Ÿ’กHINT: don't forget to require your Fighter.js file if you want to be able to use it from your index.js file.

After both objects have been instantiated, display the name and life ๐Ÿ’™ (you should start at 100) of each of the two fighters to make sure everything is working fine. For now, you'll be using JS only in CLI mode. To do so, just type: node index.js in your terminal.

FIGHT !

Heracles warmed up a bit, tightened his muscles, crushed some rocks with his bare hands. He's ready to do battle!

Create a fight () method that will allow you to attack the enemy. To determine who you are going to attack, the fight () method will take another Fighter object as a parameter! In the body of your method, you will first calculate the number of damage points of the attacker:

  • The fighter will hit with more or less success each time, the number of points of damage that the ** attacker ** does will therefore be a random number between 1 and the strength of the fighter (use Math.random () (Documentation, and if needed some advices from this article)

  • But the attacked can defend and dodge! You will therefore mitigate the damage by subtracting the damage from the dexterity of the attacked (without never going below zero)

  • Once the damage is calculated, decrease the number of life points of the attacked by the value thus obtained. Be careful, the life of a fighter cannot fall below zero, also remember to check that

In summary :

attacked's new life points = attacked's current life points - (attacker's damage - attacked's dexterity)

knowing that (attacker's damage - attacked's dexterity) must not be negative.

There you go, your class is ready, you just have to use it!

๐Ÿ—ก๏ธ There can be only one

Heracles enters the cave, the lion is there, his red eyes pointed towards the intruder. The fight is imminent:

  1. In the index.js file, create a loop for performing a fight to the death! As long as one of the two enemies has a health > 0, the combat therefore continues.

  2. In each "round" ๐Ÿ•›, Heracles attacks the lion, then the lion attacks Heracles. You have to display the round number, who attacks who, and the remaining life points.

  3. When one of the two is defeated, you must then display the winner ๐Ÿ† and the loser ๐Ÿ’€. The screenshot below gives you a summary of what to expect.

instructions

๐ŸŽ BONUS: create a new isAlive () method to help you optimize the code.

Conclusion

Once you're done, feel free to tweak the code and add new features. You will be given a "clean" code at the start of the next workshop.

js-poo-heracles-labour-1's People

Contributors

rocambille avatar ericpradier avatar jujuck avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.