123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- class Tube {
- #maxLevels;
- #colors;
-
- constructor(levels)
- {
- this.#maxLevels = levels;
- this.#colors = [];
- }
-
- addColorLayer(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 = [];
- }
-
- height()
- {
- return this.#maxLevels;
- }
-
- isFull()
- {
- return this.#colors.length == this.#maxLevels;
- }
-
- isEmpty()
- {
- return this.#colors.length == 0;
- }
-
- remainingSpace()
- {
- return this.#maxLevels - this.#colors.length;
- }
-
- getColorAtLevel(index)
- {
- if (index < 0 || index >= this.#colors.length) return null;
-
- return this.#colors[index].toString();
- }
-
- peekTopColor()
- {
- let length = this.#colors.length;
-
- return this.getColorAtLevel(length-1);
- }
-
- isComplete()
- {
- if (this.remainingSpace() != 1) return false;
-
- let reference = this.#colors[0];
-
- for (let i = 1; i < this.#colors.length; i++)
- {
- if (this.#colors[i] != reference)
- {
- return false;
- }
- }
-
- return true;
- }
- }
|