123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- class Game {
- #tubes;
- #selectedTubeIndex;
-
- constructor(nbTubes, tubeLevel)
- {
- this.#tubes = [];
- this.#selectedTubeIndex = -1;
-
- for (let i = 0; i < nbTubes; i++)
- {
- this.#tubes.push(new Tube(tubeLevel));
- }
- }
-
- tubeAt(tubeIndex)
- {
- if (!this.isInRange(tubeIndex)) return null;
-
- return this.#tubes[tubeIndex];
- }
-
- selectedTubeIndex()
- {
- return this.#selectedTubeIndex;
- }
-
- selectedTube()
- {
- if (this.#selectedTubeIndex == -1) return null;
-
- return this.#tubes[this.#selectedTubeIndex];
- }
-
- selectTubeAt(index)
- {
- if (this.isInRange(index))
- {
- this.#selectedTubeIndex = index;
- }
- }
-
- removeTube(tube)
- {
- let tubeIndex = this.#tubes.indexOf(tube);
-
- if (tubeIndex != -1)
- {
- this.#tubes.splice(tubeIndex, 1)[0];
- if (tubeIndex == this.#selectedTubeIndex) this.unselectTube();
- }
- }
-
- clean()
- {
- this.#tubes = []
- this.unselectTube();
- }
-
- unselectTube()
- {
- this.#selectedTubeIndex = -1;
- }
-
- tubesNumber()
- {
- return this.#tubes.length;
- }
-
- isInRange(index)
- {
- return index >= 0 && index < this.#tubes.length;
- }
-
- isComplete()
- {
- return this.#tubes.every(
- tube => tube.isComplete() || tube.isEmpty()
- );
- }
- }
|