This repository has been archived by the owner on Jul 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pce.js
1183 lines (1164 loc) · 40 KB
/
pce.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
/*
> PCE-JS - Web Implementation of PlayeChessEngine (PCE)
> Code by SinisterIcy
> Version ALPHA
> Licensed under the GNU General Public License v3.0
> Github: https://github.com/playeChess/PCE-JS
*/
/**
* Namespace for PlayeChessEngine
*/
var PlayeChessEngine = {
Move: class Move {},
/**
* Namespace of PlayeChessEngine for board
*/
board: {
/**
* Namespace of PlayeChessEngine for pieces
*/
pieces: {
piece_type: {},
Piece: class Piece {},
Pawn: class Pawn {},
Rook: class Rook {},
Knight: class Knight {},
Bishop: class Bishop {},
Queen: class Queen {},
King: class King {}
},
Board: class Board {}
},
status: {},
draw: {},
win: {},
PCE: class PCE {}
}
/**
* A move (with start and end coordinates)
*/
PlayeChessEngine.Move = class Move {
StartCoords
/**
* Start coords of the move
* @type {[number, number]}
*/
get start_coords() {
return this.StartCoords
}
/**
* End coords of the move
* @type {[number, number]}
*/
EndCoords
get end_coords() {
return this.EndCoords
}
IsCapture = false
/**
* If the move is a capture
* @type {boolean}
*/
set is_capture(is_capture) {
this.IsCapture = is_capture
}
get is_capture() {
return this.IsCapture
}
IsValid = false
/**
* If the move is valid
* @type {boolean}
*/
set is_valid(is_valid) {
this.IsValid = is_valid
}
get is_valid() {
return this.IsValid
}
constructor(start_square_x, start_square_y, end_square_x, end_square_y) {
this.StartCoords = [start_square_x, start_square_y]
this.EndCoords = [end_square_x, end_square_y]
}
/**
* Get a string who contains details about the move
* @returns {string} The details string
*/
show() {
let files = "abcdefgh"
return files[this.start_coords[0]] + (this.start_coords[1] + 1) + " -> " + files[this.end_coords[0]] + (this.end_coords[1] + 1)
}
/**
* Checks if the move is included in an array of moves
* @param {PlayeChessEngine.Move[]} moves The array of moves
* @returns {bool} Whether or not the move is included
*/
included_in(moves) {
let included = false
moves.forEach(mv => {
mv.is_valid = false
if(JSON.stringify(mv) == JSON.stringify(this)) {
included = true
}
})
return included
}
}
PlayeChessEngine.board.pieces.piece_type = {
no: -1,
p: 0,
r: 1,
n: 2,
b: 3,
q: 4,
k: 5
}
/**
* A piece (with type, coords and color)
*/
PlayeChessEngine.board.pieces.Piece = class Piece {
Type
/**
* The type of the piece (-1,no -> no piece, 0,p -> pawn, 1,r -> rook, 2,n -> knight, 3,b -> bishop, 4,q -> queen, 5,k -> king)
* @type {PlayeChessEngine.board.pieces.piece_type}
*/
get type() {
return this.Type
}
set type(type) {
this.Type = type
}
Coords
/**
* The coordinates of the piece
* @type {[number, number]}
*/
get coords() {
return this.Coords
}
set coords(coords) {
this.Coords = coords
}
IsWhite
/**
* Whether or not the piece is white
* @type {boolean}
*/
set is_white(is_white) {
this.IsWhite = is_white
}
get is_white() {
return this.IsWhite
}
HasMoved
/**
* Whether or not the piece has moved yet
* @type {boolean}
*/
set has_moved(has_moved) {
this.HasMoved = has_moved
}
get has_moved() {
return this.HasMoved
}
constructor(type, is_white, x, y) {
this.type = type
this.is_white = is_white
this.coords = [x, y]
this.has_moved = false
}
/**
* Validates the basic rules of the move (can not go where there is one of your pieces)
* @param {PlayeChessEngine.board.pieces.Piece[][]} board The board to check on
* @param {number} x_final The final x coordinate
* @param {number} y_final The final y coordinate
* @returns {boolean} Whether or not the move is legal
*/
validate_validation(board, x_final, y_final) {
if (board[x_final][y_final].type == PlayeChessEngine.board.pieces.piece_type.no) {
return true
}
if (board[x_final][y_final].is_white != this.is_white) {
return true
}
return false
}
/**
* Checks if the path from a cell to another is clear
* @param {PlayeChessEngine.board.pieces.Piece[][]} board The board to check on
* @param {number} x_final The final x coordinate
* @param {number} y_final The final y coordinate
* @returns {boolean} Whether or not the move is legal
*/
check_path(board, x_final, y_final) {
let x_diff = x_final - this.coords[0]
let y_diff = y_final - this.coords[1]
if (x_diff == 0) {
if (y_diff > 0) {
for (let i = 1; i < y_diff; i++) {
if (board[this.coords[0]][this.coords[1] + i].type != PlayeChessEngine.board.pieces.piece_type.no) {
return false
}
}
}
else {
for (let i = 0; i < Math.abs(y_diff); i++) {
if (board[this.coords[0]][this.coords[1] - i].type != PlayeChessEngine.board.pieces.piece_type.no) {
return false
}
}
}
}
else if (y_diff == 0) {
if (x_diff > 0) {
for (let i = 1; i < x_diff; i++) {
if (board[this.coords[0] + i][this.coords[1]].type != PlayeChessEngine.board.pieces.piece_type.no) {
return false
}
}
}
else {
for (let i = 0; i < Math.abs(x_diff); i++) {
if (board[this.coords[0] - i][this.coords[1]].type != PlayeChessEngine.board.pieces.piece_type.no) {
return false
}
}
}
}
else if (Math.abs(x_diff) == Math.abs(y_diff)) {
if (x_diff > 0 && y_diff > 0) {
for (let i = 1; i < x_diff; i++) {
if (board[this.coords[0] + i][this.coords[1] + i].type != PlayeChessEngine.board.pieces.piece_type.no) {
return false
}
}
}
else if (x_diff > 0 && y_diff < 0) {
for (let i = 1; i < x_diff; i++) {
if (board[this.coords[0] + i][this.coords[1] - i].type != PlayeChessEngine.board.pieces.piece_type.no) {
return false
}
}
}
else if (x_diff < 0 && y_diff > 0) {
for (let i = 0; i < Math.abs(x_diff); i++) {
if (board[this.coords[0] - i][this.coords[1] + i].type != PlayeChessEngine.board.pieces.piece_type.no) {
return false
}
}
}
else if (x_diff < 0 && y_diff < 0) {
for (let i = 0; i < Math.abs(x_diff); i++) {
if (board[this.coords[0] - i][this.coords[1] - i].type != PlayeChessEngine.board.pieces.piece_type.no) {
return false
}
}
}
}
return true
}
/**
* Get a string who contains details about the Piece
* @returns {string} The details string
*/
show() {
if (this.is_white) {
let piece_names = ["P", "R", "N", "B", "Q", "K"]
return piece_names[this.type]
}
let piece_names = ["p", "r", "n", "b", "q", "k"]
return piece_names[this.type]
}
/**
* Checks if a move is the same as another
* @param {Move} other The other move
* @returns {boolean} Whether or not the moves are eqal
*/
compare(other) {
if (this.type == other.type && this.is_white == other.is_white && this.coords[0] == other.coords[0] && this.coords[1] == other.coords[1]) {
return true
}
return false
}
}
/**
* The Pawn class
* @extends PlayeChessEngine.board.pieces.Piece
*/
PlayeChessEngine.board.pieces.Pawn = class Pawn extends PlayeChessEngine.board.pieces.Piece {
constructor(is_white, x, y) {
super(PlayeChessEngine.board.pieces.piece_type.p, is_white, x, y)
}
/**
* The validation function for the Pawn
* @param {PlayeChessEngine.board.pieces.Piece[][]} board The board to check on
* @param {number} x_final The final x coordinate
* @param {number} y_final The final y coordinate
* @returns {boolean} Whether or not the move is legal
*/
validation_function(board, x_final, y_final) {
let x_diff = x_final - this.coords[0]
let y_diff = y_final - this.coords[1]
if (y_diff == 0 && x_diff != 0) {
if (this.is_white) {
if (x_diff == 1) {
if (board[x_final][y_final].type == PlayeChessEngine.board.pieces.piece_type.no) {
return true
}
}
else if (x_diff == 2) {
if (!this.has_moved && board[x_final][y_final].type == PlayeChessEngine.board.pieces.piece_type.no) {
return true
}
}
}
else {
if (x_diff == -1) {
if (board[x_final][y_final].type == PlayeChessEngine.board.pieces.piece_type.no) {
return true
}
}
else if (x_diff == -2) {
if (!this.has_moved && board[x_final][y_final].type == PlayeChessEngine.board.pieces.piece_type.no) {
return true
}
}
}
}
else if (Math.abs(x_diff) == 1 && Math.abs(y_diff) == 1) {
if (this.is_white) {
if (x_diff == 1 && y_diff == 1) {
if (board[x_final][y_final].type != PlayeChessEngine.board.pieces.piece_type.no && !board[x_final][y_final].is_white) {
return true
}
}
else if (x_diff == 1 && y_diff == -1) {
if (board[x_final][y_final].type != PlayeChessEngine.board.pieces.piece_type.no && !board[x_final][y_final].is_white) {
return true
}
}
}
else {
if (x_diff == -1 && y_diff == 1) {
if (board[x_final][y_final].type != PlayeChessEngine.board.pieces.piece_type.no && board[x_final][y_final].is_white) {
return true
}
}
else if (x_diff == -1 && y_diff == -1) {
if (board[x_final][y_final].type != PlayeChessEngine.board.pieces.piece_type.no && board[x_final][y_final].is_white) {
return true
}
}
}
}
return false
}
}
/**
* The Bishop class
* @extends PlayeChessEngine.board.pieces.Piece
*/
PlayeChessEngine.board.pieces.Rook = class Rook extends PlayeChessEngine.board.pieces.Piece {
constructor(is_white, x, y) {
super(PlayeChessEngine.board.pieces.piece_type.r, is_white, x, y)
}
/**
* The validation function for the Rook
* @param {PlayeChessEngine.board.pieces.Piece[][]} board The board to check on
* @param {number} x_final The final x coordinate
* @param {number} y_final The final y coordinate
* @returns {boolean} Whether or not the move is legal
*/
validation_function(board, x_final, y_final) {
let x_diff = x_final - this.coords[0]
let y_diff = y_final - this.coords[1]
if (x_diff * y_diff == 0 && x_diff != y_diff) {
if (this.validate_validation(board, x_final, y_final)) {
return this.check_path(board, x_final, y_final)
}
}
return false
}
}
/**
* The Knight class
* @extends PlayeChessEngine.board.pieces.Piece
*/
PlayeChessEngine.board.pieces.Knight = class Knight extends PlayeChessEngine.board.pieces.Piece {
constructor(is_white, x, y) {
super(PlayeChessEngine.board.pieces.piece_type.n, is_white, x, y)
}
/**
* The validation function for the Knight
* @param {PlayeChessEngine.board.pieces.Piece[][]} board The board to check on
* @param {number} x_final The final x coordinate
* @param {number} y_final The final y coordinate
* @returns {boolean} Whether or not the move is legal
*/
validation_function(board, x_final, y_final) {
let x_diff = x_final - this.coords[0]
let y_diff = y_final - this.coords[1]
if ((Math.abs(x_diff) == 2 && Math.abs(y_diff) == 1) || (Math.abs(x_diff) == 1 && Math.abs(y_diff) == 2)) {
return this.validate_validation(board, x_final, y_final)
}
return false
}
}
/**
* The Bishop class
* @extends PlayeChessEngine.board.pieces.Piece
*/
PlayeChessEngine.board.pieces.Bishop = class Bishop extends PlayeChessEngine.board.pieces.Piece {
constructor(is_white, x, y) {
super(PlayeChessEngine.board.pieces.piece_type.b, is_white, x, y)
}
/**
* The validation function for the Bishop
* @param {PlayeChessEngine.board.pieces.Piece[][]} board The board to check on
* @param {number} x_final The final x coordinate
* @param {number} y_final The final y coordinate
* @returns {boolean} Whether or not the move is legal
*/
validation_function(board, x_final, y_final) {
let x_diff = x_final - this.coords[0]
let y_diff = y_final - this.coords[1]
if (Math.abs(x_diff) == Math.abs(y_diff) && x_diff != 0) {
if (this.validate_validation(board, x_final, y_final)) {
return this.check_path(board, x_final, y_final)
}
}
return false
}
}
/**
* The Queen class
* @extends PlayeChessEngine.board.pieces.Piece
*/
PlayeChessEngine.board.pieces.Queen = class Queen extends PlayeChessEngine.board.pieces.Piece {
constructor(is_white, x, y) {
super(PlayeChessEngine.board.pieces.piece_type.q, is_white, x, y)
}
/**
* The validation function for the Queen
* @param {PlayeChessEngine.board.pieces.Piece[][]} board The board to check on
* @param {number} x_final The final x coordinate
* @param {number} y_final The final y coordinate
* @returns {boolean} Whether or not the move is legal
*/
validation_function(board, x_final, y_final) {
let x_diff = x_final - this.coords[0]
let y_diff = y_final - this.coords[1]
if ((Math.abs(x_diff) == Math.abs(y_diff) && x_diff != 0) || (x_diff * y_diff == 0 && x_diff != y_diff)) {
if (this.validate_validation(board, x_final, y_final)) {
return this.check_path(board, x_final, y_final)
}
}
return false
}
}
/**
* The King class
* @extends PlayeChessEngine.board.pieces.Piece
*/
PlayeChessEngine.board.pieces.King = class King extends PlayeChessEngine.board.pieces.Piece {
constructor(is_white, x, y) {
super(PlayeChessEngine.board.pieces.piece_type.k, is_white, x, y)
}
/**
* The validation function for the King
* @param {PlayeChessEngine.board.pieces.Piece[][]} board The board to check on
* @param {number} x_final The final x coordinate
* @param {number} y_final The final y coordinate
* @returns {boolean} Whether or not the move is legal
*/
validation_function(board, x_final, y_final) {
let x_diff = x_final - this.coords[0]
let y_diff = y_final - this.coords[1]
if (Math.abs(x_diff) <= 1 && Math.abs(y_diff) <= 1) {
return this.validate_validation(board, x_final, y_final)
}
return false
}
}
/**
* The Board class
*/
PlayeChessEngine.board.Board = class Board {
Board = [
[new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1)],
[new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1)],
[new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1)],
[new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1)],
[new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1)],
[new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1)],
[new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1)],
[new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1), new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1)],
]
/**
* The board of the game
* @type {PlayeChessEngine.board.pieces.Piece[][]}
*/
set board(board) {
this.Board = board
}
get board() {
return this.Board
}
Boards = []
/**
* The list of boards the game has been through
* @type {PlayeChessEngine.board.pieces.Piece[][][]}
*/
set boards(boards) {
this.Boards = boards
}
get boards() {
return this.Boards
}
Moves = []
/**
* The list of moves the game has been through
* @type {PlayeChessEngine.Move[]}
*/
set moves(moves) {
this.Moves = moves
}
get moves() {
return this.Moves
}
WhiteTurn = true
/**
* If it is white's turn
* @type {boolean}
* @default true
*/
set white_turn(whiteTurn) {
this.WhiteTurn = whiteTurn
}
get white_turn() {
return this.WhiteTurn
}
constructor(fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") {
let fen_array = fen.split(" ")
fen = fen_array[0]
this.white_turn = fen_array[1] == "w"
this.load_fen(fen)
console.log(this.board)
}
/**
* Compare the board to another board
* @param {PlayeChessEngine.board.Board} other
* @returns {boolean} If the boards are the same
*/
compare(other) {
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
if (!this.Board[i][j].compare(other.Board[i][j])) {
return false
}
}
}
if (this.white_turn != other.white_turn) {
return false
}
// come bak here
return true
}
/**
* Reverses the fen string
* @param {string} fen
* @returns {string} The reversed fen string
*/
reverse_fen(fen) {
let fen_array = fen.split("/")
let new_fen = ""
for (let i = fen_array.length - 1; i >= 0; i--) {
new_fen += [...fen_array[i]].reverse().join('') + "/"
}
return new_fen
}
/**
* Loads a fen string into the board (fills the board with pieces)
* @param {string} fen The fen string
*/
load_fen(fen) {
fen = this.reverse_fen(fen)
let fen_arr = [...fen]
let x = 0
let y = 0
fen_arr.forEach(c => {
if (c == "/") {
y = 0
x++
}
else if (c == "8")
return
else if (c >= "1" && c <= "8") {
y += c.charCodeAt(0) - "0".charCodeAt(0)
}
else {
let is_white = c >= "A" && c <= "Z"
switch (c) {
case "P":
case "p":
this.Board[x][y] = new PlayeChessEngine.board.pieces.Pawn(is_white, x, y)
break
case "R":
case "r":
this.Board[x][y] = new PlayeChessEngine.board.pieces.Rook(is_white, x, y)
break
case "N":
case "n":
this.Board[x][y] = new PlayeChessEngine.board.pieces.Knight(is_white, x, y)
break
case "B":
case "b":
this.Board[x][y] = new PlayeChessEngine.board.pieces.Bishop(is_white, x, y)
break
case "Q":
case "q":
this.Board[x][y] = new PlayeChessEngine.board.pieces.Queen(is_white, x, y)
break
case "K":
case "k":
this.Board[x][y] = new PlayeChessEngine.board.pieces.King(is_white, x, y)
break
}
y++
}
})
}
/**
* Prints the board
* @param {PlayeChessEngine.Move[]} [moves=[]] The list of moves to highlight
* @param {PlayeChessEngine.board.Board} [board=this.board] The board to print
*/
print_board(moves = [], board = this.Board) {
let board_str = ""
board_str += " ┌─────────────────┐\n"
for (let i = 0; i < 8; i++) {
board_str += (8 - i) + " │ ";
for (let j = 0; j < 8; j++) {
let skip = false
for (let k = 0; k < moves.length; k++) {
if (moves[k][0] == 7 - i && moves[k][1] == j) {
board_str += "* "
skip = true
break
}
}
if (skip)
continue
if (board[7 - i][j].type == PlayeChessEngine.board.pieces.piece_type.no) {
board_str += ". "
}
else {
board_str += board[7 - i][j].show() + " "
}
}
board_str += "|\n"
}
board_str += " └─────────────────┘\n"
board_str += " a b c d e f g h\n"
console.log(board_str)
}
/**
* Gets all the moves for a given pice
* @param {number} x The x coordinate of the piece
* @param {number} y The y coordinate of the piece
* @param {boolean} [from_premove=false] If the function is called from a premove (used to prevent infinite recursion)
* @returns {PlayeChessEngine.Move[]} The list of moves
*/
get_moves(x, y, from_premove = false) {
if(this.board[x][y].type == PlayeChessEngine.board.pieces.piece_type.no) {
return []
}
let moves = []
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
console.log("Validating " + x + " " + y + " to " + JSON.stringify(this.board[i][j]))
if(this.board[x][y].type == PlayeChessEngine.board.pieces.piece_type.no)
continue
if (this.board[x][y].validation_function(this.board, i, j)) {
let move = new PlayeChessEngine.Move(x, y, i, j)
if (from_premove) {
move.is_valid = true
moves.push(move)
}
else if (!this.premove_check(move, this.board[x][y].is_white)) {
move.is_valid = true
moves.push(move)
}
}
}
}
return moves
}
/**
* Gets all the moves for a given player
* @param {PlayeChessEngine.board.pieces.Piece[]} brd The board to check on
* @param {boolean} white If the player is white
* @param {boolean} [from_premove=false] If the function is called from a premove (used to prevent infinite recursion)
* @returns {PlayeChessEngine.Move[]} The list of moves
*/
get_all_moves(brd, white, from_premove = false) {
let moves = []
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
if (brd[i][j].type == PlayeChessEngine.board.pieces.piece_type.no)
continue
if (brd[i][j].is_white != white)
continue
let tmp_moves = this.get_moves(i, j, from_premove)
for (let k = 0; k < tmp_moves.length; k++) {
moves.push(tmp_moves[k])
}
}
}
return moves
}
/**
* Gets all landing cells for a player
* @param {boolean} white If the player is white
* @param {boolean} [from_premove=false] If the function is called from a premove (used to prevent infinite recursion)
* @returns {[number, number][]} The end coordinates of every single move for the player
*/
get_all_landing_moves(white, from_premove = false) {
let moves = []
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
if (this.board[i][j].type == PlayeChessEngine.board.pieces.piece_type.no)
continue
if (this.board[i][j].is_white != white)
continue
let tmp_moves = this.get_moves(i, j, from_premove)
for (let k = 0; k < tmp_moves.length; k++) {
moves.push([tmp_moves[k].end_coords[0], tmp_moves[k].end_coords[1]])
}
}
}
return moves
}
/**
* Checks if a player is in check
* @param {boolean} white If the player is white
* @returns {boolean} If the player is in check
*/
is_check(white) {
let moves = this.get_all_landing_moves(!white, true)
for (let i = 0; i < moves.length; i++) {
if (this.board[moves[i][0]][moves[i][1]].type == 5)
return true
}
return false
}
get_adapted_copy(piece) {
if(piece instanceof PlayeChessEngine.board.pieces.Pawn) {
return Object.assign(new PlayeChessEngine.board.pieces.Pawn, piece)
}
if(piece instanceof PlayeChessEngine.board.pieces.Rook) {
return Object.assign(new PlayeChessEngine.board.pieces.Rook, piece)
}
if(piece instanceof PlayeChessEngine.board.pieces.Knight) {
return Object.assign(new PlayeChessEngine.board.pieces.Knight, piece)
}
if(piece instanceof PlayeChessEngine.board.pieces.Bishop) {
return Object.assign(new PlayeChessEngine.board.pieces.Bishop, piece)
}
if(piece instanceof PlayeChessEngine.board.pieces.Queen) {
return Object.assign(new PlayeChessEngine.board.pieces.Queen, piece)
}
if(piece instanceof PlayeChessEngine.board.pieces.King) {
return Object.assign(new PlayeChessEngine.board.pieces.King, piece)
}
return Object.assign(new PlayeChessEngine.board.pieces.Piece, piece)
}
/**
* Transfers a piece from start_coords to end_coords
* @param {[number, number]} start_coords The start coords
* @param {*} end_coords
*/
transfer_piece(start_coords, end_coords) {
this.board[end_coords[0]][end_coords[1]] = this.get_adapted_copy(this.board[start_coords[0]][start_coords[1]])
this.board[start_coords[0]][start_coords[1]] = new PlayeChessEngine.board.pieces.Piece(PlayeChessEngine.board.pieces.piece_type.no, true, -1, -1)
this.board[start_coords[0]][start_coords[1]].coords = start_coords
this.board[end_coords[0]][end_coords[1]].coords = end_coords
}
/**
* Copies a board to another (to avoid references)
* @param {Piece[][]} board The board to copy
* @returns {Piece[][]} The board to copy to
*/
copy_board(board) {
let tmp = [[...Array(8)], [...Array(8)], [...Array(8)], [...Array(8)], [...Array(8)], [...Array(8)], [...Array(8)], [...Array(8)]]
for(let i = 0; i < 8; i++) {
for(let j = 0; j < 8; j++) {
tmp[i][j] = this.get_adapted_copy(board[i][j])
}
}
return tmp
}
/**
* Copies the board of the given Board object to another
* @param {Board} board The board to copy
* @returns {Piece[][]} The board to copy to
*/
disref_board(board) {
return this.copy_board(board.board)
}
/**
* Copies the board of a given Board object ot another
* @param {PlayeChessEngine.board.Board} board The board to copy
* @returns {PlayeChessEngine.board.Board} The board to copy to
*/
copy(board) {
let tmp = new PlayeChessEngine.board.Board()
tmp.board = this.copy_board(board.board)
tmp.boards = [...Array(board.boards.length)]
for(let i = 0; i < board.boards.length; i++) {
tmp.boards[i] = this.copy_board(board.boards[i])
}
tmp.moves = [...Array(board.moves.length)]
for(let i = 0; i < board.moves.length; i++) {
tmp.moves[i] = Object.assign(new PlayeChessEngine.Move(), board.moves[i])
}
tmp.white_turn = board.white_turn
return tmp
}
/**
* Checks if a move puts the player who plays it in check
* @param {PlayeChessEngine.Move} move The move to check
* @param {bool} white Whether or not the player is white
* @returns {bool} If the move puts the player in check
*/
premove_check(move, white) {
let tmp_board = this.copy(this)
this.transfer_piece(move.start_coords, move.end_coords)
let is_check = this.is_check(white)
this.board = this.copy_board(tmp_board.board)
return is_check
}
/**
* Gets the status of the game
* @param {bool} white If we want to check the status of white
* @returns {number} The status number (0 -> game ongoing, 1 -> checkmate, 2 -> stalemate)
*/
status(white) {
if (this.get_all_moves(this.board, white).length == 0) {
if (this.is_check(white))
return 1
else
return 2
}
return 0
}
/**
* Checks if the game is in insufficient material situation
* @returns {bool} Whether or not it actually is insufficient material
*/
insufficient_material() {
let num_knights = 0
let num_bishops = 0
this.board.forEach(row => {
row.forEach(piece => {
if (piece.type == PlayeChessEngine.board.pieces.piece_type.no)
return
switch (piece.type) {
case PlayeChessEngine.board.pieces.piece_type.p:
case PlayeChessEngine.board.pieces.piece_type.r:
case PlayeChessEngine.board.pieces.piece_type.q:
return false
case PlayeChessEngine.board.pieces.piece_type.n:
num_knights++
break
case PlayeChessEngine.board.pieces.piece_type.b:
num_bishops++
break
default: break
}
return
})
})
if (num_knights == 0 && num_bishops == 0)
return true
return false
}
/**
* Checks if a player can castle
* @param {bool} white Whether or not the player is white
* @param {bool} kingside If you want to check kingside castling or not
* @returns {bool} If the player can actually castle on the given side
*/
can_castle(white, kingside) {
let row = white ? 7 : 0
let king = this.board[row][4]
if (king.type == PlayeChessEngine.board.pieces.piece_type.no)
return false
if (king.has_moved)
return false
let rook = this.board[row][kingside ? 7 : 0]
if (rook.type == PlayeChessEngine.board.pieces.piece_type.no)
return false
if (rook.has_moved)
return false
let start = kingside ? 5 : 1
let end = kingside ? 7 : 3
for (let i = start; i < end; i++) {
if (this.board[row][i].type != PlayeChessEngine.board.pieces.piece_type.no || this.get_all_landing_moves(!white).includes([row, i])) {
return false
}
}
return true
}
/**
* Castles a player
* @param {bool} white Whether or not the player is white
* @param {bool} kingside If you want to castle kingside or not
*/
castle(white, kingside) {
let row = white ? 7 : 0
let king = this.board[row][4]
let rook = this.board[row][kingside ? 7 : 0]
this.transfer_piece([row, 4], [row, kingside ? 6 : 2])
this.transfer_piece([row, kingside ? 7 : 0], [row, kingside ? 5 : 3])
}
/**
* Gets the coordinate of promotion for a player
* @param {bool} white Whether or not the player is white
* @returns {[number, number]} The coordinates of promotion (or [-1, -1] if the player cannot promote)
*/
get_promotion(white) {
let row = white ? 0 : 7
for (let i = 0; i < 8; i++) {
if (this.board[row][i].type != PlayeChessEngine.board.pieces.piece_type.no) {
if (this.board[row][i].type == PlayeChessEngine.board.pieces.piece_type.p && this.board[row][i].is_white == white) {
return [row, i]
}
}
}
return [-1, -1]
}
/**
* Promote a given player's piece to a given type
* @param {bool} white Whether or not the player is white
* @param {PlayeChessEngine.board.pieces.piece_type} type The type of piece to promote
* @returns {bool} If the player could actually promote
*/
promote(white, type) {
let [row, col] = this.get_promotion(white)
if (row == -1 || col == -1) {
return false
}
if (type == PlayeChessEngine.board.pieces.piece_type.r) {
this.board[row][col] = new PlayeChessEngine.board.pieces.Rook(white, row, col)
}
else if (type == PlayeChessEngine.board.pieces.piece_type.n) {
this.board[row][col] = new PlayeChessEngine.board.pieces.Knight(white, row, col)
}
else if (type == PlayeChessEngine.board.pieces.piece_type.b) {
this.board[row][col] = new PlayeChessEngine.board.pieces.Bishop(white, row, col)
}
else if (type == PlayeChessEngine.board.pieces.piece_type.q) {
this.board[row][col] = new PlayeChessEngine.board.pieces.Queen(white, row, col)
}
else {
return false
}
return true
}
/**
* Checks if the game is in a threefold repetition situation
* @param {bool} white Whether or not it is white to play
* @returns {bool} If the game is actually in threefold repetition
*/
check_threefold_repetition(white) {
let count = 1
for (let i = 0; i < this.boards.length; i++) {
if (this.compare(this.boards[i]) && ((i % 2 == 0) == white))
count++
}
return count >= 3
}
/**
* Gets coordinates of en passant for a player on the given side of a pawn
* @param {bool} white Whether or not the player is white
* @param {number} side The horizontal offset from the pawn (-1 or 1)
* @returns {[number, number]} The coordinates of en passant (or [-1, -1] if the player cannot en passant)
*/
get_en_passant_side(white, side) {
let last_move = this.moves[this.moves.length - 1]