-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller.js
1219 lines (1097 loc) · 31.5 KB
/
controller.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 WHITE = 'white';
const BLACK = 'black';
const PROTECT = ''; // Falsy sentinel value to mark protected piece
const LMB = 0;
const RMB = 2;
const MMB = 1;
const STATE_STRINGS = {
alpha: '\u03b1',
beta: '\u03b2',
}
var hoveredPiece = undefined;
var heldPiece = undefined;
var armies = {
white: undefined,
black: undefined
};
var gameHistory = [];
var turnNumber = 0;
var turn = 'white';
// marks if there is a quantum move in progress and somehow indicates which
// part of the superstate still needs to be moved.
var requiredMove = undefined;
// ui elements
var dead, turnIndicator, hidden;
function getKing(color) {
return armies[color][0];
}
function startDrag(piece) {
let element = piece.element;
element.style.position = 'absolute';
element.style.left = event.x;
element.style.top = event.y;
element.style.transform = 'translate(-50%, -50%)';
element.style['z-index'] = 10;
heldPiece = piece;
}
function checkAlert(king, message) {
let kingSpace = document.getElementById(toAlgebraic(king.location));
let bb = kingSpace.getBoundingClientRect();
let alertContainer = document.getElementById('check-alert-container');
let alBB = alertContainer.getBoundingClientRect();
kingSpace.after(alertContainer);
alertContainer.style.display = 'block';
alertContainer.style.position = 'absolute';
alertContainer.style.top = bb.top;
alertContainer.style.left = ((bb.left + bb.right) / 2);
alertContainer.style.transform = 'translate(-50%, -50%)';
alertContainer.style['z-index'] = 10;
document.getElementById('check-alert').innerHTML = message;
}
function unhighlight(space) {
space.classList.remove('legal-move', 'threatened', 'path');
for (let piece of space.children) {
piece.classList.remove('threatened', 'protected');
}
}
function unhighlightBoard() {
for (let space of document.getElementsByClassName('board-space')) {
unhighlight(space);
}
}
function getSpace(location) {
return document.getElementById(toAlgebraic(location));
}
function getSpaceFromPoint(x, y) {
for (let space of document.getElementsByClassName('board-space')) {
let bounds = space.getBoundingClientRect();
if (x > bounds.left && x < bounds.right && y > bounds.top && y < bounds.bottom) {
return space;
}
}
return undefined;
}
function isInCheck(color) {
let king = getKing(color);
for (let piece of armies[otherColor(color)]) {
if (piece.alive) {
if (piece.isQuantum) {
let alphaCheck = piece.superstate.alpha.canCapture(king.location);
let betaCheck = piece.superstate.beta.canCapture(king.location);
if (alphaCheck && betaCheck) {
return true;
}
}
else if (piece.canCapture(king.location)) {
return true;
}
}
}
return false;
}
function canCheck(piece, kingLocation, ...hMoves) {
if (piece.isQuantum) {
let alphaCheck = piece.superstate.alpha.canCapture(kingLocation, ...hMoves);
let betaCheck = piece.superstate.beta.canCapture(kingLocation, ...hMoves);
return alphaCheck && betaCheck;
}
else return piece.canCapture(kingLocation, ...hMoves);
}
function wouldBeCheck(color, pieceToMove, destination) {
let king = getKing(color);
for (let piece of armies[otherColor(color)]) {
if (piece.alive) {
if (sameLocation(piece.location, destination) && pieceToMove.canCapture(destination)) {
continue; // There might still be another piece keeping you in check
}
else if (pieceToMove === king) {
if (canCheck(piece, destination, [pieceToMove, destination])) {
return true;
}
}
else {
if (canCheck(piece, king.location, [pieceToMove, destination])) {
return true;
}
}
}
}
return false;
}
function isCheckmate(color) {
let king = getKing(color);
if (isInCheck) {
for (let piece of armies[color]) {
if (piece.alive && piece.hasLegalMove()) {
return false;
}
}
return true;
}
else return false;
}
/// Collapsing on putting king on check
function checkCollapse(superstate, king) {
let alphaCheck = superstate.alpha.canCapture(king.location);
let betaCheck = superstate.beta.canCapture(king.location);
console.log(superstate.piece.color, king.color, king.location, superstate.alpha.location, superstate.beta.location, alphaCheck, betaCheck);
if (alphaCheck === betaCheck) return;
if (alphaCheck) {
superstate.beta.collapse();
}
else {
superstate.alpha.collapse();
}
}
function passTurn() {
let firstKing = getKing(turn);
let secondKing = getKing(otherColor(turn));
for (let piece of armies[turn]) {
if (piece.isQuantum) {
if (sameLocation(piece.superstate.alpha.location, piece.superstate.beta.location)) {
piece.collapse(undefined);
}
else {
console.log("Before switch collapsing");
checkCollapse(piece.superstate, secondKing);
}
}
}
turn = otherColor(turn);
turnIndicator.className = turn;
requiredMove = undefined;
for (let piece of armies[turn]) {
if (piece instanceof Pawn) {
piece.justDoubleMoved = false;
}
if (piece.isQuantum) {
console.log("After switch collapsing");
checkCollapse(piece.superstate, firstKing);
}
}
recordHistory();
if (isInCheck(turn)) {
if (isCheckmate(turn)) {
checkAlert(secondKing, "Checkmate!"); // TEMP
}
else {
checkAlert(secondKing, "Check!"); // TEMP
}
}
}
Piece = function() {
var nextId = function () {
let counts = {};
return function iterator(color, type) {
let slot = `${type}-${color}`;
if (counts[slot] === undefined) {
counts[slot] = 0;
return `${slot}-0`;
}
else {
counts[slot]++;
return `${slot}-${counts[slot]}`;
}
}
}();
var piecesByIds = {}
class Piece {
constructor(color, position, type) {
this.id = nextId(color, type);
piecesByIds[this.id] = this;
this.type = type;
this.color = color;
this.isQuantum = false;
this.canQuantum = true;
this.superstate = undefined;
this.location = toSpace(position);
this.element = undefined;
this.hasMoved = false;
this.alive = true;
this.promotedFrom = undefined;
}
static byId(id) {
return piecesByIds[id];
}
static at(location, ...hMoves) {
// hMoves are hypothetical moves that occur beforehand
let space = getSpace(location);
if (!space || space.children.length === 0) {
for (let [pieceToMove, destination] of hMoves) {
if (sameLocation(location, destination)) {
return pieceToMove;
}
}
return undefined;
}
else if (space.children.length === 1) {
let piece = Piece.byId(space.firstChild.id);
if (piece instanceof Piece) {
for (let [pieceToMove, destination] of hMoves) {
if (piece === pieceToMove) {
return undefined;
}
}
return piece;
}
}
// Drop-thru
let result = new Array(...space.children).map(function (child) {
let piece = Superstate.byId(child.id);
for (let [pieceToMove, destination] of hMoves) {
if (piece === pieceToMove) {
return undefined;
}
}
return piece;
}).filter(x => x !== undefined);
if (result.length) return result;
else return undefined;
}
isSolid(location, ...hMoves) {
/// Can a piece pass through this square?
let piecesAt = Piece.at(location, ...hMoves);
if (piecesAt === undefined) {
return false;
}
else if (piecesAt instanceof Piece) {
return true;
}
else if (piecesAt instanceof Array) {
if (piecesAt.length <= 1) return false;
if (piecesAt.length >= 4) return true;
// 1 or more quantum pieces... This is tricky.
// It also depends on the color of the piece and quantum pieces you pass through
return true;
}
}
initPiece() {
this.element = document.createElement('div');
this.element.id = this.id;
this.element.classList.add('piece', this.color, this.type);
let self = this;
let piece = this.element;
piece.addEventListener('mousedown', function(event) {
if (!self.alive || turn !== self.color) return;
if (event.button === LMB && (!requiredMove || requiredMove === self)) {
startDrag(self);
event.preventDefault();
}
if (event.button === RMB && this.canQuantum) {
heldPiece = undefined;
}
});
piece.addEventListener('mouseenter', function(event) {
hoveredPiece = self;
self.highlightLegalMoves();
});
piece.addEventListener('mouseleave', function(event) {
if (hoveredPiece === self) {
unhighlightBoard();
}
});
piece.addEventListener('contextmenu', function(event) {
if (self.alive && turn === self.color && self.canQuantum && !requiredMove) {
self.split();
event.preventDefault();
}
});
}
split() {
if (this.superstate == undefined) {
this.superstate = new Superstate(this);
}
else {
this.superstate.reset();
}
this.isQuantum = true;
this.render();
}
collapse(which) {
if (this.isQuantum) {
if (which === undefined) {
if (sameLocation(this.superstate.alpha.location, this.superstate.beta.location)) {
this.location = this.superstate.alpha.location;
}
else {
console.log("Attempt to collapse state to undefined");
return;
}
}
else {
this.location = this.superstate[which].location;
}
this.superstate.collapse(which);
this.superstate.hide();
this.isQuantum = false;
this.render();
}
}
isCapOrGuard(other) {
if (other instanceof Piece) {
return other.color !== this.color? true : PROTECT;
}
else if (other instanceof Array) {
return other.length <= 1;
// TEMP: quantum entanglement might be weird here...
// return false;
}
}
moveTo(location, switchTurn) {
if (switchTurn === undefined) {
switchTurn = true;
}
if (this.color === turn) {
let other = Piece.at(location);
if (other instanceof Piece) {
other.capture(this);
}
else if (other instanceof Array) {
for (let qPiece of other) {
qPiece.other.collapse();
}
}
let path = this.pathTo(location);
for (let space of path) {
let obstacle = Piece.at(space);
if (obstacle instanceof Array) {
for (let qPiece of obstacle) {
qPiece.other.collapse();
}
}
}
this.location = toSpace(location);
this.hasMoved = true;
this.render();
if (switchTurn) {
passTurn();
}
}
}
capture(captor) {
unhighlight(getSpace(this.location));
this.location = undefined;
this.element.classList.add('captured')
dead[this.color].append(this.element);
this.alive = false;
}
/// the spaces on the way to the given location.
pathTo(location) {
return straightLine(this.location, location);
}
canMove(location, ...hMoves) {
return false;
}
canCapture(location, ...hMoves) {
return this.canMove(location, ...hMoves);
}
isLegalMove(location) {
let [rank, file] = toSpace(location);
let [curRank, curFile] = this.location;
if (rank === curRank && file === curFile) {
return false;
}
let result = this.canMove(location);
if (result !== false && wouldBeCheck(this.color, this, location)) {
return false;
}
return result;
}
hasLegalMove() {
for (let rank = 0; rank < 8; rank++) {
for (let file = 0; file < 8; file++) {
if (this.isLegalMove([rank, file])) {
return true;
}
}
}
return false;
}
highlightLegalMoves() {
if (!this.location) return;
for (let space of document.getElementsByClassName('board-space')) {
let piece = Piece.at(space.id);
let legality = this.isLegalMove(space.id);
if (legality) {
if (piece instanceof Piece) {
space.classList.add('threatened');
piece.element.classList.add('threatened');
}
else {
space.classList.add('legal-move');
}
}
else if (legality === PROTECT && !(piece instanceof King)) {
if (piece) { // A bit defensive, but whatever
piece.element.classList.add('protected');
}
}
else {
unhighlight(space);
}
}
}
render() {
if (this.isQuantum) {
hidden.append(this.element);
this.superstate.render();
}
else {
let space = document.getElementById(toAlgebraic(this.location));
if (!this.element) {
this.initPiece();
}
if (this.alive) {
space.append(this.element);
}
else {
dead[this.color].append(this.element);
}
}
}
}
return Piece;
}();
function canRideToOrtho(piece, [rank, file], ...hMoves) {
let [curRank, curFile] = piece.location;
if (rank == curRank) {
let dir = file > curFile? 1 : -1;
let diff = Math.abs(file - curFile);
for (let offset = 1; offset < diff; offset++) {
let f = curFile + dir * offset;
if (piece.isSolid([rank, f], ...hMoves)) return false;
}
let other = Piece.at([rank, file], ...hMoves);
if (other) {
return piece.isCapOrGuard(other);
}
else return true;
}
else if (file == curFile) {
let dir = rank > curRank? 1 : -1;
let diff = Math.abs(rank - curRank);
for (let offset = 1; offset < diff; offset++) {
let r = curRank + dir * offset;
if (piece.isSolid([r, file], ...hMoves)) return false;
}
let other = Piece.at([rank, file], ...hMoves);
if (other) {
return piece.isCapOrGuard(other);
}
else return true;
}
else return false;
}
function canRideToDiag(piece, [rank, file], ...hMoves) {
let [curRank, curFile] = piece.location;
let rankDiff = rank - curRank;
let fileDiff = file - curFile;
if (Math.abs(rankDiff) === Math.abs(fileDiff)) {
let diff = Math.abs(rankDiff);
let fileDir = Math.sign(fileDiff);
let rankDir = Math.sign(rankDiff);
for (let offset = 1; offset < diff; offset++) {
let f = curFile + fileDir * offset;
let r = curRank + rankDir * offset;
if (piece.isSolid([r, f], ...hMoves)) return false;
}
let other = Piece.at([rank, file], ...hMoves);
if (other) {
return piece.isCapOrGuard(other);
}
else return true;
}
else return false;
}
class King extends Piece {
constructor(color, position) {
super(color, position, 'king');
this.canQuantum = false;
}
canMove(location) {
let [rank, file] = toSpace(location);
let [curRank, curFile] = this.location;
// Castling
if (!this.hasMoved && rank === curRank) {
if (file === curFile + 2 && !isInCheck(this.color) && !wouldBeCheck(this.color, this, [rank, curFile + 1])) {
let maybeRook = Piece.at([rank, 7]);
if (maybeRook instanceof Rook && !maybeRook.hasMoved) {
return !this.isSolid([rank, 5]) && !this.isSolid([rank, 6]);
}
}
else if (file === curFile - 2 && !isInCheck(this.color) && !wouldBeCheck(this.color, this, [rank, curFile - 1])) {
let maybeRook = Piece.at([rank, 0]);
if (maybeRook instanceof Rook && !maybeRook.hasMoved) {
return !this.isSolid([rank, 1]) && !this.isSolid([rank, 2]) && !this.isSolid([rank, 3]);
}
}
}
if (
rank <= curRank + 1 && rank >= curRank - 1
&& file <= curFile + 1 && file >= curFile - 1
) {
let other = Piece.at(location);
if (other) {
return this.isCapOrGuard(other);
}
else return true;
}
else return false;
}
moveTo(location, switchTurn) {
let [rank, file] = toSpace(location);
let [curRank, curFile] = this.location;
// Castling
if (file === curFile + 2) {
let maybeRook = Piece.at([rank, 7]);
if (maybeRook instanceof Rook) {
maybeRook.moveTo([rank, 5], false);
}
}
else if (file === curFile - 2) {
let maybeRook = Piece.at([rank, 0]);
if (maybeRook instanceof Rook) {
maybeRook.moveTo([rank, 3], false);
}
}
super.moveTo(location, switchTurn);
}
}
class Queen extends Piece {
constructor(color, position) {
super(color, position, 'queen');
}
canMove(location, ...hMoves) {
let [rank, file] = toSpace(location);
let [curRank, curFile] = this.location;
let ortho = canRideToOrtho(this, [rank, file], ...hMoves);
if (ortho !== false) return ortho;
return canRideToDiag(this, [rank, file], ...hMoves);
}
}
class Bishop extends Piece {
constructor(color, position) {
super(color, position, 'bishop');
}
canMove(location, ...hMoves) {
let [rank, file] = toSpace(location);
let [curRank, curFile] = this.location;
return canRideToDiag(this, [rank, file], ...hMoves);
}
}
class Knight extends Piece {
constructor(color, position) {
super(color, position, 'knight');
}
canMove(location) {
let [rank, file] = toSpace(location);
let [curRank, curFile] = this.location;
let rankDiff = Math.abs(rank - curRank);
let fileDiff = Math.abs(file - curFile);
if (rankDiff === 2 && fileDiff === 1 || rankDiff === 1 && fileDiff === 2) {
let other = Piece.at(location);
if (other) {
return this.isCapOrGuard(other);
}
else return true;
}
else return false;
}
}
class Rook extends Piece {
constructor(color, position) {
super(color, position, 'rook');
}
canMove(location, ...hMoves) {
let [rank, file] = toSpace(location);
let [curRank, curFile] = this.location;
return canRideToOrtho(this, [rank, file], ...hMoves);
}
}
class Pawn extends Piece {
constructor(color, position) {
super(color, position, 'pawn');
this.canQuantum = false;
this.justDoubleMoved = false;
}
canMove(location) {
let [rank, file] = toSpace(location);
let [curRank, curFile] = this.location;
let dir = this.color === 'white'? 1 : -1;
if (file === curFile) {
if (!this.hasMoved && rank === curRank + 2 * dir) {
return !this.isSolid(location) && !this.isSolid([curRank + dir, file]);
}
return rank === curRank + dir && !this.isSolid(location);
}
else if (Math.abs(file - curFile) === 1) {
if (rank === curRank + dir) {
let other = Piece.at(location);
if (other instanceof Piece) {
return this.isCapOrGuard(other);
}
if (other instanceof Array) {
for (let qState of other) {
if (qState.color !== this.color) {
return true;
}
}
}
else if (other == undefined) { // Check for en passant capture
let maybePawn = Piece.at([curRank, file]);
return maybePawn instanceof Pawn && maybePawn.justDoubleMoved;
}
}
}
else return false;
}
canCapture(location) {
let [rank, file] = toSpace(location);
let [curRank, curFile] = this.location;
let dir = this.color === 'white'? 1 : -1;
return Math.abs(file - curFile) === 1 && rank === curRank + dir;
}
moveTo(location, switchTurn) {
let [rank, file] = toSpace(location);
let [curRank, curFile] = this.location;
if (Math.abs(rank - curRank) == 2) {
this.justDoubleMoved = true;
}
else if (file !== curFile) {
// collapse-capture
let other = Piece.at(location)
if (other instanceof Array) {
let qState = other.find(qs => qs.color !== this.color);
if (qState) {
qState.collapse();
}
}
else {
// en passant
let maybePawn = Piece.at([curRank, file]);
if (maybePawn instanceof Pawn && maybePawn.justDoubleMoved) {
maybePawn.capture();
}
}
}
super.moveTo(location, switchTurn);
let dir = this.color === 'white'? 1 : -1;
let lastRank = this.color === 'white'? 7 : 0;
if (rank === lastRank) {
this.promote(location);
}
}
promote(location) {
// TODO: allow player to choose promotion piece
let promotedPiece = new Queen(this.color, location);
let index = armies[this.color].indexOf(this);
armies[this.color][index] = promotedPiece;
promotedPiece.hasMoved = true;
promotedPiece.promotedFrom = this;
promotedPiece.render();
this.alive = false;
hidden.append(this.element);
}
}
const IMPOSSIBLE = 0;
const NORMAL = 1;
const LEFTCAPTURED = 2; // "left" term in entanglement captured by right
const RIGHTCAPTURED = 3;
const ALPHA = 0;
const BETA = 1;
const STATE_INDEX = {
alpha: ALPHA,
beta: BETA
}
Superstate = function() {
let ids = {};
class QuantumState {
constructor(superstate, location, state) {
this.id = `${superstate.piece.id}-${state}`;
ids[this.id] = this;
this.parent = superstate;
this.location = location;
this.state = state;
this.element = undefined;
this.other = undefined;
this.alive = true;
}
get color() {
return this.parent.piece.color;
}
get piece() {
return this.parent.piece;
}
isLegalMove(location) {
this.piece.location = this.location;
return (
this.piece.isLegalMove(location)
&& !(Piece.at(location) instanceof Piece) // Quantum Pieces cannot capture classical pieces
);
}
canCapture(location, ...hMoves) {
this.piece.location = this.location;
return this.piece.canCapture(location, ...hMoves);
}
moveTo(location) {
if (requiredMove & requiredMove !== this) {
return;
}
this.location = toSpace(location);
this.render();
// Force quantum moves to happen in pairs.
if (requiredMove == undefined) {
requiredMove = this.other;
}
if (requiredMove === this) {
passTurn();
}
// TODO: entanglement
}
highlightLegalMoves(location) {
this.parent.piece.location = this.location;
return this.parent.piece.highlightLegalMoves(location);
}
capture(captor) {
// TODO: this might actually just entangle stuff...
unhighlight(getSpace(this.location));
this.location = undefined;
this.element.classList.add('captured');
dead[this.color].append(this.element);
this.alive = false;
}
render() {
let space = document.getElementById(toAlgebraic(this.location));
space.append(this.element);
}
collapse() {
this.piece.collapse(this.state);
}
}
class Entanglement {
constructor(first, second) {
this.first = first;
this.second = second;
}
}
class Force extends Entanglement {
collapse() {
this.second.collapse()
}
};
class Capture extends Entanglement {
collapse() {
this.second.collapse()
this.second.parent.piece.capture(this.first.parent.piece)
}
}
class Superstate {
constructor(piece) {
this.piece = piece;
// locations
this.states = [
new QuantumState(this, piece.location, 'alpha'),
new QuantumState(this, piece.location, 'beta')
];
this.alpha.other = this.beta;
this.beta.other = this.alpha;
this.entanglements = [];
}
reset() {
for (let stateObj of this.states) {
stateObj.location = this.piece.location;
stateObj.alive = this.piece.alive;
}
}
get alpha() {
return this.states[0];
}
get beta() {
return this.states[1];
}
static get Entanglement() {
return Entanglement;
}
static get Force() {
return Force;
}
static get Capture() {
return Capture;
}
static get QuantumState() {
return QuantumState;
}
static byId(id) {
return ids[id];
}
initPieces() {
let self = this;
let piece = this.piece;
for (let stateObj of this.states) {
let state = stateObj.state;
let element = document.createElement('div');
element.id = `${piece.id}-${state}`;
element.classList.add('piece', piece.color, piece.type, 'quantum');
let label = document.createElement('div');
let intId = parseInt(piece.id.split('-').pop());
label.classList.add('quantum', 'label', state, 'pair-' + intId);
label.innerHTML = `${intId+1}${STATE_STRINGS[state]}`
element.appendChild(label);
stateObj.element = element;
element.addEventListener('mousedown', function(event) {
if (!stateObj.alive || turn !== stateObj.color) return;
if (event.button === LMB && (!requiredMove || requiredMove === stateObj)) {
startDrag(stateObj);
event.preventDefault();
}
});
element.addEventListener('mouseenter', function(event) {
hoveredPiece = self;
stateObj.highlightLegalMoves();
});
element.addEventListener('mouseleave', function(event) {
if (hoveredPiece === self) {
unhighlightBoard();
}
});
element.addEventListener('contextmenu', function(event) {
if (!requiredMove && turn === piece.color) {
stateObj.collapse();
requiredMove = piece;
event.preventDefault();
}
})
}
}
collapse(which) {
if (which == undefined) {
return;
}
for (let qState of Piece.at(this[which].location)) {
if (qState.piece !== this.piece && qState.piece.isQuantum) {
qState.other.collapse();
}
}
}
hide() {
for (let stateObj of this.states) {
hidden.append(stateObj.element);
}
}
render() {
if (!this.elements) {
this.initPieces();
}
for (let state of this.states) {
state.render();
}
}
}
return Superstate;
}();
function renderBoard() {