|
@@ -5,7 +5,8 @@ class Game {
|
5
|
5
|
{
|
6
|
6
|
this.tubes = [];
|
7
|
7
|
this.selectedTube = null;
|
8
|
|
-
|
|
8
|
+ this.isCompleted = false;
|
|
9
|
+
|
9
|
10
|
for (let i = 0; i < nbTubes; i++)
|
10
|
11
|
{
|
11
|
12
|
this.tubes.push(new Tube(tubeLevel));
|
|
@@ -63,6 +64,7 @@ class Game {
|
63
|
64
|
{
|
64
|
65
|
return sourceTube != null && targetTube != null
|
65
|
66
|
&& sourceTube != targetTube
|
|
67
|
+ && !targetTube.isComplete()
|
66
|
68
|
&& (targetTube.isEmpty() || sourceTube.peekTopColor() == targetTube.peekTopColor())
|
67
|
69
|
&& !targetTube.isFull();
|
68
|
70
|
}
|
|
@@ -74,29 +76,22 @@ class Game {
|
74
|
76
|
targetTube.addColorLayer(sourceTube.removeColorLayer());
|
75
|
77
|
}
|
76
|
78
|
|
77
|
|
- this.checkTubeCompletion(sourceTube);
|
78
|
79
|
this.checkTubeCompletion(targetTube);
|
79
|
80
|
}
|
80
|
81
|
|
81
|
82
|
checkTubeCompletion(tube)
|
82
|
83
|
{
|
83
|
84
|
if (tube.isComplete())
|
84
|
|
- {
|
85
|
|
- let tubeIndex = this.tubes.indexOf(tube);
|
86
|
|
-
|
87
|
|
- this.tubes.splice(tubeIndex, 1);
|
88
|
|
-
|
89
|
|
- if (this.selectedTube == tube) this.selectedTube = null;
|
90
|
|
-
|
|
85
|
+ {
|
91
|
86
|
this.checkGameCompletion();
|
92
|
87
|
}
|
93
|
88
|
}
|
94
|
89
|
|
95
|
90
|
checkGameCompletion()
|
96
|
91
|
{
|
97
|
|
- if (this.tubes.length == 1 && this.tubes[0].isEmpty())
|
98
|
|
- {
|
99
|
|
- this.tubes = [];
|
100
|
|
- }
|
|
92
|
+ // If all tubes are complete or empty, the game is complete
|
|
93
|
+ this.isCompleted = this.tubes.every(
|
|
94
|
+ tube => tube.isComplete() || tube.isEmpty()
|
|
95
|
+ );
|
101
|
96
|
}
|
102
|
97
|
}
|