menu
announcement

Spectrum is now read-only. Learn more about the decision in our official announcement.

WarriorJS

An exciting game of programming and Artificial Intelligence

Channels
Team

Level 4 and how health is set

October 5, 2018 at 8:13pm

Level 4 and how health is set

October 5, 2018 at 8:13pm (Edited 3 years ago)
In level 4, it recommends setting the this.health property to warrior.health(), but it doesn't seem like that value updates after a warrior.rest() call when still in the same turn.
From what I figure, action calls only happen after the playTurn method returns. Because of this, I won't see the result of a rest until the start of the next turn. Is this correct?
Code:
class Player {
constructor() {
this.health = null
this.maxHealth = null
this.warrior = null
}
playTurn(warrior) {
this.warrior = warrior
// Set a starting health, so we know what our percentages are.
if (this.health === null) {
this.health = this.maxHealth = warrior.health()
}
if (!this.isBeingAttacked()) {
if (this.isHurt()) {
warrior.rest()
} else {
if (this.seesAnEnemy()) {
warrior.attack()
} else {
warrior.walk()
}
}
} else {
if (this.seesAnEnemy()) {
warrior.attack()
} else {
warrior.walk()
}
}
warrior.think(`I'm setting my health to ${warrior.health()}`)
this.health = warrior.health()
}
isBeingAttacked() {
return this.warrior.health() !== this.health
}
isHurt() {
return this.warrior.health() < this.maxHealth
}
seesAnEnemy() {
return !this.warrior.feel().isEmpty()
}
}
And here is the output of one of the affected turns.
hyuugurt thinks I'm setting my health to 1
hyuugurt rests
hyuugurt receives 2 health, up to 3 health

October 11, 2018 at 1:44pm
Yes your assumption here is correct, actions are not performed until the end of the turn, while senses are resolved immediately.
You can find some related discussion in the following github issue https://github.com/olistic/warriorjs/issues/84
I found it made things clearer to rename that variable to this.lastHealth, since it really is your health on your last turn before any actions resolved.
Side note, you might want to think about modifying the conditional in your isBeingAttackedMethod() to check if the current health is less than your last health, instead of checking for inequality. Otherwise you may get a false positive when healing.