-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
574 lines (483 loc) · 25.2 KB
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
function drawMessage(message, color, fontSize = 28) {
ctx2.clearRect(0, 0, canvas.width, canvas.height);
if (message === 'Glückwunsch! Dies war der letzte Knoten in der Priority Queue und du hast\nalle kürzesten Distanzen bestimmt!\n\nDijkstras Algorithmus wurde erfolgreich gelöst! :-)') {
const elapsedTime = endTimer();
const formattedTime = formatTime(elapsedTime);
message += `\n\nBenötigte Zeit: ${formattedTime}\n\nMit PFEILTASTE Links und Rechts kannst du dir die einzelnen\nkürzesten Pfade noch einmal ansehen.`;
}
ctx2.fillStyle = color;
ctx2.font = `${fontSize}px Arial`; // Verwende die übergebene Schriftgröße
const lines = message.split('\n');
let yOffset = 30;
const lineHeight = fontSize + 6;
lines.forEach((line) => {
ctx2.fillText(line, 10, yOffset);
yOffset += lineHeight;
});
}
/* NK Vorgefertigter Graph */
function drawNeighbors(node) {
// Initialisierung der NK zu A
if (node === 'A') {
for (let nearNode of nearNodes) {
const label = graph[nearNode].label ? graph[nearNode].label : nearNode;
drawNode(nearNode, 'lightgray', label);
}
}
//zeichnet alte NK wieder weiß
for (let nearNode of nearNodes) {
const label = graph[nearNode].label ? graph[nearNode].label : nearNode;
drawNode(nearNode, 'white', label);
}
nearNodes = [];
let neighborNodes = Object.keys(graph[node]);
// Schleife zum zeichnen der NK
for (let neighbor of neighborNodes) {
const label = graph[neighbor].label ? graph[neighbor].label : neighbor;
drawNode(neighbor, 'lightgray', label);
nearNodes.push(neighbor);
}
}
/* NK Zufälliger Graph */
function drawRndNode(position, color, label) {
ctx.beginPath();
ctx.strokeStyle = 'black';
ctx.arc(position.x, position.y, 28, 0, 2 * Math.PI, false);
ctx.fillStyle = label === 'node' ? 'lightgrey' : color;
ctx.fill();
ctx.lineWidth = 1;
ctx.stroke();
ctx.fillStyle = 'black';
ctx.font = '24px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(label === 'A' ? 'A(0)' : label, position.x, position.y);
}
function drawRndNeighbors(currentNode, isStartNode = false) {
// A immer gruen und Startknoten
if (currentNode.node === 'A') {
let startNode = nodes.find(n => n.label === 'A');
if (startNode) {
drawRndNode({ x: startNode.x, y: startNode.y }, 'lightgreen', 'A');
}
} else {
// Zeichne den aktuellen Knoten weiß
drawRndNode({ x: currentNode.x, y: currentNode.y }, 'white', currentNode.node);
}
// Reset vorherige NK
for (let nearNode of nearNodes) {
let node = nodes.find(n => n.label === nearNode);
if (node) {
drawRndNode({ x: node.x, y: node.y }, 'white', nearNode);
}
}
nearNodes = [];
// NK aus graph des aktuellen Knotens und filtert alle K die nur undef. NK haben
let neighborNodes = Object.entries(graph[currentNode.node])
.filter(([node, value]) => value !== undefined)
.map(([node, value]) => node);
// Schelife zum zeichnen der NK
for (let neighbor of neighborNodes) {
let node = nodes.find(n => n.label === neighbor);
if (node) {
drawRndNode({ x: node.x, y: node.y }, 'lightgray', neighbor);
nearNodes.push(neighbor);
}
}
if (!isStartNode) {
drawRndShortestPath();
}
}
/* NK Eigener Graph */
function drawOwnNeighNode(x, y, label, color) {
ctx.strokeStyle = 'black';
ctx.beginPath();
ctx.arc(x, y, 30, 0, 2 * Math.PI, false);
ctx.fillStyle = color;
ctx.fill();
ctx.lineWidth = 1;
ctx.stroke();
ctx.fillStyle = 'black';
ctx.font = '26px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(label, x, y);
}
function drawOwnNeighbors(currentNode) {
const radius = 30;
const nodeLabel = currentNode.node;
// Initialisierung der NK zu A
if (nodeLabel === 'A') {
for (let nearNode of nearNodes) {
let node = nodes.find(n => n.label === nearNode);
if (node) {
drawOwnNeighNode(node.x, node.y, nearNode, 'lightgray');
}
}
}
//zeichnet alte NK wieder weiß
for (let nearNode of nearNodes) {
let node = nodes.find(n => n.label === nearNode);
if (node) {
drawOwnNeighNode(node.x, node.y, nearNode, 'white');
}
}
nearNodes = [];
let neighborNodes = graph[nodeLabel];
// Schleife zum zeichnen der NK
for (let neighbor in neighborNodes) {
let node = nodes.find(n => n.label === neighbor);
if (node) {
drawOwnNeighNode(node.x, node.y, neighbor, 'lightgray');
nearNodes.push(neighbor);
// Zeichne die Verbindungslinie zwischen dem aktuellen Knoten und dem Nachbarknoten
let currentNodeObj = nodes.find(n => n.label === nodeLabel);
let position1 = { x: currentNodeObj.x, y: currentNodeObj.y };
let position2 = { x: node.x, y: node.y };
// Berechnet die Richtung und Distanz zwischen den Knoten
const directionX = position2.x - position1.x;
const directionY = position2.y - position1.y;
const distance = Math.sqrt(directionX * directionX + directionY * directionY);
const unitDirectionX = directionX / distance;
const unitDirectionY = directionY / distance;
// Berechnet die Positionen der Knotenränder
const borderPosition1 = {
x: position1.x + unitDirectionX * radius,
y: position1.y + unitDirectionY * radius
};
const borderPosition2 = {
x: position2.x - unitDirectionX * radius,
y: position2.y - unitDirectionY * radius
};
// Zeichnet die Linie zwischen den Knoten
ctx.beginPath();
ctx.moveTo(borderPosition1.x, borderPosition1.y);
ctx.lineTo(borderPosition2.x, borderPosition2.y);
ctx.strokeStyle = 'black';
ctx.lineWidth = 1;
ctx.stroke();
}
}
}
// Zeichnet den kürzesten Pfad vorgefertigt
function drawShortestPath() {
if (currentPathIndex < shortestPathDraw.length) {
let path = shortestPathDraw[currentPathIndex];
// Kanten Knoten kürzester Pfades zeichnen
for (let i = 0; i < path.length - 1; i++) {
const node1 = path[i].node;
const node2 = path[i + 1].node;
drawConnection(node1, node2, 'red', 3);
ctx.strokeStyle = 'black'; // Setze die Linienfarbe auf Schwarz zurück
}
if (path.length && path.slice(-1)[0].node) {
drawNeighbors(path.slice(-1)[0].node);
}
// Alle bisher grün markierten Knoten und den aktuellen Pfad zeichnen
for (let i = 0; i <= currentPathIndex; i++) {
let currentPath = shortestPathDraw[i];
currentPath.forEach((pathNode, index) => {
let position = nodes[pathNode.node];
ctx.beginPath();
ctx.arc(position.x, position.y, 32, 0, 2 * Math.PI, false);
if (i === currentPathIndex && index === currentPath.length - 1) {
ctx.fillStyle = '#FFD580'; // Helles Orange für den letzten Knoten des aktuellen Pfades
} else {
ctx.fillStyle = 'lightgreen';
}
ctx.strokeStyle = 'red'; // Setze die Linienfarbe auf rot zurück
ctx.fill();
ctx.lineWidth = 1;
ctx.stroke();
ctx.fillStyle = 'black';
ctx.font = '22px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(`${pathNode.node} (${pathNode.distance})`, position.x, position.y);
});
}
}
}
function drawRndShortestPath() {
if (currentPathIndex < shortestPathDraw.length) {
const radius = 30;
for (let idx = 0; idx <= currentPathIndex; idx++) {
let path = shortestPathDraw[idx];
for (let i = 0; i < path.length - 1; i++) {
let node1 = path[i].node;
let node2 = path[i + 1].node;
let pos1 = nodes.find(n => n.label === node1);
let pos2 = nodes.find(n => n.label === node2);
if (pos1 && pos2) {
const angle = Math.atan2(pos2.y - pos1.y, pos2.x - pos1.x);
const startX = pos1.x + radius * Math.cos(angle);
const startY = pos1.y + radius * Math.sin(angle);
const endX = pos2.x - radius * Math.cos(angle);
const endY = pos2.y - radius * Math.sin(angle);
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(endX, endY);
ctx.strokeStyle = 'red';
ctx.lineWidth = 3;
ctx.stroke();
}
}
path.forEach((pathNode, index) => {
let node = nodes.find(n => n.label === pathNode.node);
ctx.beginPath();
ctx.arc(node.x, node.y, 30, 0, 2 * Math.PI, false);
if (index === path.length - 1 && idx === currentPathIndex) {
ctx.fillStyle = '#FFD580';
} else {
ctx.fillStyle = 'lightgreen';
}
ctx.strokeStyle = 'red';
ctx.fill();
ctx.lineWidth = 1;
ctx.stroke();
ctx.fillStyle = 'black';
ctx.font = '22px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(`${pathNode.node} (${pathNode.distance})`, node.x, node.y);
});
}
}
}
function drawOwnShortestPath() {
const radius = 30;
if (currentPathIndex < shortestPathDraw.length) {
let path = shortestPathDraw[currentPathIndex];
// Kanten Knoten kürzester Pfade
for (let i = 0; i < path.length - 1; i++) {
let node1Label = path[i].node;
let node2Label = path[i + 1].node;
let node1 = nodes.find(n => n.label === node1Label);
let node2 = nodes.find(n => n.label === node2Label);
if (node1 && node2) {
let position1 = { x: node1.x, y: node1.y };
let position2 = { x: node2.x, y: node2.y };
// Berechnet die Richtung und Distanz zwischen den Knoten
const directionX = position2.x - position1.x;
const directionY = position2.y - position1.y;
const distance = Math.sqrt(directionX * directionX + directionY * directionY);
const unitDirectionX = directionX / distance;
const unitDirectionY = directionY / distance;
// Berechnet die Positionen der Knotenränder
const borderPosition1 = {
x: position1.x + unitDirectionX * radius,
y: position1.y + unitDirectionY * radius
};
const borderPosition2 = {
x: position2.x - unitDirectionX * radius,
y: position2.y - unitDirectionY * radius
};
// Zeichnet die Linie zwischen den Knoten
ctx.beginPath();
ctx.moveTo(borderPosition1.x, borderPosition1.y);
ctx.lineTo(borderPosition2.x, borderPosition2.y);
ctx.strokeStyle = 'red';
ctx.lineWidth = 3;
ctx.stroke();
}
}
// Zeichne aktuellen Knoten und die Nachbarknoten
let currentNode = path[path.length - 1];
drawOwnNeighbors(currentNode);
for (let i = 0; i <= currentPathIndex; i++) {
let currentPath = shortestPathDraw[i];
currentPath.forEach((pathNode, index) => {
let node = nodes.find(n => n.label === pathNode.node);
ctx.beginPath();
ctx.arc(node.x, node.y, 30, 0, 2 * Math.PI, false);
if (i === currentPathIndex && index === currentPath.length - 1) {
ctx.fillStyle = '#FFD580'; // Helles Orange für den letzten Knoten des aktuellen Pfades
} else {
ctx.fillStyle = 'lightgreen';
}
ctx.strokeStyle = 'red'; // Setze die Linienfarbe auf rot zurück
ctx.fill();
ctx.lineWidth = 1;
ctx.stroke();
ctx.fillStyle = 'black';
ctx.font = '21px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(`${pathNode.node}(${pathNode.distance})`, node.x, node.y);
});
}
}
}
// Event-Listener für Screenshot Ausgabe
document.addEventListener('keydown', function (event) {
if (algorithmCompleted) {
if (event.key === 'ArrowLeft') {
if (currentScreenshotIndex > 0) {
currentScreenshotIndex--;
displayScreenshot();
}
} else if (event.key === 'ArrowRight') {
if (currentScreenshotIndex < screenshots.length - 1) {
currentScreenshotIndex++;
displayScreenshot();
}
}
}
});
function displayScreenshot() {
let screenshotImage = new Image();
screenshotImage.onload = function () {
ctx.clearRect(0, 0, canvas1.width, canvas1.height);
ctx.drawImage(screenshotImage, 0, 0);
};
screenshotImage.src = screenshots[currentScreenshotIndex];
}
// Validierung der Benutzereingabe
function validateInput() {
if (algorithmCompleted) {
return;
}
let sortedKeys = Object.keys(pqStates);
if (currentIndex >= sortedKeys.length) {
currentIndex = sortedKeys.indexOf('A');
}
let key = sortedKeys[currentIndex];
let userInput, userEntries, expectedState;
let screenshot = canvas.toDataURL();
screenshots.push(screenshot);
userInput = document.getElementById('inputf').value.toUpperCase().replace(/\s+/g, '');
// Methoden zur Angleichung der Eingabe und des erwarteten Zustands
userEntries = userInput
.toLowerCase()
.replace(/\s+/g, '')
.split(',')
.sort()
.join(',');
expectedState = pqStates[key]
.map(element => `${element.node.toLowerCase()}${element.distance}`)
.sort()
.join(',');
if (userEntries === expectedState) {
userInput = userInput.split(',')
.sort((a, b) => {
let numA = parseInt(a.slice(1));
let numB = parseInt(b.slice(1));
if (numA === numB) {
return a[0].localeCompare(b[0]);
}
return numA - numB;
})
.join(',');
let userInputArray = userInput.split(',');
let formattedUserInput = userInputArray.map(element => {
let letter = element.charAt(0);
let number = element.slice(1);
return `${letter}(${number})`;
}).join(',');
// Ausgabe des kürzesten Knotens
let shortestDistanceNode = userInputArray[0];
let letter = shortestDistanceNode.charAt(0);
let number = shortestDistanceNode.slice(1);
let output = `${letter}(${number})`;
document.getElementById('Element' + (currentIndex + 1)).value = output;
document.getElementById('priorityQueue' + (currentIndex + 1)).value = formattedUserInput;
// Identifiziere neue und aktualisierte Knoten aus pqStates
let previousState = currentIndex > 0 ? pqStates[sortedKeys[currentIndex - 1]] : [];
let currentState = pqStates[key];
let newNodes = currentState.filter(node => !previousState.some(prevNode => prevNode.node === node.node));
let updatedNodes = currentState.filter(node => previousState.some(prevNode => prevNode.node === node.node && prevNode.distance !== node.distance));
if (isOwnNodesNewActive) {
drawOwnShortestPath(shortestPathDraw[currentPathIndex]);
} else if (isRndNodesActive) {
drawRndShortestPath(shortestPathDraw[currentPathIndex]);
let currentNode = shortestPaths[currentIndex + 1];
drawRndNeighbors(currentNode);
} else {
drawShortestPath(shortestPathDraw[currentPathIndex]);
}
currentIndex++;
currentPathIndex++;
// Überprüfe, ob es das letzte Element in der Priority Queue ist
if (currentIndex === sortedKeys.length - 1) {
drawMessage('Glückwunsch! Dies war der letzte Knoten in der Priority Queue und du hast\nalle kürzesten Distanzen bestimmt!\n\nDijkstras Algorithmus wurde erfolgreich gelöst! :-)', 'green');
algorithmCompleted = true;
let finalScreenshot = canvas.toDataURL();
screenshots.push(finalScreenshot);
currentScreenshotIndex = screenshots.length - 1;
} else {
// Ausgabe der neuen und aktualisierten Knoten
if (newNodes.length > 0 || updatedNodes.length > 0) {
let message = '';
if (newNodes.length > 0) {
message += "Neue Knoten:\n" + newNodes.map(node => `${node.node}(${node.distance})`).join(', ') + "\n\n";
}
if (updatedNodes.length > 0) {
message += "Aktualisierte Knoten:\n" + updatedNodes.map(node => `${node.node}(${node.distance})`).join(', ');
}
drawMessage(message, 'blue', 32);
} else {
drawMessage("Nur Ausgabe, kein Update!", 'blue', 32);
}
let remainingElements = formattedUserInput.split(',').slice(1);
let nextInput = remainingElements.map(element => element.replace(/\(|\)/g, '')).join(',');
document.getElementById('inputf').value = nextInput;
}
// Überprüfen, ob Zeile21 Ausgabetabelle keinen leeren Wert hat und Z22 vorhanden
if (document.getElementById('Element21').value !== '' && !document.getElementById('Element22')) {
// Tabelle mit ID "distanceTable" auswählen
let table = document.getElementById('distanceTable');
// 5 neue Zeilen hinzufügen
for (let i = 22; i <= 26; i++) {
let newRow = table.insertRow();
let cell1 = newRow.insertCell();
let cell2 = newRow.insertCell();
cell1.innerHTML = `<input type="text" id="Element${i}" placeholder="...">`;
cell2.innerHTML = `<input type="text" id="priorityQueue${i + 1}" placeholder="...">`;
}
}
} else {
drawMessage('Die Eingabe ist nicht korrekt!\nDu kannst dir per "Hilf mir" Button die korrekte Antwort anzeigen lassen!\n\nDies geht aber nur 3 mal pro Spiel!\nBitte versuche es noch einmal!', 'red');
}
}
function showExpectedState() {
event.stopPropagation();
if (showExpectedStateCount >= 3) {
const message = "Alle Hilfen wurden aufgebraucht. Versuche es nun selbst!";
drawMessage(message, 'red');
return;
}
let remainingUses = 3 - showExpectedStateCount;
// Sortiert die Schlüssel der pqStates und gibt den erwarteten Zustand aus
let sortedKeys = Object.keys(pqStates);
if (currentIndex >= sortedKeys.length) {
currentIndex = sortedKeys.indexOf('A');
}
// sortedk und currentIndex als Index für den erwarteten Zustand müssen passen!
let key = sortedKeys[currentIndex];
let expectedState = pqStates[key]
.map(element => `${element.node}${element.distance}`)
.sort()
.join(', ');
// Überprüfen, ob der aktuelle expectedState dem letzten angezeigten expectedState entspricht
if (expectedState === lastExpectedState) {
const message = `Der erwartete Zustand wurde bereits angezeigt für Knoten ${key} mit:\n\n ${expectedState}!`;
drawMessage(message, 'red');
return;
}
const message = `Die P.Q. für Knoten ${key} lautet: ${expectedState} \n\nVerbleibende Nutzungen: ${remainingUses - 1}`;
if (remainingUses === 0) {
message += `\nAlle Hilfen wurden aufgebraucht.\n\n Knoten ${key} mit:\n\n ${expectedState}!`;
}
drawMessage(message, 'red');
showExpectedStateCount++;
lastExpectedState = expectedState; // Speichern des aktuell angezeigten expectedState
}
////////////////////////////////////////////////////////////////////////////////////////////////////// Event listener der 3 Spielmodi
// key logger Enter
document.addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
if (document.getElementById('inputf') === document.activeElement) {
validateInput();
}
}
});