Comments (10)
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.
I've been trying to do that, but I can't figure out what the code for it would be.
from warriorjs.
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.
Thanks!
from warriorjs.
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.
@pawel2105 I just left a comment in your gist. Good luck!
from warriorjs.
@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.
Hi @williamgurney! I left a comment in your gist, cheers!
from warriorjs.
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.
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)
- Any plans on i18n? HOT 1
- Documentation links are broken in many places HOT 1
- access to newer abilities in previous levels HOT 3
- Connect WarriorJS CLI with warriorjs.com HOT 2
- Add ability to get warrior's max health HOT 3
- brief description on each release HOT 3
- @warriorjs/tower-tick-tick-boom warrior haven't got the "pivot" ability and bashing 'backward' forcedly
- Should the warrior receive more damage when attacked from behind? HOT 1
- Add retro sounds HOT 2
- translate Korean HOT 2
- Korean Translation HOT 3
- Can't access functions from a Unit object
- Watch for Player.js changes and run
- I cannot sign in or create an account for the online platform HOT 2
- When I 'Run' the game after defeated, the state isn't refreshed unless I reload the page for several times HOT 1
- rescue('backward') is undefined HOT 3
- This code is wierdly running HOT 4
- 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 HOT 1
- Error: No files matching the pattern "packages/**/src" were found
- Please help me with the algorithm of moves leading to victory in this 9th level on the map "Tick, Tick... Boom!"!
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from warriorjs.