Giter Club home page Giter Club logo

Comments (10)

olistic avatar olistic commented on May 4, 2024

To get past level four is crucial that your warrior rests, just like in level three. However, now you must check that you are not being attacked before resting. To do so, you can store your warrior's health in a Player's instance property at the end of each turn, and if your warrior is injured but the health at the end of past turn is greater than its current health (which means he's under attack), you should walk/attack instead of rest.

from warriorjs.

DevGuy128 avatar DevGuy128 commented on May 4, 2024

I've been trying to do that, but I can't figure out what the code for it would be.

from warriorjs.

olistic avatar olistic commented on May 4, 2024

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!

from warriorjs.

DevGuy128 avatar DevGuy128 commented on May 4, 2024

Thanks!

from warriorjs.

pawel2105 avatar pawel2105 commented on May 4, 2024

My JS ain't that good, although I'm pretty sure I'm on the right track. I've added my code to this gist so as not to spoil for anyone. Would appreciate some guidance via a tip in the gist (do gist comments send notifications?)

from warriorjs.

olistic avatar olistic commented on May 4, 2024

@pawel2105 I just left a comment in your gist. Good luck!

from warriorjs.

williamgurney avatar williamgurney commented on May 4, 2024

@olistic Hi, really enjoy the game however struggling with the above.
Everything goes okay however my warrior does not rest enough to beat the final sludge, he will rest for one turn then walk the other!

Would appreciate some guidance if possible? It feels like such a small thing.

also added it to this gist

from warriorjs.

olistic avatar olistic commented on May 4, 2024

Hi @williamgurney! I left a comment in your gist, cheers!

from warriorjs.

TomsH1 avatar TomsH1 commented on May 4, 2024
class Player {
  
  constructor() {
    this._health = 20;
  }

  playTurn(warrior) {
    // TODO: Add missing condition
    warrior.think("Am I under attack? "+this.isUnderAttack(warrior.health()));
    if (
      this.isInjured(warrior.health()) && 
      !this.isUnderAttack(warrior.health())) {
      warrior.rest();
    } else if(!warrior.feel().isEmpty()) {
      warrior.attack();
    } else {
      warrior.walk();
    }
    
    this._health = warrior.health(); 
  }

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

  isUnderAttack(health) {
    if(health < this._health){
      return true;
    }
    return false;
    // TODO: Compare this._health with warrior.health()
  }

}

@olistic Based on your example this implementation works. I tried creating a method chained IsEnemy() to feel(), but doing a !warrior.feel().isEmpty() is enough

from warriorjs.

Winder777 avatar Winder777 commented on May 4, 2024

No es muy creativa mi respuesta, pero es otra alternativa:

class Player {

playTurn(warrior) {

if (this.health==null){this.health=0;} 

if (this.health==0) {warrior.walk()}
else if(this.health<=5){warrior.attack()}
else if(this.health<=6){warrior.walk()}
else if(this.health<=10){warrior.attack()}
else if(this.health<=20){warrior.rest()}
else if(this.health<=22){warrior.walk()}
else if(this.health<=30){warrior.attack()}
else if(this.health<=35){warrior.walk()}

this.health = this.health+1
warrior.think(this.health);
}
}

from warriorjs.

Related Issues (20)

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.