Browse Source

les tubes disparaissent quand ils sont complétés

pull/1/head
Figg 9 months ago
parent
commit
38a5215350
2 changed files with 32 additions and 2 deletions
  1. 15
    2
      js/game.js
  2. 17
    0
      js/tube.js

+ 15
- 2
js/game.js View File

@@ -73,11 +73,24 @@ class Game {
73 73
 		
74 74
 		let pouringColor = sourceTube.peekTopColor();
75 75
 		
76
-		while (sourceTube.peekTopColor() == pouringColor && !targetTube.isFull())
76
+		while (sourceTube.peekTopColor() == pouringColor && !targetTube.isFull() && !targetTube.isComplete())
77 77
 		{
78 78
 			targetTube.addColor(sourceTube.removeColorLayer());
79 79
 		}
80
+		
81
+		this.checkTubeCompletion(sourceTube);
82
+		this.checkTubeCompletion(targetTube);
80 83
 	}
81 84
 	
82
-	
85
+	checkTubeCompletion(tube)
86
+	{
87
+		if (tube.isComplete()) 
88
+		{
89
+			let tubeIndex = this.tubes.indexOf(tube);
90
+			
91
+			this.tubes.splice(tubeIndex, 1);
92
+			
93
+			if (this.selectedTube == tube) this.selectedTube = null;
94
+		}
95
+	}
83 96
 }

+ 17
- 0
js/tube.js View File

@@ -56,4 +56,21 @@ class Tube {
56 56
 		
57 57
 		return this.getColorAtLevel(length-1);
58 58
 	}
59
+
60
+	isComplete()
61
+	{
62
+		if (this.emptySpaceSize() != 1) return false;
63
+		
64
+		let reference = this.#colors[0];
65
+		
66
+		for (let i = 1; i < this.#colors.length; i++)
67
+		{
68
+			if (this.#colors[i] != reference)
69
+			{
70
+				return false;
71
+			}
72
+		}
73
+		
74
+		return true;
75
+	}
59 76
 }

Loading…
Cancel
Save