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

I'm confused,where do I set this.health on lesson four??

August 2, 2019 at 3:09am

I'm confused,where do I set this.health on lesson four??

August 2, 2019 at 3:09am (Edited 2 years ago)
No matter where I put warrior.think("blah blah blah: " + warrior.health()); in my code it comes out with the same value!
This is my code:
//warrior.think("Health BEFORE class Player???????: " + warrior.health());
class Player {
/**
* Plays a warrior turn.
*
* @param {Warrior} warrior The warrior.
*/
//warrior.think("Health before playTurn: " + warrior.health());
playTurn(warrior) {
// Cool code goes here
warrior.think("health before turn: " + warrior.health())
if (warrior.feel().isEmpty()) {
if (warrior.health() < warrior.maxHealth()) {
warrior.think("Health before rest: " + warrior.health());
warrior.rest();
warrior.think("Health after rest: " + warrior.health());
} else {
warrior.think("Health before walk: " + warrior.health());
warrior.walk();
warrior.think("Health after walk: " + warrior.health());
}
} else {
warrior.think("Health before attack: " + warrior.health());
warrior.attack();
warrior.think("Health after attack: " + warrior.health());
}
warrior.think("health after turn: " + warrior.health());
}
//warrior.think("Health after playTurn: " + warrior.health());
}
//warrior.health("Health after class Player???: " + warrior.health());
And this is the output, all the warrior.think() outputs are the same:
turn 001
Elon thinks health before turn: 20
Elon thinks Health before walk: 20
Elon thinks Health after walk: 20
Elon thinks health after turn: 20
Elon walks forward
turn 002
Elon thinks health before turn: 20
Elon thinks Health before attack: 20
Elon thinks Health after attack: 20
Elon thinks health after turn: 20
Elon attacks forward and hits Thick Sludge
Thick Sludge takes 5 damage, 19 health power left
Thick Sludge attacks forward and hits Elon
Elon takes 3 damage, 17 health power left
turn 003
Elon thinks health before turn: 17
Elon thinks Health before attack: 17
Elon thinks Health after attack: 17
Elon thinks health after turn: 17
Elon attacks forward and hits Thick Sludge
Thick Sludge takes 5 damage, 14 health power left
Thick Sludge attacks forward and hits Elon
Elon takes 3 damage, 14 health power left
turn 004
Elon thinks health before turn: 14
Elon thinks Health before attack: 14
Elon thinks Health after attack: 14
Elon thinks health after turn: 14
Elon attacks forward and hits Thick Sludge
Thick Sludge takes 5 damage, 9 health power left
Thick Sludge attacks forward and hits Elon
Elon takes 3 damage, 11 health power left
turn 005
Elon thinks health before turn: 11
Elon thinks Health before attack: 11
Elon thinks Health after attack: 11
Elon thinks health after turn: 11
Elon attacks forward and hits Thick Sludge
Thick Sludge takes 5 damage, 4 health power left
Thick Sludge attacks forward and hits Elon
Elon takes 3 damage, 8 health power left
turn 006
Elon thinks health before turn: 8
Elon thinks Health before attack: 8
Elon thinks Health after attack: 8
Elon thinks health after turn: 8
Elon attacks forward and hits Thick Sludge
Thick Sludge takes 5 damage, 0 health power left
Thick Sludge dies
Elon earns 24 points
turn 007
Elon thinks health before turn: 8
Elon thinks Health before rest: 8
Elon thinks Health after rest: 8
Elon thinks health after turn: 8
Elon rests
Elon receives 2 health, up to 10 health
Archer shoots forward and hits Elon
Elon takes 3 damage, 7 health power left
turn 008
Elon thinks health before turn: 7
Elon thinks Health before rest: 7
Elon thinks Health after rest: 7
Elon thinks health after turn: 7
Elon rests
Elon receives 2 health, up to 9 health
Archer shoots forward and hits Elon
Elon takes 3 damage, 6 health power left
turn 009
Elon thinks health before turn: 6
Elon thinks Health before rest: 6
Elon thinks Health after rest: 6
Elon thinks health after turn: 6
Elon rests
Elon receives 2 health, up to 8 health
Archer shoots forward and hits Elon
Elon takes 3 damage, 5 health power left
turn 010
Elon thinks health before turn: 5
Elon thinks Health before rest: 5
Elon thinks Health after rest: 5
Elon thinks health after turn: 5
Elon rests
Elon receives 2 health, up to 7 health
Archer shoots forward and hits Elon
Elon takes 3 damage, 4 health power left
turn 011
Elon thinks health before turn: 4
Elon thinks Health before rest: 4
Elon thinks Health after rest: 4
Elon thinks health after turn: 4
Elon rests
Elon receives 2 health, up to 6 health
Archer shoots forward and hits Elon
Elon takes 3 damage, 3 health power left
turn 012
Elon thinks health before turn: 3
Elon thinks Health before rest: 3
Elon thinks Health after rest: 3
Elon thinks health after turn: 3
Elon rests
Elon receives 2 health, up to 5 health
Archer shoots forward and hits Elon
Elon takes 3 damage, 2 health power left
turn 013
Elon thinks health before turn: 2
Elon thinks Health before rest: 2
Elon thinks Health after rest: 2
Elon thinks health after turn: 2
Elon rests
Elon receives 2 health, up to 4 health
Archer shoots forward and hits Elon
Elon takes 3 damage, 1 health power left
turn 014
Elon thinks health before turn: 1
Elon thinks Health before rest: 1
Elon thinks Health after rest: 1
Elon thinks health after turn: 1
Elon rests
Elon receives 2 health, up to 3 health
Archer shoots forward and hits Elon
Elon takes 3 damage, 0 health power left
Elon dies
THEY'RE ALL THE SAME, EVERY TIME!! So where the heck am I supposed to set this.health so that it'll be greater than warrior.health() when the warrior gets damaged???

August 7, 2019 at 7:13pm
Hey Elon, you need to create your own variable in the player class to keep track of your health outside of warrior.Health() and check against that variable. In the ReadMe it states you need to create this variable.
like-fill
1

August 11, 2019 at 2:02pm
Alright, I think I figured it out. Thanks!