12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- class Game {
- #colorsBank = ['black','silver','gray','maroon','red','purple','fuchsia','green','lime','olive','yellow','navy','blue','teal','aqua'];
-
- constructor(nbTubes, tubeLevel)
- {
- this.tubes = [];
- this.selectedTube = null;
- this.isCompleted = false;
-
- for (let i = 0; i < nbTubes; i++)
- {
- this.tubes.push(new Tube(tubeLevel));
- }
-
- this.initializeColorTubes(tubeLevel);
- }
-
- /* The goal is to have each tube filled with its own color. Thus, there
- is (tubes minus one) different colors divided in (tubeLevel) layers
- mixed in the tubes.
- One tube is empty to allow movement.
- */
- initializeColorTubes(tubeLevel)
- {
- let nbTubes = this.tubes.length - 1;
- let allColors = [];
-
- for (let i = 0; i < nbTubes; i++)
- {
- let coloridx = i % this.#colorsBank.length;
- for (let times = 0; times < tubeLevel-1; times++)
- {
- allColors.push(this.#colorsBank[coloridx]);
- }
- }
-
- for (let i = allColors.length - 1; i > 0; i--) {
- let j = Math.floor(Math.random() * (i + 1));
- let temp = allColors[i];
- allColors[i] = allColors[j];
- allColors[j] = temp;
- }
-
- for (let index = 0; index < allColors.length; index++)
- {
- let tubeIndex = Math.floor(index/(tubeLevel-1));
- this.tubes[tubeIndex].addColorLayer(allColors[index]);
- }
- }
-
- selectTube(tubeId)
- {
- let newSelectedTube = this.tubes[tubeId];
-
- if (this.selectedTube == newSelectedTube)
- this.selectedTube = null;
- else if (this.canPourInto(this.selectedTube, newSelectedTube))
- this.pourColorInto(this.selectedTube, newSelectedTube);
- else if (!newSelectedTube.isEmpty())
- this.selectedTube = newSelectedTube;
- }
-
- canPourInto(sourceTube, targetTube)
- {
- return sourceTube != null && targetTube != null
- && sourceTube != targetTube
- && !targetTube.isComplete()
- && (targetTube.isEmpty() || sourceTube.peekTopColor() == targetTube.peekTopColor())
- && !targetTube.isFull();
- }
-
- pourColorInto(sourceTube, targetTube)
- {
- while (this.canPourInto(sourceTube, targetTube))
- {
- targetTube.addColorLayer(sourceTube.removeColorLayer());
- }
-
- this.checkTubeCompletion(targetTube);
- }
-
- checkTubeCompletion(tube)
- {
- if (tube.isComplete())
- {
- this.checkGameCompletion();
- }
- }
-
- checkGameCompletion()
- {
- // If all tubes are complete or empty, the game is complete
- this.isCompleted = this.tubes.every(
- tube => tube.isComplete() || tube.isEmpty()
- );
- }
- }
|