olistic / warriorjs

🏰 An exciting game of programming and Artificial Intelligence
https://warriorjs.com
MIT License
9.41k stars 488 forks source link

where did constructor() come from and what is it? How does it work and what does it mean? I'm not able to learn anything here #264

Closed Levi-369 closed 1 year ago

Levi-369 commented 1 year ago
          I could give you a few pointers, but not the complete solution. After all, this game is intended to learn, and you won't learn anything if I do.
class Player {
  constructor() {
    this._health = 20;
  }

  playTurn(warrior) {
    // TODO: Add missing condition
    if () {
      warrior.rest();
    } else {
      if (warrior.feel().isEnemy()) {
        warrior.attack();
      } else {
        warrior.walk();
      }
    }

    // TODO: Update this._health with warrior.health
  }

  isInjured(warrior) {
    // TODO: Return false if warrior health is less than 20
  }

  isUnderAttack(warrior) {
    // TODO: Compare this._health with warrior.health()
  }
}

You can call the newly defined methods isInjured and isUnderAttack (and any other method you create in the future) from the playTurn method like this:

this.isInjured(warrior)

Good luck!

Originally posted by @olistic in https://github.com/olistic/warriorjs/issues/9#issuecomment-104427025

Levi-369 commented 1 year ago

I don't know how GitHub works or how to delete this... I just wanted to ask privately and just seen this was answered in 2015