-
Notifications
You must be signed in to change notification settings - Fork 4.1k
first eterations #4479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
first eterations #4479
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,93 @@ | ||
| 'use strict'; | ||
|
|
||
| class Animal { | ||
| // write your code here | ||
| /** | ||
| static createAliveAnimalList() { | ||
| const alive = []; | ||
| const emptyslots = []; | ||
|
|
||
| const list = alive; | ||
|
|
||
| list.add = (animal) => { | ||
| if (emptyslots.length === 0) { | ||
| animal.index = alive.push(animal) - 1; | ||
| } else { | ||
| const index = emptyslots.pop(); | ||
|
|
||
| animal.index = index; | ||
| alive[index] = animal; | ||
| } | ||
| }; | ||
|
|
||
| list.remove = (animal) => { | ||
| alive[animal.index] = null; | ||
| emptyslots.push(animal.index); | ||
| }; | ||
|
|
||
| return list; | ||
| } | ||
|
|
||
| static alive = Animal.createAliveAnimalList(); | ||
| */ | ||
|
|
||
| static alive = []; | ||
| static #emptyslots = []; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why such a variable name? |
||
|
|
||
| constructor(name, health = 100) { | ||
| this.name = name; | ||
| this._health = health; | ||
| this.addToAlive(); | ||
| } | ||
|
|
||
| addToAlive() { | ||
| if (Animal.#emptyslots.length > 0) { | ||
| this.index = Animal.#emptyslots.pop(); | ||
| Animal.alive[this.index] = this; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| this.index = Animal.alive.length; | ||
| Animal.alive.push(this); | ||
| } | ||
| get health() { | ||
| return this._health; | ||
| } | ||
| set health(value) { | ||
| this._health = value; | ||
|
|
||
| if (this._health <= 0) { | ||
| this.die(); | ||
| } | ||
| } | ||
| takeDamage(value) { | ||
| this.health -= value; | ||
| } | ||
| die() { | ||
| Animal.alive[this.index] = null; | ||
| Animal.#emptyslots.push(this.index); | ||
| } | ||
| } | ||
|
|
||
| class Herbivore extends Animal { | ||
| // write your code here | ||
| constructor(name, health) { | ||
| super(name, health); | ||
| this.hidden = false; | ||
| } | ||
|
|
||
| hide() { | ||
| this.hidden = true; | ||
| } | ||
| } | ||
|
|
||
| class Carnivore extends Animal { | ||
| // write your code here | ||
| bite(animal) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Override instruction requires checking |
||
| if (!(animal instanceof Herbivore) || animal.hidden) { | ||
| return; | ||
| } | ||
|
|
||
| animal.takeDamage(50); | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove comments