-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.js
1687 lines (1444 loc) · 53 KB
/
backend.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const VALID_INSTRUCTIONS = new Set([
"NIL",
"MOV",
"NOP",
"SWP",
"SAV",
"ADD",
"SUB",
"NEG",
"JMP",
"JEZ",
"JNZ",
"JGZ",
"JLZ"
]);
const VALID_DIR = new Set([
"NIL",
"LEFT",
"RIGHT",
"UP",
"DOWN"
]);
const VALID_INTERNAL_DIR = new Set([
"ACC"
]);
//UI DATA
let MODAL_OPEN = false;
let RUNNING = false;
let FAST = false;
const RUN_SPEED = 500;
const FAST_RUN_SPEED = 150;
let iterations = 0;
const node_count = 9;
let graph_connections = null;
const current_nodes = [];
let current_connection_port_data = []; //[from, to, direction, data]
const LEVEL_ONE_TITLE = "00000"
const LEVEL_ONE_NAME = "Diagnostic STEP A";
const LEVEL_ONE_BLURB = "> Read a value from IN.A \n > Read a value from IN.C \n > Output IN.A to OUT.C \n > Output IN.C to OUT.A";
const LEVEL_ONE_NODES = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const LEVEL_ONE_DATA_A = [111, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 999]; //36 length
const LEVEL_ONE_DATA_B = [];
const LEVEL_ONE_DATA_C = [111, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 999];
const LEVEL_ONE_EXPECTED_OUTPUT_A = [111, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 999];
const LEVEL_ONE_EXPECTED_OUTPUT_B = [];
const LEVEL_ONE_EXPECTED_OUTPUT_C = [111, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 999];
const LEVEL_ONE_CONNECTIONS = [
[1, 2, 'RIGHT'],
[1, 4, 'DOWN'],
[1, 11, 'UP'], // input
[2, 1, 'LEFT'],
[2, 22, 'UP'], //input
[2, 3, 'RIGHT'],
[2, 5, 'DOWN'],
[3, 33, 'UP'], //input
[3, 2, 'LEFT'],
[3, 6, 'DOWN'],
[4, 1, 'UP'],
[4, 7, 'DOWN'],
[4, 5, 'RIGHT'],
[5, 2, 'UP'],
[5, 4, 'LEFT'],
[5, 6, 'RIGHT'],
[5, 8, 'DOWN'],
[6, 3, 'UP'],
[6, 5, 'LEFT'],
[6, 9, 'DOWN'],
[7, 4, 'UP'],
[7, 8, 'RIGHT'],
[8, 5, 'UP'],
[8, 7, 'LEFT'],
[8, 9, 'RIGHT'],
[9, 6, 'UP'],
[9, 8, 'LEFT'],
[7, 111, 'DOWN'], //output
[9, 333, 'DOWN'], //output
[11, 1, 'DOWN'], //input
[33, 3, 'DOWN'] //input
];
//load level data into these placeholders in initialize level
let OUTPUT_STACK_A = [];
let OUTPUT_STACK_B = [];
let OUTPUT_STACK_C = [];
let INPUT_STACK_A = [];
let INPUT_STACK_B = [];
let INPUT_STACK_C = [];
let EXPECTED_OUTPUT_A = [];
let EXPECTED_OUTPUT_B = [];
let EXPECTED_OUTPUT_C = [];
let LEVEL_TITLE = "";
let LEVEL_NAME = "";
//load the level data for the UI
let UI_LEVEL_TITLE;
let UI_LEVEL_NAME;
let UI_LEVEL_BLURB;
let UI_SOLVED_BLURB;
let UI_SELECTED;
//list containing data waiting at valid connection
//cannot add connection if data is waiting
//instruction is set back
//Adjacency list directed
class Graph {
constructor() {
this.adjacencyList = {};
}
addVertex(vertex) {
if (!this.adjacencyList[vertex]) {
this.adjacencyList[vertex] = [];
}
}
addEdge(vertex1, vertex2) {
if (!this.adjacencyList[vertex1]) {
this.addVertex(vertex1);
}
if (!this.adjacencyList[vertex2]) {
this.addVertex(vertex2);
}
this.adjacencyList[vertex1].push(vertex2);
}
removeEdge(vertex1, vertex2) {
this.adjacencyList[vertex1] = this.adjacencyList[vertex1].filter(
v => v !== vertex2
);
}
removeVertex(vertex) {
while (this.adjacencyList[vertex].length) {
const adjacentVertex = this.adjacencyList[vertex].pop();
this.removeEdge(adjacentVertex, vertex);
}
delete this.adjacencyList[vertex];
}
display() {
for (let vertex in this.adjacencyList) {
console.log(vertex + " -> " + this.adjacencyList[vertex].join(", "));
}
}
getEdges(vertex) {
return this.adjacencyList[vertex] || [];
}
}
class TNODE {
constructor(node_id) {
this.name = node_id;
this.ACC = 0;
this.BAK = 0;
this.LAST = "N/A";
this.MODE = "IDLE";
this.IDLE = "0%";
this.hang_send_state = false;
this.hang_receive_state = false;
this.current_instruction_line = -1;
this.label_jump_instruction_line = -1;
}
}
function start_up() {
initalize_nodes();
reset_display();
add_node_connections();
create_node_connection_arrows();
limit_instruction_input_fiels();
initialize_level();
load_level_data();
}
function reset_display() {
for (let i = 0; i < current_nodes.length; i++) {
current_nodes[i].ACC = 0;
current_nodes[i].BAK = 0;
current_nodes[i].current_instruction_line = -1;
accv = "NODE" + current_nodes[i].name + "_ACC_VALUE";
document.getElementById(accv).innerHTML = "0";
bakv = "NODE" + current_nodes[i].name + "_BAK_VALUE";
document.getElementById(bakv).innerHTML = "0";
}
clear_instruction_colors();
current_connection_port_data.length = 0;
}
function reset_level_data() {
//empty i/o registers for level
//change the colors back on the p tags
OUTPUT_STACK_A = [];
OUTPUT_STACK_B = [];
OUTPUT_STACK_C = [];
INPUT_STACK_A = [];
INPUT_STACK_B = [];
INPUT_STACK_C = [];
const current_output_a_div = document.getElementById('level_num_box_in_a');
const current_output_a_para = current_output_a_div.getElementsByTagName('p');
const current_output_b_div = document.getElementById('level_num_box_in_b');
const current_output_b_para = current_output_b_div.getElementsByTagName('p');
const current_output_c_div = document.getElementById('level_num_box_in_c');
const current_output_c_para = current_output_c_div.getElementsByTagName('p');
//output
const current_received_output_a_div = document.getElementById('level_num_box_output_a');
let current_received_output_a_para = current_received_output_a_div.getElementsByTagName('p');
const current_received_output_b_div = document.getElementById('level_num_box_output_b');
let current_received_output_b_para = current_received_output_b_div.getElementsByTagName('p');
const current_received_output_c_div = document.getElementById('level_num_box_output_c');
let current_received_output_c_para = current_received_output_c_div.getElementsByTagName('p');
//delete all previous highlights
for (i = 0; i < 35; i++) {
current_output_a_para[i].classList.remove('selected_number_higholight');
current_output_b_para[i].classList.remove('selected_number_higholight');
current_output_c_para[i].classList.remove('selected_number_higholight');
current_received_output_a_para[i].classList.remove('selected_number_higholight');
current_received_output_a_para[i].classList.remove('incorrect_number_highlight');
current_received_output_b_para[i].classList.remove('selected_number_higholight');
current_received_output_b_para[i].classList.remove('incorrect_number_highlight');
current_received_output_c_para[i].classList.remove('selected_number_higholight');
current_received_output_c_para[i].classList.remove('incorrect_number_highlight');
}
}
function update_display() {
for (let i = 0; i < current_nodes.length; i++) {
accv = "NODE" + current_nodes[i].name + "_ACC_VALUE";
document.getElementById(accv).innerHTML = current_nodes[i].ACC;
bakv = "NODE" + current_nodes[i].name + "_BAK_VALUE";
document.getElementById(bakv).innerHTML = current_nodes[i].BAK;
}
}
function update_level_data_ui() {
//input
const current_output_a_div = document.getElementById('level_num_box_in_a');
const current_output_a_para = current_output_a_div.getElementsByTagName('p');
const current_output_b_div = document.getElementById('level_num_box_in_b');
const current_output_b_para = current_output_b_div.getElementsByTagName('p');
const current_output_c_div = document.getElementById('level_num_box_in_c');
const current_output_c_para = current_output_c_div.getElementsByTagName('p');
//output
const current_received_output_a_div = document.getElementById('level_num_box_output_a');
let current_received_output_a_para = current_received_output_a_div.getElementsByTagName('p');
const current_received_output_b_div = document.getElementById('level_num_box_output_b');
let current_received_output_b_para = current_received_output_b_div.getElementsByTagName('p');
const current_received_output_c_div = document.getElementById('level_num_box_output_c');
let current_received_output_c_para = current_received_output_c_div.getElementsByTagName('p');
//delete all previous highlights
for (i = 0; i < 35; i++) {
current_output_a_para[i].classList.remove('selected_number_higholight');
current_output_b_para[i].classList.remove('selected_number_higholight');
current_output_c_para[i].classList.remove('selected_number_higholight');
//keep incorrect lines ?
current_received_output_a_para[i].classList.remove('selected_number_higholight');
//current_received_output_a_para[i].classList.remove('incorrect_number_highlight');
current_received_output_b_para[i].classList.remove('selected_number_higholight');
//current_received_output_b_para[i].classList.remove('incorrect_number_highlight');
current_received_output_c_para[i].classList.remove('selected_number_higholight');
//current_received_output_c_para[i].classList.remove('incorrect_number_highlight');
}
//HIGHLIGHT CURRENT LINE THAT THE IN NODES ARE OUTPUTTING
let s = (EXPECTED_OUTPUT_A.length - INPUT_STACK_A.length) - 1;
if (s < 0) {
s = 0
};
current_output_a_para[s].classList.add('selected_number_higholight');
s = (EXPECTED_OUTPUT_B.length - INPUT_STACK_B.length) - 1;
if (s < 0) {
s = 0
};
current_output_b_para[s].classList.add('selected_number_higholight');
s = (EXPECTED_OUTPUT_C.length - INPUT_STACK_C.length) - 1;
if (s < 0) {
s = 0
};
current_output_c_para[s].classList.add('selected_number_higholight');
//Fill the OUT A B C with received outputs
//HIGHLIGHT CURRENT LINE THAT IS CURRENTLY BEING OUTPUTTED
//HIGHLIGHT INCORRECT OUTPUTS
for (i = 0; i < OUTPUT_STACK_A.length; i++) {
current_received_output_a_para[i].innerText = OUTPUT_STACK_A[i];
if ((i + 1) == OUTPUT_STACK_A.length) {
if (OUTPUT_STACK_A[i] == EXPECTED_OUTPUT_A[i]) {
current_received_output_a_para[i].classList.add('selected_number_higholight');
} else {
current_received_output_a_para[i].classList.add('incorrect_number_highlight');
}
}
}
for (i = 0; i < OUTPUT_STACK_B.length; i++) {
current_received_output_b_para[i].innerText = OUTPUT_STACK_B[i];
if ((i + 1) == OUTPUT_STACK_B.length) {
if (OUTPUT_STACK_B[i] == EXPECTED_OUTPUT_B[i]) {
current_received_output_b_para[i].classList.add('selected_number_higholight');
} else {
current_received_output_b_para[i].classList.add('incorrect_number_highlight');
}
}
}
for (i = 0; i < OUTPUT_STACK_C.length; i++) {
current_received_output_c_para[i].innerText = OUTPUT_STACK_C[i];
if ((i + 1) == OUTPUT_STACK_C.length) {
if (OUTPUT_STACK_C[i] == EXPECTED_OUTPUT_C[i]) {
current_received_output_c_para[i].classList.add('selected_number_higholight');
} else {
current_received_output_c_para[i].classList.add('incorrect_number_highlight');
}
}
}
}
function clear_instruction_colors(n = -1) {
if (n == -1) {
for (let i = 0; i < current_nodes.length; i++) {
for (let k = 0; k <= 14; k++) {
const ln = "NODE" + current_nodes[i].name + "_INSTRUCTION" + k + "_CODE_LINE";
document.getElementById(ln).style.backgroundColor = 'rgb(36, 35, 35)';
}
}
} else {
for (let k = 0; k <= 14; k++) {
const ln = "NODE" + n + "_INSTRUCTION" + k + "_CODE_LINE";
document.getElementById(ln).style.backgroundColor = 'rgb(36, 35, 35)';
}
}
}
function add_node_connections() {
graph_connections = new Graph();
for (let i = 0; i < current_nodes.length; i++) {
graph_connections.addVertex(i.name);
}
LEVEL_ONE_CONNECTIONS.forEach(pair => {
const [first, second] = pair;
graph_connections.addEdge(first, second);
})
}
function create_node_connection_arrows() {
// alert(graph_connections.getEdges(1));
}
function initalize_nodes() {
for (let i = 0; i < LEVEL_ONE_NODES.length; i++) {
let new_node = new TNODE(LEVEL_ONE_NODES[i]);
current_nodes.push(new_node);
}
}
function limit_instruction_input_fiels() {
document.querySelectorAll('.INSTRUCTION_CODE_LINE').forEach(line => {
line.addEventListener('input', (e) => {
const text = e.target.innerText;
// Limit characters to 15
if (text.length > 15) {
e.target.innerText = text.substring(0, 15);
placeCaretAtEnd(e.target);
}
// Prevent new lines
if (text.includes('\n')) {
e.target.innerText = text.replace(/\n/g, '');
placeCaretAtEnd(e.target);
}
});
// Prevent pasting multiline text
line.addEventListener('paste', (e) => {
e.preventDefault();
const text = e.clipboardData.getData('text/plain').replace(/\n/g, '').substring(0, 15);
document.execCommand('insertText', false, text);
});
});
}
function initialize_level() {
//grab the level form the URL
let urlParams = new URLSearchParams(window.location.search);
let level_id = urlParams.get('levelid');
if (level_id == "" || level_id == null){
level_id = "ONE";
}
let t;
t = "LEVEL_" + level_id + "_";
//initialize level data
INPUT_STACK_A = Array.from(eval(t + "DATA_A"));
INPUT_STACK_B = Array.from(eval(t + "DATA_B"));
INPUT_STACK_C = Array.from(eval(t + "DATA_C"));
EXPECTED_OUTPUT_A = Array.from(eval(t + "EXPECTED_OUTPUT_A"));
EXPECTED_OUTPUT_B = Array.from(eval(t + "EXPECTED_OUTPUT_B"));
EXPECTED_OUTPUT_C = Array.from(eval(t + "EXPECTED_OUTPUT_C"));
LEVEL_TITLE = eval(t + "TITLE");
UI_LEVEL_TITLE = LEVEL_TITLE;
LEVEL_NAME = eval(t + "NAME");
OUTPUT_STACK_A = [];
OUTPUT_STACK_B = [];
OUTPUT_STACK_C = [];
const blurb = document.getElementById('LEVEL_BLURB_TEXT');
blurb.innerText = eval(t + "BLURB")
//delete former data if there is any
//input
const current_output_a_div = document.getElementById('level_num_box_in_a');
const current_output_a_para = current_output_a_div.getElementsByTagName('p');
const current_output_b_div = document.getElementById('level_num_box_in_b');
const current_output_b_para = current_output_b_div.getElementsByTagName('p');
const current_output_c_div = document.getElementById('level_num_box_in_c');
const current_output_c_para = current_output_c_div.getElementsByTagName('p');
//output
const current_received_output_a_div = document.getElementById('level_num_box_output_a');
let current_received_output_a_para = current_received_output_a_div.getElementsByTagName('p');
const current_received_output_b_div = document.getElementById('level_num_box_output_b');
let current_received_output_b_para = current_received_output_b_div.getElementsByTagName('p');
const current_received_output_c_div = document.getElementById('level_num_box_output_c');
let current_received_output_c_para = current_received_output_c_div.getElementsByTagName('p');
const current_expected_output_a = document.getElementById('level_num_box_expected_a');
let current_expected_output_a_para = current_expected_output_a.getElementsByTagName('p');
const current_expected_output_b = document.getElementById('level_num_box_expected_b');
let current_expected_output_b_para = current_expected_output_b.getElementsByTagName('p');
const current_expected_output_c = document.getElementById('level_num_box_expected_c');
let current_expected_output_c_para = current_expected_output_c.getElementsByTagName('p');
Array.from(current_output_a_para).forEach(p => p.remove());
Array.from(current_output_b_para).forEach(p => p.remove());
Array.from(current_output_c_para).forEach(p => p.remove());
Array.from(current_received_output_a_para).forEach(p => p.remove());
Array.from(current_received_output_b_para).forEach(p => p.remove());
Array.from(current_received_output_c_para).forEach(p => p.remove());
Array.from(current_expected_output_a_para).forEach(p => p.remove());
Array.from(current_expected_output_b_para).forEach(p => p.remove());
Array.from(current_expected_output_c_para).forEach(p => p.remove());
//fill out user data boxes with empty values
const numberBoxoutputa = document.getElementById("level_num_box_output_a");
const numberBoxoutputb = document.getElementById("level_num_box_output_b");
const numberBoxoutputc = document.getElementById("level_num_box_output_c");
const in_header_a = document.getElementById("in_a");
const in_header_b = document.getElementById("in_b");
const in_header_c = document.getElementById("in_c");
const out_header_a = document.getElementById("out_a");
const out_header_b = document.getElementById("out_b");
const out_header_c = document.getElementById("out_c");
//DISPLAY DATA
const numberBox = document.getElementById("level_num_box_in_a");
if (INPUT_STACK_A.length < 1) {
for (i = 0; i < 36; i++) {
const numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = "...";
numberBox.className = "null_number";
numberBox.appendChild(numberElement);
in_header_a.className = "disabled_level_header";
}
}
INPUT_STACK_A.forEach(number => {
const numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = number;
numberBox.className = "level_num_box";
numberBox.appendChild(numberElement);
in_header_a.className = "level_header";
})
const numberBoxb = document.getElementById("level_num_box_in_b");
if (INPUT_STACK_B.length < 1) {
for (i = 0; i < 36; i++) {
const numberElement = document.createElement('p');
numberElement.className = '.number';
numberBoxb.className = "null_number";
numberElement.textContent = "...";
numberBoxb.appendChild(numberElement);
in_header_b.className = "disabled_level_header";
}
}
INPUT_STACK_B.forEach(number => {
const numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = number;
numberBoxb.appendChild(numberElement);
numberBoxb.className = "level_num_box";
in_header_b.className = "level_header";
})
const numberBoxc = document.getElementById("level_num_box_in_c");
if (INPUT_STACK_C.length < 1) {
for (i = 0; i < 36; i++) {
const numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = "...";
numberBoxc.className = "null_number";
numberBoxc.appendChild(numberElement);
in_header_c.className = "disabled_level_header";
}
}
INPUT_STACK_C.forEach(number => {
const numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = number;
numberBoxc.appendChild(numberElement);
numberBoxc.className = "level_num_box";
in_header_c.className = "level_header";
})
const numberBoxoa = document.getElementById("level_num_box_expected_a");
if (EXPECTED_OUTPUT_A.length < 1) {
for (i = 0; i < 36; i++) {
const numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = "...";
numberBoxoa.className = "null_number";
numberBoxoa.appendChild(numberElement);
numberBoxoutputa.className = "null_number";
out_header_a.className = "disabled_level_header";
}
}
EXPECTED_OUTPUT_A.forEach(number => {
const numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = number;
numberBoxoa.appendChild(numberElement);
numberBoxoa.className = "level_num_box";
numberBoxoutputa.className = "level_num_box";
out_header_a.className = "level_header";
})
const numberBoxob = document.getElementById("level_num_box_expected_b");
if (EXPECTED_OUTPUT_B.length < 1) {
for (i = 0; i < 36; i++) {
const numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = "...";
numberBoxob.className = "null_number";
numberBoxob.appendChild(numberElement);
numberBoxoutputb.className = "null_number";
out_header_b.className = "disabled_level_header";
}
}
EXPECTED_OUTPUT_B.forEach(number => {
const numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = number;
numberBoxob.appendChild(numberElement);
numberBoxob.className = "level_num_box";
numberBoxoutputb.className = "level_num_box";
out_header_b.className = "level_header";
})
const numberBoxoc = document.getElementById("level_num_box_expected_c");
if (EXPECTED_OUTPUT_C.length < 1) {
for (i = 0; i < 36; i++) {
const numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = "...";
numberBoxoc.className = "null_number";
numberBoxoc.appendChild(numberElement);
numberBoxoutputc.className = "null_number";
out_header_c.className = "disabled_level_header";
}
}
EXPECTED_OUTPUT_C.forEach(number => {
const numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = number;
numberBoxoc.appendChild(numberElement);
numberBoxoc.className = "level_num_box";
numberBoxoutputc.className = "level_num_box";
out_header_c.className = "level_header";
})
for (i = 0; i < 36; i++) {
let numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = " . ";
numberBoxoutputa.appendChild(numberElement);
numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = " . ";
numberBoxoutputb.appendChild(numberElement);
numberElement = document.createElement('p');
numberElement.className = '.number';
numberElement.textContent = " . ";
numberBoxoutputc.appendChild(numberElement);
}
}
// Function to place the caret at the end of the contenteditable div
function placeCaretAtEnd(el) {
el.focus();
if (typeof window.getSelection != 'undefined' &&
typeof document.createRange != 'undefined') {
const range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
}
function node_send_hang_state_check(node_name) {
const in_node = current_nodes.find(({
name
}) => name === node_name);
if (current_connection_port_data.length > 0) {
for (let i = 0; i < current_connection_port_data.length; i++) {
const [from, to, direction, ddata] = current_connection_port_data[i];
if (from == node_name) {
//still in hang send state
return true;
}
}
} else {
//no longer in hang state
in_node.hang_send_state = false;
return false;
}
in_node.hang_send_state = false;
return false;
}
function arrays_Equal(arr1, arr2) {
// Check if arrays are the same length
if (arr1.length !== arr2.length) {
return false;
}
// Compare elements
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}
function process_level_conditions() {
let a = false;
let b = false;
let c = false;
if (EXPECTED_OUTPUT_A.length > 0) {
if (arrays_Equal(OUTPUT_STACK_A, EXPECTED_OUTPUT_A)) {
//console.log("EQUAL")
a = true;
}
} else {
a = true;
}
if (EXPECTED_OUTPUT_B.length > 0) {
if (arrays_Equal(OUTPUT_STACK_B, EXPECTED_OUTPUT_B)) {
//console.log("EQUAL")
b = true;
}
} else {
b = true;
}
if (EXPECTED_OUTPUT_C.length > 0) {
if (arrays_Equal(OUTPUT_STACK_C, EXPECTED_OUTPUT_C)) {
//console.log("EQUAL")
c = true;
}
} else {
c = true;
}
if (a == true && b == true && c == true) {
save_level_completion_data();
save_level_data();
document.getElementById("win_modal").style.display = "block";
}
}
function process_level() {
//push numbers to the inputs if the previous numbers have been taken
if (INPUT_STACK_A.length > 0) {
a = false;
//check to see if there is data from this node in the sent list
if (current_connection_port_data.length > 0) {
for (let i = 0; i < current_connection_port_data.length; i++) {
const [from, to, direction, ddata] = current_connection_port_data[i];
if (from == 11) {
//still has number waiting to be recieved
a = true;
}
}
}
if (a == false) {
//send a data to be recieved & pop it from the send list
current_connection_port_data.push([11, 1, "DOWN", INPUT_STACK_A.shift()]);
}
}
if (INPUT_STACK_B.length > 0) {
a = false;
//check to see if there is data from this node in the sent list
if (current_connection_port_data.length > 0) {
for (let i = 0; i < current_connection_port_data.length; i++) {
const [from, to, direction, ddata] = current_connection_port_data[i];
if (from == 22) {
//still has number waiting to be recieved
a = true;
}
}
}
if (a == false) {
//send a data to be recieved & pop it from the send list
current_connection_port_data.push([22, 2, "DOWN", INPUT_STACK_B.shift()]);
}
}
if (INPUT_STACK_C.length > 0) {
a = false;
//check to see if there is data from this node in the sent list
if (current_connection_port_data.length > 0) {
for (let i = 0; i < current_connection_port_data.length; i++) {
const [from, to, direction, ddata] = current_connection_port_data[i];
if (from == 33) {
//still has number waiting to be recieved
a = true;
}
}
}
if (a == false) {
//send a data to be recieved & pop it from the send list
current_connection_port_data.push([33, 3, "DOWN", INPUT_STACK_C.shift()]);
}
}
//OUTPUT
//recieve numbers from outputs if some are waiting
if (EXPECTED_OUTPUT_A.length > 0) {
if (current_connection_port_data.length > 0) {
for (let i = 0; i < current_connection_port_data.length; i++) {
const [from, to, direction, ddata] = current_connection_port_data[i];
if (7 == from && 111 == to && "DOWN" == direction) {
if (OUTPUT_STACK_A.length < EXPECTED_OUTPUT_A.length) {
OUTPUT_STACK_A.push(ddata);
// Remove the found entry
current_connection_port_data.splice(i, 1);
}
}
}
}
}
if (EXPECTED_OUTPUT_B.length > 0) {
if (current_connection_port_data.length > 0) {
for (let i = 0; i < current_connection_port_data.length; i++) {
const [from, to, direction, ddata] = current_connection_port_data[i];
if (8 == from && 222 == to && "DOWN" == direction) {
if (OUTPUT_STACK_B.length < EXPECTED_OUTPUT_B.length) {
OUTPUT_STACK_B.push(ddata);
// Remove the found entry
current_connection_port_data.splice(i, 1);
}
}
}
}
}
if (EXPECTED_OUTPUT_C.length > 0) {
if (current_connection_port_data.length > 0) {
for (let i = 0; i < current_connection_port_data.length; i++) {
const [from, to, direction, ddata] = current_connection_port_data[i];
if (9 == from && 333 == to && "DOWN" == direction) {
if (OUTPUT_STACK_C.length < EXPECTED_OUTPUT_C.length) {
OUTPUT_STACK_C.push(ddata);
// Remove the found entry
current_connection_port_data.splice(i, 1);
return;
}
}
}
}
}
}
function find_instruction(node_name) {
const in_node = current_nodes.find(({
name
}) => name === node_name);
let ln;
//change the color back to default for our current line
clear_instruction_colors(in_node.name);
if (in_node.label_jump_instruction_line != -1) {
in_node.current_instruction_line = in_node.label_jump_instruction_line;
in_node.label_jump_instruction_line = -1;
} else if (in_node.hang_receive_state == false) {
in_node.current_instruction_line = in_node.current_instruction_line + 1;
}
let found_instruction = "";
let found_instruction_line = "";
let starting_line = in_node.current_instruction_line - 1;
for (i = 0; i < 15; i++) {
starting_line = starting_line + 1;
if (starting_line > 14) {
starting_line = 0
} else if (starting_line < 0) {
starting_line = 0;
}
ln = "NODE" + in_node.name + "_INSTRUCTION" + starting_line + "_CODE_LINE";
const line = document.getElementById(ln);
const text = line.innerText.trim().toUpperCase();
const words = text.split(/\s+/);
for (let word of words) {
//Check first char for breakpoint, comment ! #
//Check first char for label :
if (word[0] == "!") {
found_instruction = "!";
found_instruction_line = text;
//console.log("HAS BREAKPOINT-!", word)
break;
} else if (word[0] == "#") {
found_instruction = "#";
found_instruction_line = text;
//console.log("HAS COMMENT - #", word)
break;
} else if (word[word.length - 1] == ":") {
//CHECK TO SEE IF THIS LABEL HAS INSTRUTIONS AFTER IT
//L: MOVE 8 RIGHT
solo = true;
to_remove = word;
for (let word of words) {
if (VALID_INSTRUCTIONS.has(word)) {
solo = false;
found_instruction = word;
const new_words = words.filter(word => word.toUpperCase() !== to_remove.toUpperCase());
const new_text = new_words.join(' ');
found_instruction_line = new_text;
break;
}
}
if (solo == false) {
break;
}
//LABEL LINES SHOULD BE SKIPPED SO WE DO NOT BREAK
} else if (VALID_INSTRUCTIONS.has(word)) {
found_instruction = word;
found_instruction_line = text;
//console.log("HAS CODE-", word)
break;
} else if (word == "JRO") {
console.log("IMPLEMENT JRO DUM DUM");
}
}
if (found_instruction != "") {
break;
}
}
//set the current instruction line
in_node.current_instruction_line = starting_line;
//change the line color
if (found_instruction != "") {
ln = "NODE" + in_node.name + "_INSTRUCTION" + starting_line + "_CODE_LINE";
document.getElementById(ln).style.backgroundColor = 'grey';
}
let ff = []
ff.push(found_instruction);
ff.push(found_instruction_line);
return ff;
}
function search_for_label(node_name, label_name) {
const in_node = current_nodes.find(({
name
}) => name === node_name);
let starting_line = in_node.current_instruction_line;
let label_search = label_name + ":";
for (i = 0; i < 15; i++) {
starting_line = starting_line + 1;
if (starting_line > 14) {
starting_line = 0
}
ln = "NODE" + in_node.name + "_INSTRUCTION" + starting_line + "_CODE_LINE";
const line = document.getElementById(ln);
const text = line.innerText.trim().toUpperCase();
const words = text.split(/\s+/);
for (let word of words) {
if (word[word.length - 1] == ":" && word == label_search) {
in_node.label_jump_instruction_line = starting_line;
return;
}
}
}
}
function inst_jmp(node_name, inst) {
//JMP CATS
//search for label
let i = find_src(inst[1], 1);
search_for_label(node_name, i[1]);
}
function inst_jez(node_name, inst) {
const in_node = current_nodes.find(({
name
}) => name === node_name);
if (in_node.ACC == 0) {
let i = find_src(inst[1], 1);
search_for_label(node_name, i[1]);
}
}