-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.ts
1139 lines (1030 loc) · 30.8 KB
/
logic.ts
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
/* Tool to analyse chess positions */
//[of]:HASH
/** Memory to save things and later access them again */
let HASH = new class {
private entries:{ [ key:number ]:Piece } = { };
private id:uid = 0;
public add( thing:Piece ):uid {
this.entries[++this.id] = thing;
return this.id;
}
public get( id:uid ): Piece {
return this.entries[id];
}
};
//[cf]
//[c]( MVC
//[of]:State
/** The Application state. */
class State {
public observers:Set<Observer> = new Set();
public position:Chessposition;
//[of]:constructor()
constructor(){
this.position = positionFromFen(STARTFEN);
}
//[cf]
//[of]:start()
public start() {
this.notifyObservers();
}
//[cf]
//[of]:reset()
public reset() {
this.position = positionFromFen(STARTFEN);
this.notifyObservers();
}
//[cf]
//[of]:makemove()
public makemove(from: index, to: index, promopiece:promopiece) {
if (from === to) {
return this.notifyObservers();
}
let position = this.position;
let board = position.board;
let castling = position.castling;
let whitetomove = position.whitetomove;
let fromid = board[from];
let toid = board[to];
let frompiece = HASH.get(fromid);
let topiece = HASH.get(toid);
let iswhite = frompiece.iswhite;
let enpassant = position.enpassant;
let newenpassant = null;
let movemade = false;
let waslegalmove = false;
//[of]:white castles short
if (!movemade &&
castling.whiteshort &&
(
(
from == 60 && (to == 62 || to == 63)
) ||
(from == 63 && to == 60)
) &&
!(61 in board) && !(62 in board)
) {
this.movepiece(60, 62);
this.movepiece(63, 61);
castling.whiteshort = false;
castling.whitelong = false;
movemade = true;
if (whitetomove) {
waslegalmove = true;
}
}
//[cf]
//[of]:white castles long
if (!movemade &&
castling.whitelong &&
(
(
from == 60 && (to > 55 && to < 59)
) ||
(from == 56 && to == 60)
) &&
!(57 in board) && !(58 in board) && !(59 in board)
) {
this.movepiece(60, 58);
this.movepiece(56, 59);
castling.whiteshort = false;
castling.whitelong = false;
movemade = true;
if (whitetomove) {
waslegalmove = true;
}
}
//[cf]
//[of]:black castles short
if (!movemade &&
castling.blackshort &&
(
(
from == 4 && (to == 6 || to == 7)
) ||
(from == 7 && to == 4)
) &&
!(5 in board) && !(6 in board)
) {
this.movepiece(4, 6);
this.movepiece(7, 5);
castling.blackshort = false;
castling.blacklong = false;
movemade = true;
if (!whitetomove) {
waslegalmove = true;
}
}
//[cf]
//[of]:black castles long
if (!movemade &&
castling.blacklong &&
(
(
from == 4 && (to > -1 && to < 3)
) ||
(from == 0 && to == 4)
) &&
!(1 in board) && !(2 in board) && !(3 in board)
) {
this.movepiece(4, 2);
this.movepiece(0, 3);
castling.blackshort = false;
castling.blacklong = false;
movemade = true;
if (!whitetomove) {
waslegalmove = true;
}
}
//[cf]
//[of]:en passant
if (
!movemade && enpassant && !toid && frompiece.type=='p' && to==enpassant &&
(
(
iswhite && whitetomove &&
(from==enpassant+7 || from==enpassant+9) &&
HASH.get(board[enpassant+8]).type=='p' &&
!HASH.get(board[enpassant+8]).iswhite
) ||
(
!frompiece.iswhite && !whitetomove &&
(from==enpassant-7 || from==enpassant-9) &&
HASH.get(board[enpassant-8]).type=='p' &&
HASH.get(board[enpassant-8]).iswhite
)
)
) {
this.movepiece(from, to);
if (whitetomove) {
delete board[enpassant+8]
} else {
delete board[enpassant-8]
}
movemade = true;
waslegalmove = true;
}
//[cf]
//[of]:white pawn moves two squares from start position
if (
!movemade && !toid && frompiece.type=='p' && frompiece.iswhite &&
from > 47 && from < 56 && to==from-16
) {
this.movepiece(from, to);
movemade = true;
newenpassant = from-8;
if (whitetomove) {
waslegalmove = true;
}
}
//[cf]
//[of]:black pawn moves two squares from start position
if (
!movemade && !toid && frompiece.type=='p' && !frompiece.iswhite &&
from > 7 && from < 16 && to==from+16
) {
this.movepiece(from, to);
movemade = true;
newenpassant = from+8;
if (!whitetomove) {
waslegalmove = true;
}
}
//[cf]
//[of]:pawn promotion
if (!movemade && promopiece) {
if (!(to in board)) {
board[to] = makepiece(promopiece as fenpiece, to);
delete board[from];
waslegalmove = true;
}
movemade = true;
}
//[cf]
//[of]:normal move
if (!movemade) {
if (!(toid && frompiece.iswhite === topiece.iswhite)) {
this.movepiece(from, to);
movemade = true;
waslegalmove = true; // todo
}
}
//[cf]
if (waslegalmove) {
position.whitetomove = !whitetomove;
position.enpassant = newenpassant;
} else if (newenpassant) {
position.enpassant = newenpassant;
}
this.notifyObservers();
}
//[cf]
//[of]:movepiece()
private movepiece( from:index, to:index) {
let board = this.position.board;
let fromid = board[from];
HASH.get(fromid).index = to;
board[to] = fromid;
delete board[from];
}
//[cf]
//[of]:notifyObservers()
/** called internally when changes to the State were made */
private notifyObservers() {
for (let observer of this.observers) {
observer.update();
}
}
//[cf]
}
//[cf]
//[of]:Observer
/** An element on the page, listening to changes in the State */
abstract class Observer {
protected readonly state: State;
//[of]:constructor()
constructor(state: State) {
this.state = state;
state.observers.add(this);
}
//[cf]
//[of]:update()
/** Called by the Application State when it has made changes to its state. */
abstract update():void;
//[cf]
}
//[cf]
//[of]:Board
class Board extends Observer {
private pieces:{ [ id:number ]:pieceelem } = { };
private elem:boardelem;
private grabbedelem:pieceelem|null = null;
private promochooserwhite:elem;
private promochooserblack:elem;
private position!: Pos;
private size!: pixels;
private piecesize!: pixels;
private halfpiecesize!: pixels;
//[of]:constructor()
constructor ( boardid:elemid, state:State ) {
super(state);
let elem = this.elem = $(boardid);
this.updateDimensions();
this.promochooserwhite = $('promo-chooser-white', elem);
this.promochooserblack = $('promo-chooser-black', elem);
//[c] Todo: Implement 'drop off piece' functionality
let self = this;
//[of]:board mouse down handler
elem.on('mousedown', 'chess-piece', function( event:mousedownevent ) {
self.grab($(this), event);
return handled(event);
});
//[cf]
//[of]:board mouse up handler
elem.on('mouseup', function( event:mouseupevent ) {
if (self.grabbedelem) {
self.release(event);
}
return handled(event);
});
//[cf]
//[of]:body mouse move handler
$('body').on('mousemove', function( event:mousemoveevent ) {
if (self.grabbedelem) {
self.drag(event);
}
return handled(event);
});
//[cf]
//[of]:body mouse leave handler
$('body').on('mouseleave', function() {
if (self.grabbedelem) {
self.grabbedelem.hide();
}
});
//[cf]
//[of]:body mouse enter handler
$('body').on('mouseenter', function() {
if (self.grabbedelem) {
self.grabbedelem.show();
}
});
//[cf]
//[of]:window mouse up handler
$(window).on('mouseup', function( event:mouseupevent ) {
if (self.grabbedelem) {
self.grabbedelem.show();
}
self.reset();
return handled(event);
});
//[cf]
//[of]:window rezise handler
$(window).on('resize', function() {
self.updateDimensions();
});
//[cf]
}
//[cf]
//[of]:update()
public update() {
let board = this.state.position.board;
let pieces = this.pieces;
//[of]:create new pieces, append them to the board
let f = null;
for (const id of values(board)) {
if (!(id in pieces)) {
let elem = this.makeelem(id);
if (!f) { f = document.createDocumentFragment(); }
elem.appendTo(f);
}
}
if (f) {
this.elem.append(f);
}
//[cf]
//[of]:place the pieces
for (const id of values(board)) {
let elem = pieces[id];
this.place(elem);
}
//[cf]
//[of]:drop non existing pieces off the board
for (const elem of values(pieces)) {
let index = getindex(elem);
if (!(index in board) || getid(elem)!==board[index]) {
this.dropoff(elem);
}
}
//[cf]
}
//[cf]
//[of]:updateDimensions()
/** read out the board size and piece size on document load and -resize */
private updateDimensions() {
{
let offset = this.elem.offset();
let x = offset ? offset.left : 0;
let y = offset ? offset.top : 0;
this.position = {x:x, y:y};
}
let size = this.elem.width();
if (!size) {
throw new Error('could not detect board size');
} else {
size = this.size = Math.round(size);
let piecesize = this.piecesize = Math.round(size / 8);
this.halfpiecesize = Math.round(piecesize / 2);
}
}
//[cf]
//[of]:grab(pieceelem, mousedownevent)
/** grab a piece on the board */
private grab( elem:pieceelem, event:mousedownevent ) {
this.grabbedelem = elem;
// So that the element later gets adjusted in the `place` function.
elem.data('index', -2);
let mousepos = this.mousepos(event);
this.aligncursor(elem, mousepos);
this.adjustpromochooser(elem, mousepos);
}
//[cf]
//[of]:drag(mousemoveevent)
/** drag a grabbed piece */
private drag( event:mousemoveevent ) {
let elem = this.grabbedelem;
if (elem) {
let mousepos = this.mousepos(event);
this.aligncursor(elem, mousepos);
this.adjustpromochooser(elem, mousepos);
}
}
//[cf]
//[of]:release(mouseupevent)
/** release a grabbed piece */
private release( event:mouseupevent ) {
let elem = this.grabbedelem;
this.grabbedelem = null;
if (elem) {
this.hidepromo();
elem.css({ zIndex: 1 });
let mouse = this.mousepos(event);
if (mouse.inboard) {
let [from, to, promopiece] = this.getmove(elem, mouse);
//[c] ~ setTimeout(()=>{
this.state.makemove( from, to, promopiece );
//[c] ~ },0);
} else {
this.update();
}
}
}
//[of]:getmove(elem, mousepos)
private getmove( elem:pieceelem, mousepos:Mousepos ):[index, index, promopiece] {
let piece = getpiece(elem);
let from = piece.index;
let to = this.getindex(mousepos);
let promopiece:promopiece = null;
{
let iswhite = piece.iswhite;
if (
piece.type=='p' && (
(iswhite && to<8 ) ||
(!iswhite && to>55)
)
) {
promopiece = this.getpromopiece(mousepos);
}
}
return [from, to, promopiece];
}
//[of]:getpromopiece(Mousepos)
/** read the desired promopiece from where the mouse was released */
private getpromopiece( mouse:Mousepos ):promopiece {
let x = mouse.x;
let y = mouse.y;
let row = this.getrow(mouse);
let col = this.getcol(mouse);
let piecesize = this.piecesize;
let halfpiecesize = this.halfpiecesize;
let promocolsep = col * piecesize + halfpiecesize;
let promorowsep = row * piecesize + halfpiecesize;
let promopiece = (x < promocolsep)
? (y < promorowsep)
? 'q'
: 'b'
: (y < promorowsep)
? 'r'
: 'n'
;
if (row === 0) {
promopiece = promopiece.toUpperCase();
}
return promopiece as promopiece;
}
//[cf]
//[cf]
//[cf]
//[of]:adjustpromochooser(pieceelem, Mousepos)
private adjustpromochooser(elem:pieceelem, mouse:Mousepos) {
let piece = getpiece(elem);
if (piece.type === 'p') {
if (mouse.inboard) {
let to = this.getindex(mouse);
let iswhite = piece.iswhite;
let board = this.state.position.board;
if ( iswhite ) {
if ( to<8 && !(to in board) ) {
this.showpromo(this.promochooserwhite, to);
} else if ( to>7 && to<16 && !(to-8 in board) ) {
this.showpromo(this.promochooserwhite, to-8);
} else if (to>15) {
this.hidepromo();
}
} else {
if ( to>55 && !(to in board) ) {
this.showpromo(this.promochooserblack, to);
} else if ( to>47 && to<56 && !(to+8 in board) ) {
this.showpromo(this.promochooserblack, to+8);
} else if (to<48) {
this.hidepromo();
}
}
} else {
this.hidepromo();
}
}
}
//[cf]
//[of]:showpromo(elem, index)
/** Show the promotion indicator */
private showpromo(chooser:elem, index:index) {
let [x,y] = indexToCoords(index);
chooser.css({
top:coordToPercent(y),
left:coordToPercent(x)
});
chooser.show();
}
//[cf]
//[of]:hidepromo()
private hidepromo() {
this.promochooserblack.hide();
this.promochooserwhite.hide();
}
//[cf]
//[of]:reset()
/** a mouse up in weird locations calls this */
private reset() {
let elem = this.grabbedelem;
if (elem) {
elem.css({ zIndex: 1 });
this.grabbedelem = null;
this.promochooserblack.hide();
this.promochooserwhite.hide();
this.update();
}
}
//[cf]
//[of]:place(pieceelem, index?)
/** place a piece dom element on the board according to the index of its
related Piece read from the hash. */
private place( elem:pieceelem ) {
let to = getpiece(elem).index;
let from = getindex(elem);
if (from !== to) {
let [x,y] = indexToCoords(to);
if (from === -1) { // -1 means, the element was newly created.
elem.css({
top: coordToPercent(y),
left: coordToPercent(x)
});
} else {
elem.animate({
top: coordToPercent(y),
left: coordToPercent(x)
}, 100);
}
elem.data('index', to);
}
}
//[cf]
//[of]:dropoff(pieceelem)
/** remove a piece from the board (the piece actually just gets hidden) */
private dropoff( elem:pieceelem ) {
elem.hide();
}
//[cf]
//[of]:aligncursor(pieceelem, Mousepos)
/** align a piece at the cursor */
private aligncursor( elem:pieceelem, mousepos:Mousepos ) {
let halfpiecesize = this.halfpiecesize;
elem.css({
zIndex: 1000,
top: mousepos.y - halfpiecesize,
left: mousepos.x - halfpiecesize,
});
}
//[cf]
//[of]:mousepos(dragdropevent)
/** return the mouse position relative to the board. Returns null when
strict === true and the mouse is not inside the board */
private mousepos( event:dragdropevent ):Mousepos {
let position = this.position;
let x = event.pageX - position.x;
let y = event.pageY - position.y;
let boardsize = this.size;
let inboard = x>0 && y>0 && x<boardsize && y<boardsize;
return {
x: x,
y: y,
inboard: inboard,
};
}
//[cf]
//[of]:getindex(Mousepos)
/** get the board index correlating to this mouse position */
private getindex(mouse:Mousepos):index {
return 8 * this.getrow(mouse) + this.getcol(mouse);
}
//[cf]
//[of]:getrow(Mousepos)
/** get the row index correlating to this mouse position */
private getrow( mouse:Mousepos ):row {
return Math.floor(mouse.y / this.piecesize);
}
//[cf]
//[of]:getcol(Mousepos)
/** get the column index correlating to this mouse position */
private getcol( mouse:Mousepos ):col {
return Math.floor(mouse.x / this.piecesize);
}
//[cf]
//[of]:makeelem(uid)
/** create the dom element for a piece and add it to <Board>.pieces. Return the
element (It wont get appended to the boards dom element). */
private makeelem( id:uid ):pieceelem {
let piece = HASH.get(id);
let piecetype = piece.iswhite ? piece.type.toUpperCase() : piece.type;
let elem = $(`<chess-piece class="${piecetype}" />`);
elem.data({ id: id, index: -1 });
this.pieces[id] = elem;
return elem;
}
//[cf]
}
//[c]( Tools
//[of]:getindex(pieceelem)
function getindex( elem:pieceelem ):index {
return parseInt(elem.data('index'));
}
//[cf]
//[of]:getid(pieceelem)
function getid( elem:pieceelem ):uid {
return parseInt(elem.data('id'));
}
//[cf]
//[of]:getpiece(pieceelem)
function getpiece( elem:pieceelem ):Piece {
return HASH.get( getid(elem) );
}
//[cf]
//[c])
//[cf]
//[c])
//[c]( Constants
//[of]:STARTFEN
const STARTFEN : fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
//[cf]
//[of]:COLTRANSTABLE
const COLTRANSTABLE:{ [ key:string ]:int } = {
'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7,
};
//[cf]
//[of]:ROWTRANSTABLE
const ROWTRANSTABLE:{ [ key:string ]:int } = {
'8': 0, '7': 1, '6': 2, '5': 3, '4': 4, '3': 5, '2': 6, '1': 7,
};
//[cf]
//[of]:REVCOLTRANSTABLE
const REVCOLTRANSTABLE:string = 'abcdefgh';
//[cf]
//[of]:REVROWTRANSTABLE
const REVROWTRANSTABLE:string = '87654321';
//[cf]
//[c])
//[c]( Interfaces
//[of]:Chessposition
/** The abstract chess position representation, saved in <State>.position */
interface Chessposition {
board:chessboard,
whitetomove:boolean,
castling:{
whiteshort:boolean,
whitelong:boolean,
blackshort:boolean,
blacklong:boolean
},
enpassant:index|null
}
//[of]:positionFromFen()
function positionFromFen( fen:fen ):Chessposition {
let [
fenpieces,
fentomove,
fencastling,
fenenpassant
] = fen.trim().split(/\s+/);
let board:chessboard = { };
let curindex = 0;
for (const char of fenpieces) {
let empty = parseInt(char);
if (empty) {
curindex += empty;
} else if (char === '/') {
continue;
} else {
board[curindex] = makepiece(char as fenpiece, curindex);
curindex++;
}
}
let whitetomove = fentomove.toLowerCase() === 'w';
let castling = {
whiteshort: fencastling.includes('K'),
whitelong: fencastling.includes('Q'),
blackshort: fencastling.includes('k'),
blacklong: fencastling.includes('q')
};
let enpassant = null;
if (fenenpassant !== '-') {
enpassant = coordsToIndex(
COLTRANSTABLE[fenenpassant[0]],
ROWTRANSTABLE[fenenpassant[1]],
);
}
return {
board: board,
whitetomove: whitetomove,
castling:castling,
enpassant:enpassant
};
}
//[cf]
//[of]:ischessposition()
function ischessposition(object:any): object is Chessposition {
if ('board' in object) {
return true;
}
return false;
}
//[cf]
//[cf]
//[of]:Piece
/** the abstract piece representation. This is what `HASH.get(<uid>)` returns. */
interface Piece {
type: piecetype,
id: uid,
index: index,
iswhite: boolean
}
//[of]:makepiece()
function makepiece( name:fenpiece, index:index ) : uid {
let piecetype = name.toLowerCase();
if (!"kqrbnp".includes(piecetype)) {
throw new Error("Illegal piecetype");
}
let iswhite = piecetype !== name;
let piece:Piece = {
type: piecetype as piecetype,
id: -1,
index: index,
iswhite: iswhite,
};
let id = piece.id = HASH.add(piece);
return id;
}
//[cf]
//[of]:ispiece()
function ispiece(object:any): object is Piece {
if (
'type' in object && 'id' in object &&
'index' in object && 'iswhite' in object
) {
return true;
}
return false;
}
//[cf]
//[cf]
//[of]:Pos
interface Pos { x:pixels, y:pixels };
interface Mousepos { x:pixels, y:pixels, inboard:boolean };
//[of]:function ispos()
function ispos(object:any): object is Pos|Mousepos {
if ('x' in object && 'y' in object) {
return true;
}
return false;
}
//[cf]
//[cf]
//[c])
//[c]( Iterators
type key = any;
type value = any;
//[of]:items()
/** Iterate over the keys and values of an 'iterable' (in the Python sense) */
function* items(
iterable: Map<key, value> | Iterable<value> | Object
): IterableIterator<[key, value]> {
if (iterable instanceof Map) {
for (const entry of iterable.entries()) {
yield entry;
}
}
else if (
// sorted by usage frequency
iterable instanceof Array
|| typeof iterable === 'string'
|| iterable instanceof Set
|| iterable instanceof String
) {
let index = -1;
for (const value of iterable) {
yield [++index, value];
}
}
else if (iterable instanceof Object) {
for (const entry of Object.entries(iterable)) {
yield entry;
}
}
else {
throw new Error(`Can not be used with '${typeof iterable}' type`);
}
}
//[cf]
//[of]:values()
/** Iterate over the values of an 'iterable' (in the Python sense) */
function* values(
iterable: Map<key, value> | Iterable<value> | Object
): IterableIterator<value> {
if (iterable instanceof Map) {
for (const value of iterable.values()) {
yield value;
}
}
else if (
// sorted by usage frequency
iterable instanceof Array
|| typeof iterable === 'string'
|| iterable instanceof Set
|| iterable instanceof String
) {
for (const value of iterable) {
yield value;
}
}
else if (iterable instanceof Object) {
for (const value of Object.values(iterable)) {
yield value;
}
}
else {
throw new Error(`Can not be used with '${typeof iterable}' type`);
}
}
//[cf]
//[c])
//[c]( Misc Tools
//[of]:indexToCoords()
function indexToCoords( index:index ):[ col, row ] {
let x = index % 8;
return [ x, (index-x)/8 ];
}
//[cf]
//[of]:coordsToIndex()
function coordsToIndex( x:col, y:row ):index {
return y * 8 + x;
}
//[cf]
//[of]:coordToPercent()
function coordToPercent( coord:col|row ):string {
return coord * 12.5 + '%';
}
//[cf]
//[of]:clone()
/** https://jsperf.com/cloning-an-object/79 */
function clone( object:any ):any {
return JSON.parse(
JSON.stringify(object)
);
}
//[cf]
//[of]:handled()
function handled( event:dragdropevent ):boolean {
event.preventDefault();
event.stopPropagation();
return false;
}
//[cf]
//[of]:squarename()
/** Convert a index to the representation of the square name in algebraic
chess notation. Eg `0` becomes `'a8'`, `63` becomes `'h1'`. */
function squarename( index:index|null ):string|null {
if(index === null) {
return null;
}
let [x,y] = indexToCoords(index);
return REVCOLTRANSTABLE[x] + REVROWTRANSTABLE[y];
}
//[cf]
//[c])
//[c]( Types
//[of]:int
/** 'it shall be an integer, not a float' */
type int = number;
//[cf]
//[of]:pixels
/** 'these are pixels' */
type pixels = int;
//[cf]
//[of]:fen
/** A chess position encoded in Forsyth-Edwards notation. E.g.
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1' for the starting
position in chess games. */
type fen = string;
//[cf]
//[of]:fenpiece
/** Short piece names used in FENs. Big letters are white pieces */
type fenpiece = 'K'|'Q'|'R'|'B'|'N'|'P'|'k'|'q'|'r'|'b'|'n'|'p';
//[cf]
//[of]:promopiece
/** the possible target pieces when promoting a pawn, in algebraic notation */
type promopiece = 'Q'|'R'|'B'|'N'|'q'|'r'|'b'|'n'|null;
//[cf]
//[of]:piecetype
/** Short names for king, queen, rook, bishop, knight, pawn. */
type piecetype = 'k'|'q'|'r'|'b'|'n'|'p';