-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenges.pas
979 lines (865 loc) · 33.6 KB
/
challenges.pas
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
UNIT challenges;
{$mode objfpc}{$H+}
INTERFACE
USES
compoundGates,logicalGates,visualGates,serializationUtil,paletteHandling;
CONST MAX_NUMBER_OF_CHALLENGE_CHECKS=256;
TYPE
P_challenge=^T_challenge;
P_testCreator=^T_testCreator;
T_timingDecils=array[0..10] of longint;
F_caseUpdatedCallback=PROCEDURE(CONST index:longint) of object;
P_TestCreationThread=^T_testCreationThread;
{ T_testCreationThread }
T_testCreationThread=object
private
lastPreparedIndex:longint;
PROCEDURE setLastPreparedIndex(CONST index:longint);
public
parent:P_testCreator;
initialRun,continueEvaluation,running:boolean;
CONSTRUCTOR create(CONST parent_:P_testCreator);
DESTRUCTOR destroy;
PROCEDURE restart;
PROCEDURE ensureStop;
PROPERTY getLastPreparedIndex:longint read lastPreparedIndex;
end;
{ T_testCreator }
T_testCreator=object(T_serializable)
private
challengeTestCreationThread:T_testCreationThread;
PROCEDURE updateTestCaseResults(CONST callback:F_caseUpdatedCallback; CONST resume:PBoolean; CONST initCounts:boolean=false);
public
behavior :P_compoundGate;
tests:array of record
inputs:T_wireValueArray;
actuallyActive,
maxTotalSteps:longint;
outputs:T_wireValueArray;
end;
Interfaces: T_gateInterfaces;
CONSTRUCTOR createAsChallengeBasis;
CONSTRUCTOR createForAnalysis(CONST visualBoard:P_visualBoard);
DESTRUCTOR destroy;
PROCEDURE setNumberOfTestCases(CONST count:longint);
PROCEDURE generateTestCases(CONST continuous:boolean=false; CONST scramble:boolean=true);
PROCEDURE reInitStepCounts;
PROCEDURE updateTestCaseResults;
PROPERTY lastTestCasePrepared:longint read challengeTestCreationThread.lastPreparedIndex;
PROCEDURE copyTestInputs(CONST origin:P_testCreator);
FUNCTION getTimingDecils:T_timingDecils;
end;
{ T_challenge }
T_challenge=object(T_testCreator)
private
testRun:record
succeeded:boolean;
active:boolean;
testInputIndex:longint;
testStep:longint;
end;
public
marked:boolean; //for export dialog only
challengeLevel :byte;
callengeCompleted :boolean;
resultTemplate,
expectedBehavior :P_visualBoard;
editable:boolean;
palette :P_challengePalette;
challengeTitle :string;
challengeDescription:string;
CONSTRUCTOR create;
DESTRUCTOR destroy; virtual;
FUNCTION clone:P_challenge;
FUNCTION getSerialVersion:dword; virtual;
FUNCTION loadFromStream(VAR stream:T_bufferedInputStreamWrapper):boolean; virtual;
PROCEDURE saveToStream(VAR stream:T_bufferedOutputStreamWrapper); virtual;
FUNCTION resetChallenge:P_visualBoard; virtual;
PROCEDURE startTesting(CONST board:P_visualBoard); virtual;
PROPERTY currentlyTesting:boolean read testRun.active;
PROPERTY testSucceeded:boolean read testRun.succeeded;
FUNCTION testStep(CONST count, timeOutInTicks: longint; CONST board: P_visualBoard): longint; virtual;
FUNCTION getInfoLabelText(CONST uiIdle:boolean):string; virtual;
FUNCTION ensureBehavior:P_compoundGate;
PROCEDURE dropBehavior;
//For creation purposes...
PROCEDURE initNewChallenge(CONST expectedAsVisual:P_visualBoard);
FUNCTION equals(CONST c:P_challenge):boolean; virtual;
end;
{ T_tutorial }
T_tutorial=object(T_challenge)
tutorialStep:byte;
workspaceBoard:P_visualBoard;
unchangedInput:T_wireValue;
oldIoMode: T_multibitWireRepresentation;
CONSTRUCTOR create;
DESTRUCTOR destroy; virtual;
FUNCTION resetChallenge:P_visualBoard; virtual;
PROCEDURE startTesting(CONST board:P_visualBoard); virtual;
FUNCTION testStep(CONST count, timeOutInTicks: longint; CONST board: P_visualBoard): longint; virtual;
FUNCTION getInfoLabelText(CONST uiIdle:boolean):string; virtual;
FUNCTION equals(CONST c:P_challenge):boolean; virtual;
end;
{ T_challengeSet }
P_challengeSet=^T_challengeSet;
T_challengeSet=object(T_serializable)
private
PROCEDURE addCopyForExport(CONST c:P_challenge; CONST exportEditable:boolean);
public
challenge:array of P_challenge;
CONSTRUCTOR create;
DESTRUCTOR destroy;
PROCEDURE markAllAsPending;
FUNCTION getSerialVersion:dword; virtual;
FUNCTION loadFromStream(VAR stream:T_bufferedInputStreamWrapper):boolean; virtual;
PROCEDURE saveToStream(VAR stream:T_bufferedOutputStreamWrapper); virtual;
FUNCTION add(CONST c:P_challenge):boolean;
PROCEDURE moveChallenge(CONST index:longint; CONST up:boolean);
PROCEDURE exportSelected (CONST fileName:string; CONST exportEditable:boolean);
FUNCTION importChallenges(CONST fileName:string; CONST overwriteExisting:boolean):boolean;
end;
CONST checkMark='✓';
VAR tutorial:T_tutorial;
IMPLEMENTATION
USES sysutils,myGenerics;
{ T_tutorial }
CONSTRUCTOR T_tutorial.create;
VAR tempBoard:T_visualBoard;
input:P_visualGate;
output:P_visualGate;
adapter:P_visualGate;
andGate:P_visualGate;
tempBehavior:P_abstractGate;
begin
inherited;
tutorialStep:=0;
challengeTitle :='Tutorial';
challengeDescription:='Eine Grundlegende Erklärung der Bedienung';
challengeLevel :=0;
callengeCompleted :=false;
editable :=false;
tempBoard.create(palette);
tempBehavior:=newBaseGate(gt_input);
P_inputGate(tempBehavior)^.width:=4;
P_inputGate(tempBehavior)^.reset;
new(input,create(tempBehavior));
tempBoard.elementAdded(input,true);
new(output,create(newBaseGate(gt_output)));
tempBoard.elementAdded(output,true);
new(adapter,create(newBaseGate(gt_adapter)));
tempBoard.elementAdded(adapter,true);
tempBehavior:=newBaseGate(gt_andGate);
P_andGate(tempBehavior)^.inputCount:=4;
new(andGate,create(tempBehavior));
tempBoard.elementAdded(andGate,true);
tempBoard.addWire(input,0,adapter,0);
tempBoard.addWire(adapter,0,andGate,0);
tempBoard.addWire(adapter,1,andGate,1);
tempBoard.addWire(adapter,2,andGate,2);
tempBoard.addWire(adapter,3,andGate,3);
tempBoard.addWire(andGate,0,output,0);
Interfaces:=tempBoard.getInterfaces;
behavior:=tempBoard.extractBehavior;
tempBoard.destroy;
setNumberOfTestCases(16);
generateTestCases(true,false);
end;
DESTRUCTOR T_tutorial.destroy;
begin
inherited;
end;
FUNCTION T_tutorial.resetChallenge: P_visualBoard;
VAR tmp:P_abstractGate;
begin
result:=inherited;
tutorialStep:=0;
palette^.clear;
tmp:=newBaseGate(gt_input);
tmp^.reset;
palette^.paletteOption:=co_unconfiguredPaletteWithCounts;
palette^.ensureBaseGate(tmp);
palette^.countUpGate(tmp);
dispose(tmp,destroy);
workspaceBoard:=result;
end;
PROCEDURE T_tutorial.startTesting(CONST board: P_visualBoard);
begin
if tutorialStep=12 then inherited;
end;
FUNCTION T_tutorial.testStep(CONST count, timeOutInTicks: longint; CONST board: P_visualBoard): longint;
begin
if tutorialStep=12 then result:=inherited;
end;
FUNCTION T_tutorial.getInfoLabelText(CONST uiIdle:boolean): string;
CONST stepText:array[0..12] of string=(
{0} 'Auf der linken Seite siehst du die Palette.'+LineEnding+'Du kannst Bauelemente zur Schaltung hinzufügen, indem du sie mit der linken Maustaste'+LineEnding+'von der Palette auf die Schaltung ziehst.',
{1} 'Eingänge werden immer am oberen oder linken Rand positioniert,'+LineEnding+'Ausgänge immer am unteren oder rechten.'+LineEnding+'Du kannst das Element wieder entfernen, indem du es zurück zur Palette ziehst oder es markierst und Entf drückst.',
{2} 'Jetzt fügen wir einen Eingang und einen Ausgang hinzu.',
{3} 'Du kannst Kabel erstellen, indem du auf einen Ausgang eines Bauelements links-klickst.'+LineEnding+'Ein Linksklick auf ein bestehendes Kabel entfernt dieses wieder.',
{4} 'Mit einem Rechtsklick auf einen Ein- oder Ausgang kannst du beobachten, was genau dort passiert.',
{5} 'Du kannst den Wert eines Eingangs ändern, indem du ihn mit Klick auf seine untere Hälfte bearbeitest.'+LineEnding+
'Beendest du die Eingabe mit Enter, wird sie wieder geschlossen; beendest du sie mit einem Leerzeichen, wird sie übernommen ohne zu schließen',
{6} 'Alle Bits eines Eingangs werden umgeschaltet, wenn du die in der oberen rechten Ecke des Eingangs angegebene Taste drückst'+LineEnding+'Mit Shift+[Taste] öffnest du den Editor',
{7} 'Du kannst die Eigenschaften des Eingangs verändern, indem du auf seine obere Hälfte rechts-klickst.'+LineEnding+
'Setze "Breite Ausgang" auf 4!'+LineEnding+
'Erlaubte Breiten sind: 1, 4, 8 und 16',
{8} 'Das Kabel ist verschwunden, weil die Anschlüsse unterschiedliche Breiten haben.'+LineEnding+
'Dafür gibt es Adapter. Füge den Adapter hinzu und verbinde Eingang mit Adapter!',
{9} 'Jetzt fügen wir ein Und-Gatter hinzu und setzen dort (Rechtsklick) "Anzahl Eingänge" auf 4.',
{10} 'Verbinde nun jeden Ausgang des Adapters mit einem Eingang des "and"-Elements und den Ausgang des "and"-Elements mit dem Ausgang'+LineEnding+
'Du hast damit eine Schaltung erstellt, die genau dann 1 ausgibt wenn alle Eingänge 1 sind.',
{11} 'Eine Sache noch: Für breitere Eingänge kannst du die Darstellungsform'+LineEnding+
'per Rechtsklick auf dessen untere Hälfte umschalten.',
{12} 'Mit einem Klick auf "test" startest du einen Vergleich Deiner Schaltung mit dem vordefinierten Verhalten.'+LineEnding+'Das Beendet das Tutorial.');
FUNCTION stepCompleted:boolean;
VAR tmp:P_abstractGate;
begin
case tutorialStep of
0: begin
result:=gatesTotal(workspaceBoard^.getGateCount)=1;
end;
1: begin
result:=gatesTotal(workspaceBoard^.getGateCount)=0;
if result then begin
tmp:=newBaseGate(gt_output);
palette^.ensureBaseGate(tmp);
palette^.countUpGate(tmp);
dispose(tmp,destroy);
end;
end;
2: begin
result:=(length(workspaceBoard^.inputs)=1) and
(length(workspaceBoard^.outputs)=1);
end;
3: begin
result:=(length(workspaceBoard^.inputs)=1) and
(length(workspaceBoard^.outputs)=1) and
(length(workspaceBoard^.wires)=1);
end;
4: begin
result:=(length(workspaceBoard^.inputs)=1) and
(length(workspaceBoard^.outputs)=1) and
(length(workspaceBoard^.wires)=1) and
(length(workspaceBoard^.watchers)>0);
if result then begin
unchangedInput.width:=1;
unchangedInput.bit[0]:=tsv_false;
workspaceBoard^.inputs[0]^.getBehavior^.setInput(0,unchangedInput);
end;
end;
5,6: begin
result:=(length(workspaceBoard^.inputs)=1) and
(length(workspaceBoard^.outputs)=1) and
(length(workspaceBoard^.wires)=1) and
(unchangedInput<>workspaceBoard^.inputs[0]^.getBehavior^.getInput(0));
if result then unchangedInput:=workspaceBoard^.inputs[0]^.getBehavior^.getInput(0);
end;
7: begin
result:=(length(workspaceBoard^.inputs)=1) and
(length(workspaceBoard^.outputs)=1) and
(workspaceBoard^.inputs[0]^.getBehavior^.outputWidth(0)=4);
if result then begin
tmp:=newBaseGate(gt_adapter);
palette^.ensureBaseGate(tmp);
palette^.countUpGate(tmp);
dispose(tmp,destroy);
workspaceBoard^.inputs[0]^.fixedProperties:=true;
end;
end;
8: begin
result:=(length(workspaceBoard^.inputs)=1) and
(length(workspaceBoard^.outputs)=1) and
(length(workspaceBoard^.wires)=1) and
(length(workspaceBoard^.gates)=1) and
(workspaceBoard^.isInputConnected(workspaceBoard^.gates[0],0));
if result then begin
tmp:=newBaseGate(gt_andGate);
palette^.ensureBaseGate(tmp);
palette^.countUpGate(tmp);
dispose(tmp,destroy);
workspaceBoard^.gates[0]^.fixedProperties:=true;
end;
end;
9: begin
result:=(length(workspaceBoard^.inputs)=1) and
(length(workspaceBoard^.outputs)=1) and
(length(workspaceBoard^.wires)=1) and
(length(workspaceBoard^.gates)=2) and
(workspaceBoard^.isInputConnected(workspaceBoard^.gates[0],0)) and
(workspaceBoard^.gates[1]^.getBehavior^.numberOfInputs=4);
end;
10: begin
result:=(length(workspaceBoard^.inputs)=1) and
(length(workspaceBoard^.outputs)=1) and
(length(workspaceBoard^.wires)=6) and
(length(workspaceBoard^.gates)=2) and
(workspaceBoard^.gates[1]^.getBehavior^.numberOfInputs=4) and
workspaceBoard^.isInputConnected(workspaceBoard^.gates[0],0) and
workspaceBoard^.isInputConnected(workspaceBoard^.gates[1],0) and
workspaceBoard^.isInputConnected(workspaceBoard^.gates[1],1) and
workspaceBoard^.isInputConnected(workspaceBoard^.gates[1],2) and
workspaceBoard^.isInputConnected(workspaceBoard^.gates[1],3) and
workspaceBoard^.isInputConnected(workspaceBoard^.outputs[0],0);
oldIoMode:=workspaceBoard^.inputs[0]^.ioMode;
end;
11: begin
result:=(length(workspaceBoard^.inputs)=1) and
(length(workspaceBoard^.outputs)=1) and
(length(workspaceBoard^.wires)=6) and
(length(workspaceBoard^.gates)=2) and
(workspaceBoard^.gates[1]^.getBehavior^.numberOfInputs=4) and
workspaceBoard^.isInputConnected(workspaceBoard^.gates[0],0) and
workspaceBoard^.isInputConnected(workspaceBoard^.gates[1],0) and
workspaceBoard^.isInputConnected(workspaceBoard^.gates[1],1) and
workspaceBoard^.isInputConnected(workspaceBoard^.gates[1],2) and
workspaceBoard^.isInputConnected(workspaceBoard^.gates[1],3) and
workspaceBoard^.isInputConnected(workspaceBoard^.outputs[0],0) and
(workspaceBoard^.inputs[0]^.ioMode<>oldIoMode);
end;
else result:=false;
end;
end;
begin
if uiIdle and stepCompleted then inc(tutorialStep);
if (tutorialStep=12) and currentlyTesting
then result:=inherited
else result:=stepText[tutorialStep];
end;
FUNCTION T_tutorial.equals(CONST c: P_challenge): boolean;
begin
//Singleton equality
result:= c=P_challenge(@self);
end;
{ T_testCreationThread }
PROCEDURE T_testCreationThread.setLastPreparedIndex(CONST index: longint);
begin
lastPreparedIndex:=index;
end;
CONSTRUCTOR T_testCreationThread.create(CONST parent_: P_testCreator);
begin
parent:=parent_;
initialRun:=true;
continueEvaluation:=true;
running:=false;
lastPreparedIndex:=-1;
end;
DESTRUCTOR T_testCreationThread.destroy;
begin
ensureStop;
end;
FUNCTION challengeTestCreation(p:pointer):ptrint; Register;
begin
with P_TestCreationThread(p)^ do begin
parent^.updateTestCaseResults(@setLastPreparedIndex,@continueEvaluation,initialRun);
if continueEvaluation then initialRun:=false;
running:=false;
end;
result:=0;
end;
PROCEDURE T_testCreationThread.restart;
begin
ensureStop;
running:=true; continueEvaluation:=true;
beginThread(@challengeTestCreation,@self);
end;
PROCEDURE T_testCreationThread.ensureStop;
begin
while running do begin
continueEvaluation:=false;
sleep(1);
end;
end;
{ T_challengeSet }
CONSTRUCTOR T_challengeSet.create;
begin
setLength(challenge,1);
challenge[0]:=@tutorial;
end;
DESTRUCTOR T_challengeSet.destroy;
VAR i:longint;
begin
for i:=0 to length(challenge)-1 do if not(tutorial.equals(challenge[i])) then dispose(challenge[i],destroy);
setLength(challenge,0);
end;
PROCEDURE T_challengeSet.markAllAsPending;
VAR i:longint;
begin
for i:=0 to length(challenge)-1 do challenge[i]^.callengeCompleted:=false;
end;
FUNCTION T_challengeSet.getSerialVersion: dword;
begin
result:=serialVersionOf('T_challengeSet',2);
end;
FUNCTION T_challengeSet.loadFromStream(VAR stream: T_bufferedInputStreamWrapper): boolean;
VAR i:longint;
begin
if not inherited then exit(false);
if length(challenge)<>1 then exit(false);
tutorial.callengeCompleted:=stream.readBoolean;
setLength(challenge,stream.readNaturalNumber+1);
result:=stream.allOkay;
for i:=1 to length(challenge)-1 do begin
new(challenge[i],create);
result:=result and challenge[i]^.loadFromStream(stream);
{$ifdef debugMode}
writeln('Read challenge #',i,'/',length(challenge),' from stream: ',result);
{$endif}
end;
end;
PROCEDURE T_challengeSet.saveToStream(VAR stream: T_bufferedOutputStreamWrapper);
VAR i:longint;
begin
inherited;
stream.writeBoolean(tutorial.callengeCompleted);
stream.writeNaturalNumber(length(challenge)-1);
for i:=0 to length(challenge)-1 do if not(tutorial.equals(challenge[i])) then begin
challenge[i]^.saveToStream(stream);
end;
end;
FUNCTION T_challengeSet.add(CONST c: P_challenge): boolean;
VAR q:P_challenge;
begin
for q in challenge do if q^.equals(c) then exit(false);
result:=true;
setLength(challenge,length(challenge)+1);
challenge[length(challenge)-1]:=c;
c^.challengeTestCreationThread.ensureStop;
end;
PROCEDURE T_challengeSet.addCopyForExport(CONST c:P_challenge; CONST exportEditable:boolean);
begin
setLength(challenge,length(challenge)+1);
challenge[length(challenge)-1]:=c^.clone;
if not(exportEditable) then challenge[length(challenge)-1]^.editable:=false;
end;
PROCEDURE T_challengeSet.moveChallenge(CONST index: longint; CONST up: boolean);
VAR tmp:P_challenge;
newIndex:longint;
begin
if up
then newIndex:=index-1
else newIndex:=index+1;
if (index>=0) and (index<length(challenge)) and (newIndex>=0) and (newIndex<length(challenge)) then begin
tmp :=challenge[ index];
challenge[ index]:=challenge[newIndex];
challenge[newIndex]:=tmp;
end;
end;
PROCEDURE T_challengeSet.exportSelected(CONST fileName:string; CONST exportEditable:boolean);
VAR i:longint;
temp:T_challengeSet;
begin
temp.create;
for i:=0 to length(challenge)-1 do if challenge[i]^.marked then temp.addCopyForExport(challenge[i],exportEditable);
temp.saveToFile(fileName);
for i:=0 to length(temp.challenge)-1 do if not(tutorial.equals(temp.challenge[i])) then dispose(temp.challenge[i],destroy);
setLength(temp.challenge,0);
temp.destroy;
end;
FUNCTION T_challengeSet.importChallenges(CONST fileName:string; CONST overwriteExisting:boolean):boolean;
VAR i:longint;
temp:T_challengeSet;
begin
temp.create;
if not(temp.loadFromFile(fileName)) or (length(temp.challenge)=0) then begin
temp.destroy;
exit(false);
end;
temp.markAllAsPending;
if overwriteExisting then begin
for i:=0 to length(challenge)-1 do if not tutorial.equals(temp.challenge[i]) then dispose(challenge[i],destroy);
setLength(challenge,length(temp.challenge));
for i:=0 to length(challenge)-1 do challenge[i]:=temp.challenge[i];
end else begin
for i:=0 to length(temp.challenge)-1 do
if not add(temp.challenge[i]) and not tutorial.equals(temp.challenge[i])
then dispose(temp.challenge[i],destroy);
end;
setLength(temp.challenge,0);
temp.destroy;
result:=true;
end;
{ T_challenge }
CONSTRUCTOR T_challenge.create;
begin
inherited createAsChallengeBasis;
challengeLevel :=255;
callengeCompleted :=false;
challengeTitle :='';
challengeDescription:='';
new(palette,create);
new(resultTemplate ,create(palette));
new(expectedBehavior,create(palette));
testRun.active:=false;
end;
DESTRUCTOR T_challenge.destroy;
begin
inherited;
dispose(resultTemplate,destroy);
dispose(expectedBehavior,destroy);
dispose(palette,destroy);
end;
FUNCTION T_challenge.clone: P_challenge;
VAR i,j:longint;
begin
new(result,create);
result^.challengeLevel :=challengeLevel ;
result^.callengeCompleted :=callengeCompleted ;
result^.editable :=editable ;
result^.challengeTitle :=challengeTitle ;
result^.challengeDescription:=challengeDescription;
dispose(result^.resultTemplate,destroy);
dispose(result^.expectedBehavior,destroy);
dispose(result^.palette,destroy);
result^.resultTemplate :=resultTemplate^.cloneAsTrueCopy;
result^.expectedBehavior:=expectedBehavior^.cloneAsTrueCopy;
result^.palette:=palette^.cloneAndMigrate(result^.resultTemplate,result^.expectedBehavior);
setLength(result^.tests,length(tests));
for i:=0 to length(tests)-1 do begin
setLength(result^.tests[i].inputs,
length( tests[i].inputs));
for j:=0 to length(tests[i].inputs)-1 do
result^.tests[i].inputs[j]
:=tests[i].inputs[j];
result^.tests[i].actuallyActive:=tests[i].actuallyActive;
result^.tests[i].maxTotalSteps :=tests[i].maxTotalSteps;
setLength(result^.tests[i].outputs,0); //not cloned, because it can be reproduced anyway
end;
end;
FUNCTION T_challenge.getSerialVersion: dword;
begin
result:=serialVersionOf('T_challenge',0);
end;
FUNCTION T_challenge.loadFromStream(VAR stream: T_bufferedInputStreamWrapper): boolean;
VAR i,j:longint;
testCount: qword;
begin
if not inherited then exit(false);
challengeLevel :=stream.readByte;
callengeCompleted :=stream.readBoolean;
editable :=stream.readBoolean;
challengeTitle :=stream.readAnsiString;
challengeDescription:=stream.readAnsiString;
if not(stream.allOkay) then exit(false);
result:=palette^.loadFromStream(stream)
and resultTemplate^.loadFromStream(stream,false)
and expectedBehavior^.loadFromStream(stream,false)
and stream.allOkay;
if not(result) then exit(result);
testCount:=stream.readNaturalNumber;
if (testCount>65536) or not(stream.allOkay) then exit(false);
setLength(tests,testCount);
for i:=0 to length(tests)-1 do with tests[i] do begin
setLength(inputs,length(expectedBehavior^.inputs));
for j:=0 to length(inputs)-1 do begin
inputs[j]:=deserialize(stream.readNaturalNumber);
end;
maxTotalSteps:=stream.readNaturalNumber;
end;
result:=result and stream.allOkay;
end;
PROCEDURE T_challenge.saveToStream(VAR stream: T_bufferedOutputStreamWrapper);
VAR i,j:longint;
begin
inherited;
stream.writeByte(challengeLevel);
stream.writeBoolean(callengeCompleted);
stream.writeBoolean(editable);
stream.writeAnsiString(challengeTitle);
stream.writeAnsiString(challengeDescription);
palette^.saveToStream(stream);
resultTemplate^.saveToStream(stream,false);
expectedBehavior^.saveToStream(stream,false);
stream.writeNaturalNumber(length(tests));
for i:=0 to length(tests)-1 do with tests[i] do begin
for j:=0 to length(expectedBehavior^.inputs)-1 do stream.writeNaturalNumber(serialize(inputs[j]));
stream.writeNaturalNumber(maxTotalSteps);
end;
end;
FUNCTION T_challenge.resetChallenge: P_visualBoard;
VAR board: P_visualBoard;
i:longint;
g:P_visualGate;
begin
board:=resultTemplate^.cloneAsTrueCopy(not(palette^.allowConfiguration));
for g in board^.inputs do g^.fixedPosition:=true;
for g in board^.outputs do g^.fixedPosition:=true;
for g in board^.gates do g^.fixedPosition:=true;
board^.setCaption(challengeTitle);
result:=board;
board^.reset(true);
palette^.resetCounts;
for i:=0 to length(palette^.paletteEntries)-1 do with palette^.paletteEntries[i] do
if prototype<>nil then prototype^.reset;
end;
PROCEDURE T_challenge.startTesting(CONST board: P_visualBoard);
begin
ensureBehavior;
with testRun do begin
active:=board^.interfacesMatch(behavior);
testInputIndex:=0;
testStep:=0;
if not(active) then exit;
end;
board ^.reset;
expectedBehavior^.reset;
board^.setInputs(tests[0].inputs);
end;
FUNCTION T_challenge.testStep(CONST count, timeOutInTicks: longint; CONST board: P_visualBoard): longint;
VAR stepsToSimulate: longint;
begin
with testRun do stepsToSimulate:=tests[testInputIndex].maxTotalSteps-testStep;
if count<stepsToSimulate then stepsToSimulate:=count;
{$ifdef debugMode}
writeln('Tests to simulate: ',count,'/',stepsToSimulate);
writeln('Simulating step ',testRun.testStep,' of test case #',testRun.testInputIndex);
{$endif}
result:=board^.coSimulateSteps(stepsToSimulate,timeOutInTicks,ensureBehavior);
testRun.testStep+=result;
if result=0 then inc(testRun.testStep);
if testRun.testStep>=tests[testRun.testInputIndex].maxTotalSteps then begin
if board^.outputsMatch(behavior) then begin
inc(testRun.testInputIndex);
if testRun.testInputIndex>=length(tests) then begin
testRun.active:=false;
testRun.succeeded:=true;
callengeCompleted:=true;
end else begin
testRun.testStep:=0;
board^.setInputs(tests[testRun.testInputIndex].inputs);
end;
end else begin
testRun.active:=false;
testRun.succeeded:=false;
end;
end;
end;
FUNCTION T_challenge.getInfoLabelText(CONST uiIdle: boolean): string;
begin
if testRun.active
then result:='Test '+intToStr(testRun.testInputIndex+1)+' von '+intToStr(length(tests))
else result:=challengeDescription;
end;
FUNCTION T_challenge.ensureBehavior:P_compoundGate;
begin
if behavior=nil then begin
Interfaces:=expectedBehavior^.getInterfaces;
behavior :=expectedBehavior^.extractBehavior;
end;
result:=behavior;
end;
PROCEDURE T_challenge.dropBehavior;
begin
if behavior=nil then exit;
challengeTestCreationThread.ensureStop;
dispose(behavior,destroy);
behavior:=nil;
end;
PROCEDURE T_challenge.initNewChallenge(CONST expectedAsVisual: P_visualBoard);
VAR
totalInputBits:longint=0;
i:longint;
begin
randomize;
challengeLevel :=0;
callengeCompleted :=false;
editable :=true;
challengeTitle :=expectedAsVisual^.getCaption;
challengeDescription:=expectedAsVisual^.getDescription;
if expectedBehavior<>nil then dispose(expectedBehavior,destroy);
if resultTemplate <>nil then dispose(resultTemplate,destroy);
if palette <>nil then dispose(palette,destroy);
new(palette,create);
palette^.constructingChallenge:=true;
expectedAsVisual^.extractChallenge(palette,expectedBehavior);
palette^.constructingChallenge:=false;
behavior :=expectedBehavior^.extractBehavior;
resultTemplate:=expectedBehavior^.cloneAsTrueCopy;
if length(tests)>0 then exit;
Interfaces:=expectedAsVisual^.getInterfaces;
for i:=0 to length(Interfaces.inputs)-1 do totalInputBits+=Interfaces.inputs[i].wireWidth;
i:=1;
while (totalInputBits>0) and (i<256) do begin
dec(totalInputBits);
i+=i;
end;
if i=1 then i+=4;
setLength(tests,i);
generateTestCases(totalInputBits=0);
end;
FUNCTION randomInput(CONST Interfaces:T_gateInterfaces):T_wireValueArray;
VAR i,k:longint;
begin
setLength(result,length(Interfaces.inputs));
for k:=0 to length(result)-1 do begin
result[k].width:=Interfaces.inputs[k].wireWidth;
for i:=0 to result[k].width-1 do
if random>0.5
then result[k].bit[i]:=tsv_true
else result[k].bit[i]:=tsv_false;
end;
end;
FUNCTION undeterminedOutput(CONST Interfaces:T_gateInterfaces):T_wireValueArray;
VAR i,k:longint;
begin
setLength(result,length(Interfaces.outputs));
for k:=0 to length(result)-1 do begin
result[k].width:=Interfaces.outputs[k].wireWidth;
for i:=0 to WIRE_MAX_WIDTH-1 do result[k].bit[i]:=tsv_undetermined;
end;
end;
PROCEDURE T_testCreator.setNumberOfTestCases(CONST count: longint);
VAR oldCount,i:longint;
begin
challengeTestCreationThread.ensureStop;
oldCount:=length(tests);
setLength(tests,count);
for i:=oldCount to count-1 do with tests[i] do begin
inputs:=randomInput(Interfaces);
outputs:=undeterminedOutput(Interfaces);
maxTotalSteps:=1000;
actuallyActive:=0;
end;
if (count>oldCount) or (challengeTestCreationThread.lastPreparedIndex<count-1)
then challengeTestCreationThread.restart;
end;
PROCEDURE T_testCreator.generateTestCases(CONST continuous:boolean=false; CONST scramble:boolean=true);
FUNCTION inputsByIndex(index:longint):T_wireValueArray;
VAR k,i:longint;
begin
setLength(result,length(Interfaces.inputs));
for k:=0 to length(result)-1 do begin
result[k].width:=Interfaces.inputs[k].wireWidth;
for i:=0 to result[k].width-1 do begin
if odd(index)
then result[k].bit[i]:=tsv_true
else result[k].bit[i]:=tsv_false;
index:=index shr 1;
end;
end;
end;
VAR i,j,k:longint;
begin
challengeTestCreationThread.ensureStop;
if continuous then begin
for i:=0 to length(tests)-1 do begin
tests[i].inputs:=inputsByIndex(i);
tests[i].outputs:=undeterminedOutput(Interfaces);
end;
if scramble then begin
k:=length(tests); setLength(tests,k+1);
for i:=1 to length(tests)-1 do for j:=0 to i-1 do if random<0.5 then begin
tests[k]:=tests[i]; tests[i]:=tests[j]; tests[j]:=tests[k];
end;
setLength(tests,k);
end;
end else begin
for i:=0 to length(tests)-1 do begin
tests[i].inputs:=randomInput(Interfaces);
tests[i].outputs:=undeterminedOutput(Interfaces);
end;
end;
challengeTestCreationThread.restart;
end;
PROCEDURE T_testCreator.reInitStepCounts;
begin
challengeTestCreationThread.ensureStop;
challengeTestCreationThread.initialRun:=true;
end;
PROCEDURE T_testCreator.updateTestCaseResults;
begin
challengeTestCreationThread.restart;
end;
PROCEDURE T_testCreator.copyTestInputs(CONST origin:P_testCreator);
VAR i:longint;
begin
challengeTestCreationThread.ensureStop;
setLength(tests,length(origin^.tests));
for i:=0 to length(tests)-1 do begin
tests[i].inputs :=origin^.tests[i].inputs;
tests[i].maxTotalSteps:=origin^.tests[i].maxTotalSteps;
end;
for i:=0 to length(Interfaces.outputs)-1 do Interfaces.outputs[i].representation:=origin^.Interfaces.outputs[i].representation;
challengeTestCreationThread.restart;
end;
FUNCTION T_testCreator.getTimingDecils:T_timingDecils;
VAR samples:T_arrayOfLongint;
i:longint;
j:longint=0;
begin
setLength(samples,length(tests));
for i:=0 to length(samples)-1 do if i<lastTestCasePrepared then begin
samples[i]:=tests[i].actuallyActive;
inc(j);
end;
setLength(samples,j);
if j=0 then for i:=0 to 10 do result[i]:=0
else begin
sort(samples);
for i:=0 to 10 do result[i]:=samples[round(i/10*(length(samples)-1))];
setLength(samples,0);
end;
end;
FUNCTION T_challenge.equals(CONST c: P_challenge): boolean;
VAR i,j:longint;
begin
result:=(c^.challengeLevel=challengeLevel)
and (c^.challengeTitle=challengeTitle)
and (c^.challengeDescription=challengeDescription)
and (c^.expectedBehavior^.equals(expectedBehavior))
and (length(c^.tests)=length(tests));
if result then for i:=0 to length(tests)-1 do with tests[i] do begin
result:=result
and (c^.tests[i].maxTotalSteps=maxTotalSteps)
and (length(c^.tests[i].inputs)=length(inputs));
for j:=0 to length(tests[i].inputs)-1 do
result:=result
and (c^.tests[i].inputs[j]=inputs[j]);
end;
end;
PROCEDURE T_testCreator.updateTestCaseResults(CONST callback: F_caseUpdatedCallback; CONST resume: PBoolean; CONST initCounts: boolean);
VAR i, stepsDone:longint;
begin
behavior^.reset;
for i:=0 to length(tests)-1 do begin
tests[i].outputs:=undeterminedOutput(Interfaces);
tests[i].actuallyActive:=0;
end;
if callback<>nil then callback(-1);
for i:=0 to length(tests)-1 do if (resume=nil) or resume^ then begin
tests[i].outputs:=behavior^.simulateSteps(tests[i].maxTotalSteps,tests[i].inputs,resume,stepsDone);
tests[i].actuallyActive:=stepsDone;
if initCounts then tests[i].maxTotalSteps:=stepsDone+4+stepsDone shr 3;
if callback<>nil then callback(i);
end;
if (resume<>nil) and not(resume^) and (callback<>nil) then callback(length(tests)-1);
end;
CONSTRUCTOR T_testCreator.createAsChallengeBasis;
begin
setLength(tests,0);
challengeTestCreationThread.create(@self);
behavior:=nil;
end;
CONSTRUCTOR T_testCreator.createForAnalysis(CONST visualBoard: P_visualBoard);
begin
createAsChallengeBasis;
behavior:=visualBoard^.extractBehavior;
Interfaces:=visualBoard^.getInterfaces;
end;
DESTRUCTOR T_testCreator.destroy;
VAR i: integer;
begin
challengeTestCreationThread.destroy;
for i:=0 to length(tests)-1 do begin
setLength(tests[i].inputs,0);
setLength(tests[i].outputs,0);
end;
setLength(tests,0);
if behavior<>nil then dispose(behavior,destroy);
setLength(Interfaces.inputs,0);
setLength(Interfaces.outputs,0);
end;
INITIALIZATION
tutorial.create;
FINALIZATION
tutorial.destroy;
end.