Bläddra i källkod

fixes #3

Ajout d'une fonctionnalité d'historique des
actions
master
Figg 9 månader sedan
förälder
incheckning
bb55ada145
4 ändrade filer med 63 tillägg och 1 borttagningar
  1. 3
    0
      index.html
  2. 12
    1
      js/controller.js
  3. 39
    0
      js/historyHandler.js
  4. 9
    0
      js/model/historyItem.js

+ 3
- 0
index.html Visa fil

@@ -9,8 +9,10 @@
9 9
 	<script src="./js/lib/p5.min.js"></script>
10 10
 	<script src="./js/model/tube.js"></script>
11 11
 	<script src="./js/model/game.js"></script>
12
+	<script src="./js/model/historyItem.js"></script>
12 13
 	<script src="./js/ui.js"></script>
13 14
 	<script src="./js/drawer.js"></script>
15
+	<script src="./js/historyHandler.js"></script>
14 16
 	<script src="./js/controller.js"></script>
15 17
 	<script src="./js/main.js"></script>
16 18
 </head>
@@ -34,6 +36,7 @@
34 36
 			<input type="number" size="5" id="scale" value="50">
35 37
 		</span>
36 38
 		<button type="button" onclick="controller.initializeGame()">New game</button>
39
+		<button type="button" onclick="controller.undoPreviousAction()">Undo</button>
37 40
 	</div>
38 41
 	<div class="row justify-center">
39 42
 		<main oncontextmenu="return false;">

+ 12
- 1
js/controller.js Visa fil

@@ -3,6 +3,7 @@ class Controller
3 3
 	#game;
4 4
 	#drawer;
5 5
 	#colorsBank = ['black','silver','gray','maroon','red','purple','fuchsia','green','lime','olive','yellow','navy','blue','teal','aqua'];
6
+	#historyHandler;
6 7
 	
7 8
 	constructor()
8 9
 	{
@@ -14,7 +15,7 @@ class Controller
14 15
 		
15 16
 		this.#game = new Game(params.TUBESNUMBERS, params.TUBESLEVELS);
16 17
 		this.#drawer = new Drawer(params.PADDING, params.SCALE);
17
-		
18
+		this.#historyHandler = new HistoryHandler();		
18 19
 		this.#drawer.computeGameParams(params.TUBESNUMBERS, params.TUBESLEVELS);
19 20
 
20 21
 		this.ui = new UI(800, 600);
@@ -90,10 +91,15 @@ class Controller
90 91
 	
91 92
 	pourColorInto(sourceTube, targetTube)
92 93
 	{
94
+		let times = 0;
95
+
93 96
 		do
94 97
 		{
95 98
 			targetTube.addColorLayer(sourceTube.removeColorLayer());
99
+			times++;
96 100
 		} while (this.canPourInto(sourceTube, targetTube) && !targetTube.isComplete());
101
+
102
+		this.#historyHandler.log(sourceTube, targetTube, times);
97 103
 		
98 104
 		//this.checkTubeCompletion(sourceTube);
99 105
 		//this.checkTubeCompletion(targetTube);
@@ -116,6 +122,11 @@ class Controller
116 122
 		}
117 123
 	}
118 124
 
125
+	undoPreviousAction()
126
+	{
127
+		this.#historyHandler.restore();
128
+	}
129
+	
119 130
 	mousePressed(e) 
120 131
 	{
121 132
 		let clickedTubeId = this.#drawer.getTubeIdAt(mouseX, mouseY);

+ 39
- 0
js/historyHandler.js Visa fil

@@ -0,0 +1,39 @@
1
+class HistoryHandler
2
+{
3
+    #history;
4
+
5
+    constructor()
6
+    {
7
+        this.#history = [];
8
+    }
9
+
10
+    restore()
11
+    {
12
+        if (this.empty()) return;
13
+
14
+        let lastAction = this.#history.pop();
15
+
16
+        for (let i = 0; i < lastAction.times; i++)
17
+        {
18
+            let color = lastAction.targetTube.removeColorLayer();
19
+            lastAction.sourceTube.addColorLayer(color);
20
+        }
21
+    }
22
+
23
+    empty()
24
+    {
25
+        return this.#history.length == 0;
26
+    }
27
+
28
+    size()
29
+    {
30
+        return this.#history.length;
31
+    }
32
+
33
+    log(_source, _target, _times)
34
+    {
35
+        let historyItem = new HistoryItem(_source, _target, _times);
36
+
37
+        this.#history.push(historyItem);
38
+    }
39
+}

+ 9
- 0
js/model/historyItem.js Visa fil

@@ -0,0 +1,9 @@
1
+class HistoryItem
2
+{
3
+    constructor(_source, _target, _times)
4
+    {
5
+        this.sourceTube = _tbAIndex;
6
+        this.targetTube = _tbBIndex;
7
+        this.times = _times;
8
+    }
9
+}

Laddar…
Avbryt
Spara