Browse Source

Do not remove completed tubes & show a message when game is won

pull/2/head
demisel 11 months ago
parent
commit
5df90a5801
3 changed files with 25 additions and 14 deletions
  1. 1
    1
      js/controller.js
  2. 16
    0
      js/drawer.js
  3. 8
    13
      js/game.js

+ 1
- 1
js/controller.js View File

@@ -42,7 +42,7 @@ function gatherMenuValues()
42 42
 }
43 43
 function draw()
44 44
 {
45
-	if (!this.needUpdate) return;
45
+	if (!this.needUpdate && !game.isCompleted) return;
46 46
 	
47 47
 	this.drawer.draw(this.game);
48 48
 	

+ 16
- 0
js/drawer.js View File

@@ -30,6 +30,9 @@ class Drawer
30 30
 		background('white');
31 31
 		
32 32
 		this.drawTubes(this.padding, this.padding+this.tubeSize, tubes, selectedTube);
33
+
34
+		if(game.isCompleted)
35
+			this.drawWinView();
33 36
 	}
34 37
 
35 38
 	drawTubes(x, y, tubes, selectedTube)
@@ -78,4 +81,17 @@ class Drawer
78 81
 			&& x < this.#canvasWidth - this.padding
79 82
 			&& y < this.#canvasHeight - this.padding;
80 83
 	}
84
+
85
+	drawWinView(){
86
+		fill('red');
87
+		textSize(50);
88
+		let textX = this.#canvasWidth / 2 - 175 + 10 * sin(frameCount/10);
89
+		let textY = this.#canvasHeight / 2 		+ 10 * cos(frameCount/10);
90
+		let message = "👑 You Win! 👑"  
91
+		text(message, textX, textY);
92
+		textX ++;
93
+		textY ++;
94
+		fill('yellow');
95
+		text(message, textX, textY);
96
+	}
81 97
 }

+ 8
- 13
js/game.js View File

@@ -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
 }

Loading…
Cancel
Save