1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- class Tube {
- #maxLevels;
- #colors;
-
- constructor(levels)
- {
- this.#maxLevels = levels;
- this.#colors = [];
- }
-
- addColor(color)
- {
- if (this.isFull()) return false;
-
- this.#colors.push(color);
- return true;
- }
-
- removeColorLayer()
- {
- if (this.isEmpty()) return null;
-
- return this.#colors.pop();
- }
-
- drain()
- {
- this.#colors = [];
- }
-
- isFull()
- {
- return this.#colors.length == this.#maxLevels;
- }
-
- isEmpty()
- {
- return this.#colors.length == 0;
- }
-
- emptySpaceSize()
- {
- return this.#maxLevels - this.#colors.length;
- }
-
- getColorAtLevel(index)
- {
- if (index < 0 || index >= this.#colors.length) return '';
-
- return this.#colors[index].toString();
- }
-
- peekTopColor()
- {
- let length = this.#colors.length;
-
- return this.getColorAtLevel(length-1);
- }
- }
|