@alepot55/chessboardjs 1.1.0 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +4 -0
- package/README.md +136 -0
- package/chess.js +1994 -0
- package/chessboard.config.js +145 -0
- package/chessboard.css +104 -105
- package/chessboard.js +850 -1207
- package/chessboard.move.js +56 -0
- package/chessboard.piece.js +115 -0
- package/chessboard.square.js +184 -0
- package/jest.config.js +7 -0
- package/package.json +11 -4
- package/test/chessboard.test.js +128 -0
- package/chessboard_min.js +0 -1220
package/chessboard.js
CHANGED
|
@@ -1,1207 +1,850 @@
|
|
|
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
|
-
this.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
this.
|
|
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
|
-
this.
|
|
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
|
-
this.
|
|
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
|
-
if (
|
|
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
|
-
if (!this.
|
|
488
|
-
|
|
489
|
-
if (
|
|
490
|
-
|
|
491
|
-
if (
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
if (
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
if (!
|
|
508
|
-
this.
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
let
|
|
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
|
-
this.
|
|
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
|
-
return
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
return this.
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
this.
|
|
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
|
-
this.
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
if (move
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
this.
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
this.
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
this.
|
|
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
|
-
this.
|
|
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
|
-
if (!this.piece(square)) return false;
|
|
852
|
-
if (this.config.movableColors === 'none') return false;
|
|
853
|
-
if (this.config.movableColors === 'w' && this.colorPiece(square) === 'b') return false;
|
|
854
|
-
if (this.config.movableColors === 'b' && this.colorPiece(square) === 'w') return false;
|
|
855
|
-
if (!this.config.onlyLegalMoves) return true;
|
|
856
|
-
if (this.colorPiece(square) !== this.turn()) return false;
|
|
857
|
-
return true;
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
move(move, animation) {
|
|
861
|
-
|
|
862
|
-
this.checkMove(move);
|
|
863
|
-
|
|
864
|
-
if (!this.config.onlyLegalMoves) {
|
|
865
|
-
let piece = this.piece(move.slice(0, 2));
|
|
866
|
-
this.game.remove(move.slice(0, 2));
|
|
867
|
-
this.game.remove(move.slice(2, 4));
|
|
868
|
-
this.game.put({ type: move[4] ? move[4] : piece[0], color: piece[1] }, move.slice(2, 4));
|
|
869
|
-
return this.updatePosition(false, false);
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
this.unmoveAllSquares();
|
|
873
|
-
|
|
874
|
-
move = this.game.move({
|
|
875
|
-
from: move.slice(0, 2),
|
|
876
|
-
to: move.slice(2, 4),
|
|
877
|
-
promotion: move.length === 5 ? move[4] : null
|
|
878
|
-
});
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
this.history.push(move);
|
|
882
|
-
|
|
883
|
-
this.updatePosition(false, animation);
|
|
884
|
-
|
|
885
|
-
this.moved(move['to']);
|
|
886
|
-
this.moved(move['from']);
|
|
887
|
-
|
|
888
|
-
this.dehintAllSquares();
|
|
889
|
-
|
|
890
|
-
this.config.onMoveEnd(move);
|
|
891
|
-
|
|
892
|
-
return true;
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
moved(square) {
|
|
898
|
-
this.checkSquare(square);
|
|
899
|
-
if (!this.config.moveHighlight) return;
|
|
900
|
-
let elem = this.celle[square];
|
|
901
|
-
if (this.isWhiteSquare(square)) elem.className += ' movedSquareWhite';
|
|
902
|
-
else elem.className += ' movedSquareBlack';
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
unmoved(square) {
|
|
906
|
-
this.checkSquare(square);
|
|
907
|
-
if (!this.config.moveHighlight) return;
|
|
908
|
-
let elem = this.celle[square];
|
|
909
|
-
if (this.isWhiteSquare(square)) elem.className = elem.className.replace(' movedSquareWhite', '');
|
|
910
|
-
else elem.className = elem.className.replace(' movedSquareBlack', '');
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
unmoveAllSquares() {
|
|
914
|
-
for (let casella in this.celle) {
|
|
915
|
-
this.unmoved(casella);
|
|
916
|
-
}
|
|
917
|
-
return;
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
legalMove(mossa) {
|
|
921
|
-
let legalMoves = this.legalMoves(mossa.slice(0, 2));
|
|
922
|
-
for (let i in legalMoves) {
|
|
923
|
-
if (legalMoves[i]['to'] === mossa.slice(2, 4) && (mossa.length === 4 || mossa[4] === legalMoves[i]['promotion'])) return true;
|
|
924
|
-
}
|
|
925
|
-
|
|
926
|
-
return false;
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
legalMoves(from = null, verb = true) {
|
|
930
|
-
if (from) this.checkSquare(from);
|
|
931
|
-
else return this.game.moves({ verbose: verb });
|
|
932
|
-
return this.game.moves({ square: from, verbose: verb });
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
lastMove() {
|
|
936
|
-
return this.history[this.history.length - 1];
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
history() {
|
|
940
|
-
return this.history;
|
|
941
|
-
}
|
|
942
|
-
|
|
943
|
-
// State
|
|
944
|
-
|
|
945
|
-
isGameOver() {
|
|
946
|
-
if (this.game.game_over()) {
|
|
947
|
-
if (this.game.in_checkmate()) return this.game.turn() === 'w' ? 'b' : 'w';
|
|
948
|
-
return 'd';
|
|
949
|
-
}
|
|
950
|
-
return null;
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
turn() {
|
|
954
|
-
return this.game.turn();
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
getOrientation() {
|
|
958
|
-
return this.config.orientation;
|
|
959
|
-
}
|
|
960
|
-
|
|
961
|
-
orientation(color) {
|
|
962
|
-
if ((color === 'w' || color === 'b') && color !== this.config.orientation) this.flip();
|
|
963
|
-
}
|
|
964
|
-
|
|
965
|
-
// Position
|
|
966
|
-
|
|
967
|
-
chageFenTurn(fen, color) {
|
|
968
|
-
let parts = fen.split(' ');
|
|
969
|
-
parts[1] = color;
|
|
970
|
-
return parts.join(' ');
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
position(position, color = null) {
|
|
974
|
-
this.initParams();
|
|
975
|
-
this.dehintAllSquares();
|
|
976
|
-
this.deselectAllSquares();
|
|
977
|
-
this.unmoveAllSquares();
|
|
978
|
-
if (!color) color = position.split(' ')[1];
|
|
979
|
-
let change_color = this.config.orientation !== color;
|
|
980
|
-
this.config.setOrientation(color);
|
|
981
|
-
this.game = new Chess(position);
|
|
982
|
-
this.updatePosition(change_color);
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
flip() {
|
|
986
|
-
let position = this.game.fen();
|
|
987
|
-
this.position(position, this.config.orientation === 'w' ? 'b' : 'w');
|
|
988
|
-
}
|
|
989
|
-
|
|
990
|
-
playerTurn() { // Restituisce true se è il turno del giocatore
|
|
991
|
-
return this.config.orientation === this.game.turn();
|
|
992
|
-
}
|
|
993
|
-
|
|
994
|
-
isWhiteSquare(square) {
|
|
995
|
-
this.checkSquare(square);
|
|
996
|
-
let letters = 'abcdefgh';
|
|
997
|
-
return (letters.indexOf(square[0]) + parseInt(square[1])) % 2 === 0;
|
|
998
|
-
}
|
|
999
|
-
|
|
1000
|
-
isWhiteOriented() {
|
|
1001
|
-
return this.config.orientation === 'w';
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
updatePosition(change_color = false, animation = this.config.moveAnimation) {
|
|
1005
|
-
if (change_color) {
|
|
1006
|
-
this.renameSquares();
|
|
1007
|
-
}
|
|
1008
|
-
this.updatePieces(animation);
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
renameSquares() {
|
|
1012
|
-
let new_celle = {};
|
|
1013
|
-
let new_pezzi = {};
|
|
1014
|
-
let new_pieces = {};
|
|
1015
|
-
for (let elem in this.celle) {
|
|
1016
|
-
let square = this.celle[elem];
|
|
1017
|
-
let id = square.id;
|
|
1018
|
-
let [row, col] = this.getSquareCoord(id);
|
|
1019
|
-
let new_row = 7 - row;
|
|
1020
|
-
let new_col = 7 - col;
|
|
1021
|
-
square.id = this.getSquareID(new_row, new_col);
|
|
1022
|
-
new_celle[square.id] = square;
|
|
1023
|
-
if (this.pezzi[id]) {
|
|
1024
|
-
new_pezzi[square.id] = this.pezzi[id];
|
|
1025
|
-
new_pieces[(this.pezzi[id]['piece'], square.id)] = this.pieces[(this.pezzi[id]['piece'], id)];
|
|
1026
|
-
}
|
|
1027
|
-
|
|
1028
|
-
}
|
|
1029
|
-
this.celle = new_celle;
|
|
1030
|
-
this.pezzi = new_pezzi;
|
|
1031
|
-
this.pieces = new_pieces;
|
|
1032
|
-
}
|
|
1033
|
-
|
|
1034
|
-
fen() {
|
|
1035
|
-
return this.game.fen();
|
|
1036
|
-
}
|
|
1037
|
-
|
|
1038
|
-
// Squares
|
|
1039
|
-
|
|
1040
|
-
checkSquare(square) {
|
|
1041
|
-
if (!square) return;
|
|
1042
|
-
if (['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'].indexOf(square[0]) === -1 || ['1', '2', '3', '4', '5', '6', '7', '8'].indexOf(square[1]) === -1) throw new Error('Invalid square - ' + square + ' - must be a valid square like "a1" or "h8"');
|
|
1043
|
-
}
|
|
1044
|
-
|
|
1045
|
-
getSquareCoord(coord) {
|
|
1046
|
-
let letters = 'abcdefgh';
|
|
1047
|
-
if (this.isWhiteOriented()) {
|
|
1048
|
-
return [8 - parseInt(coord[1]), letters.indexOf(coord[0])];
|
|
1049
|
-
}
|
|
1050
|
-
return [parseInt(coord[1]) - 1, 7 - letters.indexOf(coord[0])];
|
|
1051
|
-
}
|
|
1052
|
-
|
|
1053
|
-
resetSquare(square) {
|
|
1054
|
-
let elem = this.celle[square];
|
|
1055
|
-
elem.className = 'square ' + (this.isWhiteSquare(square) ? 'whiteSquare' : 'blackSquare');
|
|
1056
|
-
}
|
|
1057
|
-
|
|
1058
|
-
getSquareID(row, col) {
|
|
1059
|
-
row = parseInt(row);
|
|
1060
|
-
col = parseInt(col);
|
|
1061
|
-
if (this.isWhiteOriented()) {
|
|
1062
|
-
row = 8 - row;
|
|
1063
|
-
col = col + 1;
|
|
1064
|
-
} else {
|
|
1065
|
-
row = row + 1;
|
|
1066
|
-
col = 8 - col;
|
|
1067
|
-
}
|
|
1068
|
-
let letters = 'abcdefgh';
|
|
1069
|
-
let letter = letters[col - 1];
|
|
1070
|
-
return letter + row;
|
|
1071
|
-
}
|
|
1072
|
-
|
|
1073
|
-
removeSquares() { // Rimuove le caselle dalla Chessboard
|
|
1074
|
-
for (let casella in this.celle) {
|
|
1075
|
-
this.board.removeChild(this.celle[casella]);
|
|
1076
|
-
}
|
|
1077
|
-
this.celle = {};
|
|
1078
|
-
}
|
|
1079
|
-
|
|
1080
|
-
clear(animation = true) {
|
|
1081
|
-
this.game.clear();
|
|
1082
|
-
this.updatePosition(null, animation);
|
|
1083
|
-
}
|
|
1084
|
-
|
|
1085
|
-
// Highlight
|
|
1086
|
-
|
|
1087
|
-
highlight(square) {
|
|
1088
|
-
if (!square || !this.celle[square] || !this.config.overHighlight) return;
|
|
1089
|
-
let elem = this.celle[square];
|
|
1090
|
-
elem.className += ' highlighted';
|
|
1091
|
-
}
|
|
1092
|
-
|
|
1093
|
-
dehighlight(square) {
|
|
1094
|
-
if (!square || !this.celle[square] || !this.config.overHighlight) return;
|
|
1095
|
-
this.checkSquare(square);
|
|
1096
|
-
let elem = this.celle[square];
|
|
1097
|
-
elem.className = elem.className.replace(' highlighted', '');
|
|
1098
|
-
}
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
// Promotion
|
|
1102
|
-
|
|
1103
|
-
coverSquare(square) {
|
|
1104
|
-
let cover = document.createElement("div");
|
|
1105
|
-
cover.className = "square cover";
|
|
1106
|
-
this.celle[square].appendChild(cover);
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
removeCover(square) {
|
|
1110
|
-
let elem = this.celle[square];
|
|
1111
|
-
let figli = elem.childNodes;
|
|
1112
|
-
|
|
1113
|
-
for (let i = figli.length - 1; i >= 0; i--) {
|
|
1114
|
-
if (figli[i].className.includes('cover')) {
|
|
1115
|
-
elem.removeChild(figli[i]);
|
|
1116
|
-
}
|
|
1117
|
-
}
|
|
1118
|
-
}
|
|
1119
|
-
|
|
1120
|
-
removeAllCovers() {
|
|
1121
|
-
for (let casella in this.celle) {
|
|
1122
|
-
this.removeCover(casella);
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
|
|
1126
|
-
promoteSquare(square, piece) {
|
|
1127
|
-
let choice = document.createElement("div");
|
|
1128
|
-
choice.className = "square choice";
|
|
1129
|
-
|
|
1130
|
-
let img = document.createElement("img");
|
|
1131
|
-
img.className = "piece choicable";
|
|
1132
|
-
img.src = this.getPiecePath(piece);
|
|
1133
|
-
choice.appendChild(img);
|
|
1134
|
-
|
|
1135
|
-
this.celle[square].appendChild(choice);
|
|
1136
|
-
|
|
1137
|
-
return choice;
|
|
1138
|
-
}
|
|
1139
|
-
|
|
1140
|
-
depromoteSquare(square) {
|
|
1141
|
-
let elem = this.celle[square];
|
|
1142
|
-
let figli = elem.childNodes;
|
|
1143
|
-
|
|
1144
|
-
for (let i = figli.length - 1; i >= 0; i--) {
|
|
1145
|
-
if (figli[i].className.includes('choice')) {
|
|
1146
|
-
elem.removeChild(figli[i]);
|
|
1147
|
-
}
|
|
1148
|
-
}
|
|
1149
|
-
}
|
|
1150
|
-
|
|
1151
|
-
depromoteAllSquares() {
|
|
1152
|
-
for (let casella in this.celle) {
|
|
1153
|
-
this.depromoteSquare(casella);
|
|
1154
|
-
}
|
|
1155
|
-
}
|
|
1156
|
-
|
|
1157
|
-
promote(mossa) {
|
|
1158
|
-
|
|
1159
|
-
if (!this.config.onlyLegalMoves) return false;
|
|
1160
|
-
|
|
1161
|
-
let to = mossa.slice(2, 4);
|
|
1162
|
-
let from = mossa.slice(0, 2);
|
|
1163
|
-
let pezzo = this.game.get(from);
|
|
1164
|
-
let [row, col] = this.getSquareCoord(to);
|
|
1165
|
-
let choices = ['q', 'r', 'b', 'n']
|
|
1166
|
-
|
|
1167
|
-
if (pezzo['type'] !== 'p' || !(row === 0 || row === 7)) return false;
|
|
1168
|
-
|
|
1169
|
-
this.promoting = true;
|
|
1170
|
-
|
|
1171
|
-
for (let casella in this.celle) {
|
|
1172
|
-
let [rowCurr, colCurr] = this.getSquareCoord(casella);
|
|
1173
|
-
|
|
1174
|
-
if (col === colCurr && Math.abs(row - rowCurr) <= 3) {
|
|
1175
|
-
|
|
1176
|
-
let choice = this.promoteSquare(casella, choices[Math.abs(row - rowCurr)] + pezzo['color']);
|
|
1177
|
-
choice.addEventListener('click', () => {
|
|
1178
|
-
this.onClick(to + choices[Math.abs(row - rowCurr)]);
|
|
1179
|
-
});
|
|
1180
|
-
|
|
1181
|
-
} else {
|
|
1182
|
-
this.coverSquare(casella);
|
|
1183
|
-
}
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
|
-
this.lastSquare = from;
|
|
1187
|
-
|
|
1188
|
-
return true;
|
|
1189
|
-
}
|
|
1190
|
-
|
|
1191
|
-
// Other
|
|
1192
|
-
|
|
1193
|
-
transitionTimingFunction(x, type = 'ease') {
|
|
1194
|
-
switch (type) {
|
|
1195
|
-
case 'linear':
|
|
1196
|
-
return x;
|
|
1197
|
-
case 'ease':
|
|
1198
|
-
return (x ** 2) * (3 - 2 * x);
|
|
1199
|
-
case 'ease-in':
|
|
1200
|
-
return x ** 2;
|
|
1201
|
-
case 'ease-out':
|
|
1202
|
-
return -1 * (x - 1) ** 2 + 1;
|
|
1203
|
-
case 'ease-in-out':
|
|
1204
|
-
return (x < 0.5) ? 2 * x ** 2 : 4 * x - 2 * x ** 2 - 1;
|
|
1205
|
-
}
|
|
1206
|
-
}
|
|
1207
|
-
}
|
|
1
|
+
import { Chess, validateFen } from './chess.js';
|
|
2
|
+
import ChessboardConfig from './chessboard.config.js';
|
|
3
|
+
import Square from './chessboard.square.js';
|
|
4
|
+
import Piece from './chessboard.piece.js';
|
|
5
|
+
import Move from './chessboard.move.js';
|
|
6
|
+
|
|
7
|
+
class Chessboard {
|
|
8
|
+
|
|
9
|
+
standard_positions = {
|
|
10
|
+
'start': 'start',
|
|
11
|
+
'default': 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
error_messages = {
|
|
15
|
+
'invalid_position': 'Invalid position - ',
|
|
16
|
+
'invalid_id_div': 'Board id not found - ',
|
|
17
|
+
'invalid_value': 'Invalid value - ',
|
|
18
|
+
'invalid_piece': 'Invalid piece - ',
|
|
19
|
+
'invalid_square': 'Invalid square - ',
|
|
20
|
+
'invalid_fen': 'Invalid fen - ',
|
|
21
|
+
'invalid_orientation': 'Invalid orientation - ',
|
|
22
|
+
'invalid_color': 'Invalid color - ',
|
|
23
|
+
'invalid_mode': 'Invalid mode - ',
|
|
24
|
+
'invalid_dropOffBoard': 'Invalid dropOffBoard - ',
|
|
25
|
+
'invalid_snapbackTime': 'Invalid snapbackTime - ',
|
|
26
|
+
'invalid_snapbackAnimation': 'Invalid snapbackAnimation - ',
|
|
27
|
+
'invalid_fadeTime': 'Invalid fadeTime - ',
|
|
28
|
+
'invalid_fadeAnimation': 'Invalid fadeAnimation - ',
|
|
29
|
+
'invalid_ratio': 'Invalid ratio - ',
|
|
30
|
+
'invalid_piecesPath': 'Invalid piecesPath - ',
|
|
31
|
+
'invalid_onMove': 'Invalid onMove - ',
|
|
32
|
+
'invalid_onMoveEnd': 'Invalid onMoveEnd - ',
|
|
33
|
+
'invalid_onChange': 'Invalid onChange - ',
|
|
34
|
+
'invalid_onDragStart': 'Invalid onDragStart - ',
|
|
35
|
+
'invalid_onDragMove': 'Invalid onDragMove - ',
|
|
36
|
+
'invalid_onDrop': 'Invalid onDrop - ',
|
|
37
|
+
'invalid_onSnapbackEnd': 'Invalid onSnapbackEnd - ',
|
|
38
|
+
'invalid_whiteSquare': 'Invalid whiteSquare - ',
|
|
39
|
+
'invalid_blackSquare': 'Invalid blackSquare - ',
|
|
40
|
+
'invalid_highlight': 'Invalid highlight - ',
|
|
41
|
+
'invalid_selectedSquareWhite': 'Invalid selectedSquareWhite - ',
|
|
42
|
+
'invalid_selectedSquareBlack': 'Invalid selectedSquareBlack - ',
|
|
43
|
+
'invalid_movedSquareWhite': 'Invalid movedSquareWhite - ',
|
|
44
|
+
'invalid_movedSquareBlack': 'Invalid movedSquareBlack - ',
|
|
45
|
+
'invalid_choiceSquare': 'Invalid choiceSquare - ',
|
|
46
|
+
'invalid_coverSquare': 'Invalid coverSquare - ',
|
|
47
|
+
'invalid_hintColor': 'Invalid hintColor - ',
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
constructor(config) {
|
|
51
|
+
this.config = new ChessboardConfig(config);
|
|
52
|
+
this.init();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Build
|
|
56
|
+
|
|
57
|
+
init() {
|
|
58
|
+
this.initParams();
|
|
59
|
+
this.buildGame(this.config.position);
|
|
60
|
+
this.buildBoard();
|
|
61
|
+
this.buildSquares();
|
|
62
|
+
this.addListeners();
|
|
63
|
+
this.updateBoardPieces();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
initParams() {
|
|
67
|
+
this.board = null;
|
|
68
|
+
this.squares = {};
|
|
69
|
+
this.promoting = false;
|
|
70
|
+
this.clicked = null;
|
|
71
|
+
this.movesHistory = [];
|
|
72
|
+
this.mosseIndietro = [];
|
|
73
|
+
this.clicked = null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
buildGame(position) {
|
|
77
|
+
if (typeof position === 'object') {
|
|
78
|
+
this.game = new Chess('start');
|
|
79
|
+
Object.entries(position).forEach(([square, [type, color]]) => {
|
|
80
|
+
this.game.put({ type, color }, square);
|
|
81
|
+
});
|
|
82
|
+
} else if (Object.values(this.standard_positions).includes(position)) {
|
|
83
|
+
if (position === 'start') this.game = new Chess();
|
|
84
|
+
else this.game = new Chess(this.standard_positions[position]);
|
|
85
|
+
} else if (validateFen(position)) {
|
|
86
|
+
this.game = new Chess(position);
|
|
87
|
+
} else {
|
|
88
|
+
throw new Error(this.error_messages['invalid_position'] + position);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
buildBoard() {
|
|
93
|
+
this.board = document.getElementById(this.config.id_div);
|
|
94
|
+
if (!this.board) {
|
|
95
|
+
throw new Error(this.error_messages['invalid_id_div'] + this.config.id_div);
|
|
96
|
+
}
|
|
97
|
+
this.resize(this.config.size);
|
|
98
|
+
this.board.className = "board";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
realCoord(row, col) {
|
|
102
|
+
if (this.isWhiteOriented()) row = 7 - row;
|
|
103
|
+
else col = 7 - col;
|
|
104
|
+
return [row + 1, col + 1];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
buildSquares() {
|
|
108
|
+
|
|
109
|
+
for (let row = 0; row < 8; row++) {
|
|
110
|
+
for (let col = 0; col < 8; col++) {
|
|
111
|
+
|
|
112
|
+
let [square_row, square_col] = this.realCoord(row, col);
|
|
113
|
+
let square = new Square(square_row, square_col);
|
|
114
|
+
this.squares[square.getId()] = square;
|
|
115
|
+
|
|
116
|
+
this.board.appendChild(square.element);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
removeBoard() {
|
|
122
|
+
|
|
123
|
+
this.board.innerHTML = '';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Pieces
|
|
127
|
+
|
|
128
|
+
getPiecePath(piece) {
|
|
129
|
+
if (typeof this.config.piecesPath === 'string')
|
|
130
|
+
return this.config.piecesPath + '/' + piece + '.svg';
|
|
131
|
+
else if (typeof this.config.piecesPath === 'object')
|
|
132
|
+
return this.config.piecesPath[piece];
|
|
133
|
+
else if (typeof this.config.piecesPath === 'function')
|
|
134
|
+
return this.config.piecesPath(piece);
|
|
135
|
+
else
|
|
136
|
+
throw new Error(this.error_messages['invalid_piecesPath']);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
colorPiece(square) {
|
|
140
|
+
let piece = this.piece(square);
|
|
141
|
+
return piece ? piece[1] : null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
movePiece(piece, to, duration, callback) {
|
|
145
|
+
piece.translate(to, duration, this.transitionTimingFunction, this.config.moveAnimation, callback);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
translatePiece(move, removeTo, animate, callback = null) {
|
|
149
|
+
|
|
150
|
+
if (removeTo) this.removePieceFromSquare(move.to, false);
|
|
151
|
+
|
|
152
|
+
let change_square = () => {
|
|
153
|
+
move.from.removePiece();
|
|
154
|
+
move.to.putPiece(move.piece);
|
|
155
|
+
move.piece.setDrag(this.dragFunction(move.to, move.piece));
|
|
156
|
+
if (callback) callback();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let duration = animate ? this.config.moveTime : 0;
|
|
160
|
+
|
|
161
|
+
this.movePiece(move.piece, move.to, duration, change_square);
|
|
162
|
+
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
snapbackPiece(square, animate) {
|
|
166
|
+
let move = new Move(square, square);
|
|
167
|
+
this.translatePiece(move, false, animate);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
convertSquare(square) {
|
|
171
|
+
if (square instanceof Square) return square;
|
|
172
|
+
if (typeof square === 'string' && this.squares[square]) return this.squares[square];
|
|
173
|
+
throw new Error('Invalid square value');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
removePieceFromSquare(square, fade = true) {
|
|
177
|
+
|
|
178
|
+
square = this.convertSquare(square);
|
|
179
|
+
square.check();
|
|
180
|
+
|
|
181
|
+
let piece = square.piece;
|
|
182
|
+
|
|
183
|
+
if (!piece) throw Error('Square has no piece to remove.')
|
|
184
|
+
|
|
185
|
+
if (fade) piece.fadeOut(
|
|
186
|
+
this.config.fadeTime,
|
|
187
|
+
this.config.fadeAnimation,
|
|
188
|
+
this.transitionTimingFunction);
|
|
189
|
+
|
|
190
|
+
square.removePiece();
|
|
191
|
+
|
|
192
|
+
return piece;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
dragFunction(square, piece) {
|
|
196
|
+
|
|
197
|
+
return (event) => {
|
|
198
|
+
|
|
199
|
+
event.preventDefault();
|
|
200
|
+
|
|
201
|
+
if (!this.config.draggable || !piece) return;
|
|
202
|
+
if (!this.config.onDragStart(square, piece)) return;
|
|
203
|
+
|
|
204
|
+
let prec;
|
|
205
|
+
let from = square;
|
|
206
|
+
let to = square;
|
|
207
|
+
|
|
208
|
+
const img = piece.element;
|
|
209
|
+
|
|
210
|
+
if (!this.canMove(from)) return;
|
|
211
|
+
if (!this.config.clickable) this.clicked = null;
|
|
212
|
+
if (this.onClick(from)) return;
|
|
213
|
+
|
|
214
|
+
img.style.position = 'absolute';
|
|
215
|
+
img.style.zIndex = 100;
|
|
216
|
+
|
|
217
|
+
const moveAt = (pageX, pageY) => {
|
|
218
|
+
const halfWidth = img.offsetWidth / 2;
|
|
219
|
+
const halfHeight = img.offsetHeight / 2;
|
|
220
|
+
img.style.left = `${pageX - halfWidth}px`;
|
|
221
|
+
img.style.top = `${pageY - halfHeight}px`;
|
|
222
|
+
return true;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const onMouseMove = (event) => {
|
|
226
|
+
if (!moveAt(event.pageX, event.pageY)) return;
|
|
227
|
+
|
|
228
|
+
const boardRect = this.board.getBoundingClientRect();
|
|
229
|
+
const { offsetWidth: boardWidth, offsetHeight: boardHeight } = this.board;
|
|
230
|
+
const x = event.clientX - boardRect.left;
|
|
231
|
+
const y = event.clientY - boardRect.top;
|
|
232
|
+
|
|
233
|
+
let newTo = null;
|
|
234
|
+
if (x >= 0 && x <= boardWidth && y >= 0 && y <= boardHeight) {
|
|
235
|
+
const col = Math.floor(x / (boardWidth / 8));
|
|
236
|
+
const row = Math.floor(y / (boardHeight / 8));
|
|
237
|
+
newTo = this.squares[this.getSquareID(row, col)];
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
to = newTo;
|
|
241
|
+
this.config.onDragMove(from, to, piece);
|
|
242
|
+
|
|
243
|
+
if (to !== prec) {
|
|
244
|
+
to?.highlight();
|
|
245
|
+
prec?.dehighlight();
|
|
246
|
+
prec = to;
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
const onMouseUp = () => {
|
|
251
|
+
prec?.dehighlight();
|
|
252
|
+
document.removeEventListener('mousemove', onMouseMove);
|
|
253
|
+
window.removeEventListener('mouseup', onMouseUp);
|
|
254
|
+
img.style.zIndex = 20;
|
|
255
|
+
|
|
256
|
+
const dropResult = this.config.onDrop(from, to, piece);
|
|
257
|
+
const isTrashDrop = !to && (this.config.dropOffBoard === 'trash' || dropResult === 'trash');
|
|
258
|
+
|
|
259
|
+
if (isTrashDrop) {
|
|
260
|
+
this.allSquares("unmoved");
|
|
261
|
+
this.allSquares('removeHint');
|
|
262
|
+
from.deselect();
|
|
263
|
+
this.remove(from);
|
|
264
|
+
} else if (!to || !this.onClick(to, true)) {
|
|
265
|
+
this.snapbackPiece(from, !this.promoting);
|
|
266
|
+
this.config.onSnapbackEnd(from, piece);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
window.addEventListener('mouseup', onMouseUp, { once: true });
|
|
271
|
+
document.addEventListener('mousemove', onMouseMove);
|
|
272
|
+
img.addEventListener('mouseup', onMouseUp, { once: true });
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
convertPiece(piece) {
|
|
277
|
+
if (piece instanceof Piece) return piece;
|
|
278
|
+
if (typeof piece === 'string') {
|
|
279
|
+
let [type, color] = piece.split('');
|
|
280
|
+
return new Piece(color, type, this.getPiecePath(piece));
|
|
281
|
+
}
|
|
282
|
+
throw new Error(this.error_messages['invalid_piece'] + piece);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
addPieceOnSquare(square, piece, fade = true) {
|
|
286
|
+
|
|
287
|
+
square.putPiece(piece);
|
|
288
|
+
piece.setDrag(this.dragFunction(square, piece));
|
|
289
|
+
|
|
290
|
+
if (fade) piece.fadeIn(
|
|
291
|
+
this.config.fadeTime,
|
|
292
|
+
this.config.fadeAnimation,
|
|
293
|
+
this.transitionTimingFunction
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
piece.visible();
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
updateBoardPieces(animation = false) {
|
|
300
|
+
let { updatedFlags, escapeFlags, movableFlags, pendingTranslations } = this.prepareBoardUpdateData();
|
|
301
|
+
|
|
302
|
+
this.identifyPieceTranslations(updatedFlags, escapeFlags, movableFlags, pendingTranslations);
|
|
303
|
+
|
|
304
|
+
this.executePieceTranslations(pendingTranslations, escapeFlags, animation);
|
|
305
|
+
|
|
306
|
+
this.processRemainingPieceUpdates(updatedFlags, animation);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
prepareBoardUpdateData() {
|
|
310
|
+
let updatedFlags = {};
|
|
311
|
+
let escapeFlags = {};
|
|
312
|
+
let movableFlags = {};
|
|
313
|
+
let pendingTranslations = [];
|
|
314
|
+
|
|
315
|
+
for (let squareId in this.squares) {
|
|
316
|
+
let cellPiece = this.squares[squareId].piece;
|
|
317
|
+
let cellPieceId = cellPiece ? cellPiece.getId() : null;
|
|
318
|
+
updatedFlags[squareId] = this.piece(squareId) === cellPieceId;
|
|
319
|
+
escapeFlags[squareId] = false;
|
|
320
|
+
movableFlags[squareId] = cellPiece ? this.piece(squareId) !== cellPieceId : false;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return { updatedFlags, escapeFlags, movableFlags, pendingTranslations };
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
identifyPieceTranslations(updatedFlags, escapeFlags, movableFlags, pendingTranslations) {
|
|
327
|
+
Object.values(this.squares).forEach(targetSquare => {
|
|
328
|
+
const newPieceId = this.piece(targetSquare.id);
|
|
329
|
+
const newPiece = newPieceId && this.convertPiece(newPieceId);
|
|
330
|
+
const currentPiece = targetSquare.piece;
|
|
331
|
+
const currentPieceId = currentPiece ? currentPiece.getId() : null;
|
|
332
|
+
|
|
333
|
+
if (currentPieceId === newPieceId || updatedFlags[targetSquare.id]) return;
|
|
334
|
+
|
|
335
|
+
this.evaluateTranslationCandidates(
|
|
336
|
+
targetSquare,
|
|
337
|
+
newPiece,
|
|
338
|
+
currentPiece,
|
|
339
|
+
updatedFlags,
|
|
340
|
+
escapeFlags,
|
|
341
|
+
movableFlags,
|
|
342
|
+
pendingTranslations
|
|
343
|
+
);
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
evaluateTranslationCandidates(targetSquare, newPiece, oldPiece, updatedFlags, escapeFlags, movableFlags, pendingTranslations) {
|
|
348
|
+
if (!newPiece) return;
|
|
349
|
+
const newPieceId = newPiece.getId();
|
|
350
|
+
|
|
351
|
+
for (const sourceSquare of Object.values(this.squares)) {
|
|
352
|
+
if (sourceSquare.id === targetSquare.id || updatedFlags[targetSquare.id]) continue;
|
|
353
|
+
|
|
354
|
+
const sourcePiece = sourceSquare.piece;
|
|
355
|
+
if (!sourcePiece || !movableFlags[sourceSquare.id] || this.isPiece(newPieceId, sourceSquare.id)) continue;
|
|
356
|
+
|
|
357
|
+
if (sourcePiece.id === newPieceId) {
|
|
358
|
+
this.handleTranslationMovement(targetSquare, sourceSquare, oldPiece, sourcePiece, updatedFlags, escapeFlags, movableFlags, pendingTranslations);
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
handleTranslationMovement(targetSquare, sourceSquare, oldPiece, currentSource, updatedFlags, escapeFlags, movableFlags, pendingTranslations) {
|
|
365
|
+
// Verifica il caso specifico "en passant"
|
|
366
|
+
let lastMove = this.lastMove();
|
|
367
|
+
if (!oldPiece && lastMove && lastMove['captured'] === 'p') {
|
|
368
|
+
this.removePieceFromSquare(this.squares[targetSquare.id[0] + sourceSquare.id[1]]);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
pendingTranslations.push([currentSource, sourceSquare, targetSquare]);
|
|
372
|
+
|
|
373
|
+
if (!this.piece(sourceSquare.id)) updatedFlags[sourceSquare.id] = true;
|
|
374
|
+
|
|
375
|
+
escapeFlags[sourceSquare.id] = true;
|
|
376
|
+
movableFlags[sourceSquare.id] = false;
|
|
377
|
+
updatedFlags[targetSquare.id] = true;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
executePieceTranslations(pendingTranslations, escapeFlags, animation) {
|
|
381
|
+
for (let [_, sourceSquare, targetSquare] of pendingTranslations) {
|
|
382
|
+
console.log('executing translation: ', sourceSquare.id, targetSquare.id);
|
|
383
|
+
let removeTarget = !escapeFlags[targetSquare.id] && targetSquare.piece;
|
|
384
|
+
let moveObj = new Move(sourceSquare, targetSquare);
|
|
385
|
+
this.translatePiece(moveObj, removeTarget, animation);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Gestisce gli aggiornamenti residui per ogni cella che non è ancora stata correttamente aggiornata
|
|
390
|
+
processRemainingPieceUpdates(updatedFlags, animation) {
|
|
391
|
+
for (const square of Object.values(this.squares)) {
|
|
392
|
+
let newPieceId = this.piece(square.id);
|
|
393
|
+
let newPiece = newPieceId ? this.convertPiece(newPieceId) : null;
|
|
394
|
+
let currentPiece = square.piece;
|
|
395
|
+
let currentPieceId = currentPiece ? currentPiece.getId() : null;
|
|
396
|
+
|
|
397
|
+
if (currentPieceId !== newPieceId && !updatedFlags[square.id]) {
|
|
398
|
+
this.updateSinglePiece(square, newPiece, updatedFlags, animation);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// Aggiorna il pezzo in una cella specifica. Gestisce anche il caso di promozione
|
|
404
|
+
updateSinglePiece(square, newPiece, updatedFlags, animation) {
|
|
405
|
+
if (!updatedFlags[square.id]) {
|
|
406
|
+
let lastMove = this.lastMove();
|
|
407
|
+
|
|
408
|
+
if (lastMove?.promotion) {
|
|
409
|
+
if (lastMove['to'] === square.id) {
|
|
410
|
+
|
|
411
|
+
let move = new Move(this.squares[lastMove['from']], square);
|
|
412
|
+
this.translatePiece(move, true, animation
|
|
413
|
+
, () => {
|
|
414
|
+
move.to.removePiece();
|
|
415
|
+
this.addPieceOnSquare(square, newPiece);
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
} else {
|
|
419
|
+
if (square.piece) this.removePieceFromSquare(square);
|
|
420
|
+
if (newPiece) this.addPieceOnSquare(square, newPiece);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
isPiece(piece, square) { return this.piece(square) === piece }
|
|
426
|
+
|
|
427
|
+
// Listeners
|
|
428
|
+
|
|
429
|
+
addListeners() {
|
|
430
|
+
for (const square of Object.values(this.squares)) {
|
|
431
|
+
|
|
432
|
+
let piece = square.piece;
|
|
433
|
+
|
|
434
|
+
square.element.addEventListener("mouseover", (e) => {
|
|
435
|
+
if (!this.clicked) this.hintMoves(square);
|
|
436
|
+
});
|
|
437
|
+
square.element.addEventListener("mouseout", (e) => {
|
|
438
|
+
if (!this.clicked) this.dehintMoves(square);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
const handleClick = (e) => {
|
|
442
|
+
e.stopPropagation();
|
|
443
|
+
if (this.config.clickable && (!piece || this.config.onlyLegalMoves)) this.onClick(square)
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
square.element.addEventListener("click", handleClick);
|
|
447
|
+
square.element.addEventListener("touch", handleClick);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
onClick(square, animation = this.config.moveAnimation) {
|
|
452
|
+
|
|
453
|
+
if (square.id === this.clicked?.id) return false;
|
|
454
|
+
|
|
455
|
+
let from = this.clicked;
|
|
456
|
+
this.clicked = null;
|
|
457
|
+
|
|
458
|
+
let promotion = null
|
|
459
|
+
|
|
460
|
+
if (this.promoting) {
|
|
461
|
+
if (this.promoting === 'none') from = null
|
|
462
|
+
else promotion = this.promoting;
|
|
463
|
+
|
|
464
|
+
this.promoting = false;
|
|
465
|
+
this.allSquares("removePromotion");
|
|
466
|
+
this.allSquares("removeCover");
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (!from) {
|
|
470
|
+
|
|
471
|
+
if (this.canMove(square)) {
|
|
472
|
+
square.select();
|
|
473
|
+
this.hintMoves(square);
|
|
474
|
+
this.clicked = square;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
return false;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (!this.canMove(from)) return false;
|
|
481
|
+
|
|
482
|
+
let move = new Move(from, square, promotion);
|
|
483
|
+
|
|
484
|
+
move.from.deselect();
|
|
485
|
+
this.allSquares("removeHint");
|
|
486
|
+
|
|
487
|
+
if (this.config.onlyLegalMoves && !move.isLegal(this.game)) return false;
|
|
488
|
+
|
|
489
|
+
if (!move.hasPromotion() && this.promote(move)) return false;
|
|
490
|
+
|
|
491
|
+
if (this.config.onMove(move)) {
|
|
492
|
+
this.move(move, animation);
|
|
493
|
+
return true;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
return false;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// Hint
|
|
500
|
+
|
|
501
|
+
hint(square) {
|
|
502
|
+
if (!this.config.hints || !this.squares[square]) return;
|
|
503
|
+
this.squares[square].putHint(this.colorPiece(square) && this.colorPiece(square) !== this.turn());
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
hintMoves(square) {
|
|
507
|
+
if (!this.canMove(square)) return;
|
|
508
|
+
let mosse = this.game.moves({ square: square.id, verbose: true });
|
|
509
|
+
for (let mossa of mosse) {
|
|
510
|
+
if (mossa['to'].length === 2) this.hint(mossa['to']);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
dehintMoves(square) {
|
|
515
|
+
let mosse = this.game.moves({ square: square.id, verbose: true });
|
|
516
|
+
for (let mossa of mosse) {
|
|
517
|
+
let to = this.squares[mossa['to']];
|
|
518
|
+
to.removeHint();
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// Moves
|
|
523
|
+
|
|
524
|
+
canMove(square) {
|
|
525
|
+
if (!square.piece) return false;
|
|
526
|
+
if (this.config.movableColors === 'none') return false;
|
|
527
|
+
if (this.config.movableColors === 'w' && square.piece.color === 'b') return false;
|
|
528
|
+
if (this.config.movableColors === 'b' && square.piece.color === 'w') return false;
|
|
529
|
+
if (!this.config.onlyLegalMoves) return true;
|
|
530
|
+
return square.piece.color == this.turn();
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
convertMove(move) {
|
|
534
|
+
if (move instanceof Move) return move;
|
|
535
|
+
if (typeof move == 'string') {
|
|
536
|
+
let fromId = move.slice(0, 2);
|
|
537
|
+
let toId = move.slice(2, 4);
|
|
538
|
+
let promotion = move.slice(4, 5) ? move.slice(4, 5) : null;
|
|
539
|
+
return new Move(this.squares[fromId], this.squares[toId], promotion);
|
|
540
|
+
}
|
|
541
|
+
throw new Error("Invalid move format");
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
allSquares(method) {
|
|
545
|
+
for (const square of Object.values(this.squares)) {
|
|
546
|
+
square[method]();
|
|
547
|
+
this.squares[square.id] = square;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
legalMove(move) {
|
|
552
|
+
let legal_moves = this.legalMoves(move.from.id);
|
|
553
|
+
|
|
554
|
+
for (let i in legal_moves) {
|
|
555
|
+
if (legal_moves[i]['to'] === move.to.id &&
|
|
556
|
+
move.promotion == legal_moves[i]['promotion'])
|
|
557
|
+
return true;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return false;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
legalMoves(from = null, verb = true) {
|
|
564
|
+
if (from) return this.game.moves({ square: from, verbose: verb });
|
|
565
|
+
return this.game.moves({ verbose: verb });
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// Position
|
|
569
|
+
|
|
570
|
+
chageFenTurn(fen, color) {
|
|
571
|
+
let parts = fen.split(' ');
|
|
572
|
+
parts[1] = color;
|
|
573
|
+
return parts.join(' ');
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
changeFenColor(fen) {
|
|
577
|
+
let parts = fen.split(' ');
|
|
578
|
+
parts[1] = parts[1] === 'w' ? 'b' : 'w';
|
|
579
|
+
return parts.join(' ');
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
playerTurn() {
|
|
583
|
+
return this.getOrientation() == this.game.turn()
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
isWhiteOriented() { return this.config.orientation === 'w' }
|
|
587
|
+
|
|
588
|
+
// Squares
|
|
589
|
+
|
|
590
|
+
getSquareID(row, col) {
|
|
591
|
+
row = parseInt(row);
|
|
592
|
+
col = parseInt(col);
|
|
593
|
+
if (this.isWhiteOriented()) {
|
|
594
|
+
row = 8 - row;
|
|
595
|
+
col = col + 1;
|
|
596
|
+
} else {
|
|
597
|
+
row = row + 1;
|
|
598
|
+
col = 8 - col;
|
|
599
|
+
}
|
|
600
|
+
let letters = 'abcdefgh';
|
|
601
|
+
let letter = letters[col - 1];
|
|
602
|
+
return letter + row;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
removeSquares() {
|
|
606
|
+
for (const square of Object.values(this.squares)) {
|
|
607
|
+
this.board.removeChild(square.element);
|
|
608
|
+
square.destroy();
|
|
609
|
+
|
|
610
|
+
}
|
|
611
|
+
this.squares = {};
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
promote(move) {
|
|
615
|
+
|
|
616
|
+
if (!this.config.onlyLegalMoves) return false;
|
|
617
|
+
|
|
618
|
+
let to = move.to;
|
|
619
|
+
let from = move.from;
|
|
620
|
+
let pezzo = this.game.get(from.id);
|
|
621
|
+
let choichable = ['q', 'r', 'b', 'n']
|
|
622
|
+
|
|
623
|
+
if (pezzo['type'] !== 'p' || !(to.row === 1 || to.row === 8)) return false;
|
|
624
|
+
|
|
625
|
+
for (const square of Object.values(this.squares)) {
|
|
626
|
+
let distance = Math.abs(to.row - square.row);
|
|
627
|
+
|
|
628
|
+
if (to.col === square.col && distance <= 3) {
|
|
629
|
+
|
|
630
|
+
let pieceId = choichable[distance] + pezzo['color'];
|
|
631
|
+
|
|
632
|
+
square.putPromotion(
|
|
633
|
+
this.getPiecePath(pieceId),
|
|
634
|
+
() => {
|
|
635
|
+
this.promoting = pieceId[0]
|
|
636
|
+
this.clicked = from;
|
|
637
|
+
this.onClick(to);
|
|
638
|
+
}
|
|
639
|
+
);
|
|
640
|
+
} else
|
|
641
|
+
square.putCover(
|
|
642
|
+
() => {
|
|
643
|
+
this.promoting = 'none';
|
|
644
|
+
this.onClick(square);
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
this.clicked = from.id;
|
|
649
|
+
|
|
650
|
+
return true;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
transitionTimingFunction(elapsed, duration, type = 'ease') {
|
|
654
|
+
let x = elapsed / duration;
|
|
655
|
+
switch (type) {
|
|
656
|
+
case 'linear':
|
|
657
|
+
return x;
|
|
658
|
+
case 'ease':
|
|
659
|
+
return (x ** 2) * (3 - 2 * x);
|
|
660
|
+
case 'ease-in':
|
|
661
|
+
return x ** 2;
|
|
662
|
+
case 'ease-out':
|
|
663
|
+
return -1 * (x - 1) ** 2 + 1;
|
|
664
|
+
case 'ease-in-out':
|
|
665
|
+
return (x < 0.5) ? 2 * x ** 2 : 4 * x - 2 * x ** 2 - 1;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// user
|
|
670
|
+
|
|
671
|
+
turn() {
|
|
672
|
+
return this.game.turn();
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
getOrientation() {
|
|
676
|
+
return this.config.orientation;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
fen() {
|
|
680
|
+
return this.game.fen();
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
lastMove() {
|
|
684
|
+
return this.movesHistory[this.movesHistory.length - 1];
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
get history() {
|
|
688
|
+
return this.movesHistory;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
history() {
|
|
692
|
+
return this.movesHistory;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
get(square) {
|
|
696
|
+
square = this.convertSquare(square);
|
|
697
|
+
square.check();
|
|
698
|
+
let piece = square.piece;
|
|
699
|
+
return piece ? piece.id : null;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
position(position, color = null) {
|
|
703
|
+
this.allSquares('removeHint');
|
|
704
|
+
this.allSquares("deselect");
|
|
705
|
+
this.allSquares("unmoved");
|
|
706
|
+
if (color && color !== this.config.orientation) {
|
|
707
|
+
position = this.changeFenColor(position);
|
|
708
|
+
this.config.orientation = color;
|
|
709
|
+
this.destroy();
|
|
710
|
+
this.init();
|
|
711
|
+
} else {
|
|
712
|
+
this.buildGame(this.config.position);
|
|
713
|
+
this.updateBoardPieces();
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
flip() {
|
|
718
|
+
let position = this.game.fen();
|
|
719
|
+
this.position(position, this.config.orientation === 'w' ? 'b' : 'w');
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
build() {
|
|
723
|
+
if (this.board) this.destroy();
|
|
724
|
+
this.init();
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
move(move, animation) {
|
|
728
|
+
move = this.convertMove(move);
|
|
729
|
+
move.check();
|
|
730
|
+
|
|
731
|
+
let from = move.from;
|
|
732
|
+
let to = move.to;
|
|
733
|
+
|
|
734
|
+
if (!this.config.onlyLegalMoves) {
|
|
735
|
+
let piece = this.piece(from.id);
|
|
736
|
+
this.game.remove(from.id);
|
|
737
|
+
this.game.remove(to.id);
|
|
738
|
+
this.game.put({ type: move.hasPromotion() ? move.promotion : piece[0], color: piece[1] }, to.id);
|
|
739
|
+
this.updateBoardPieces(animation);
|
|
740
|
+
} else {
|
|
741
|
+
this.allSquares("unmoved");
|
|
742
|
+
|
|
743
|
+
move = this.game.move({
|
|
744
|
+
from: from.id,
|
|
745
|
+
to: to.id,
|
|
746
|
+
promotion: move.hasPromotion() ? move.promotion : undefined
|
|
747
|
+
});
|
|
748
|
+
|
|
749
|
+
if (move === null) {
|
|
750
|
+
throw new Error("Invalid move: move could not be executed");
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
this.movesHistory.push(move);
|
|
754
|
+
|
|
755
|
+
this.updateBoardPieces(animation);
|
|
756
|
+
|
|
757
|
+
from.moved();
|
|
758
|
+
to.moved();
|
|
759
|
+
this.allSquares("removeHint");
|
|
760
|
+
|
|
761
|
+
this.config.onMoveEnd(move);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
clear(animation = true) {
|
|
766
|
+
this.game.clear();
|
|
767
|
+
this.updateBoardPieces(animation);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
insert(square, piece) {
|
|
771
|
+
square = this.convertSquare(square);
|
|
772
|
+
piece = this.convertPiece(piece);
|
|
773
|
+
square.check();
|
|
774
|
+
piece.check();
|
|
775
|
+
if (square.piece) this.remove(square);
|
|
776
|
+
this.game.put({ type: piece.type, color: piece.color }, square.id);
|
|
777
|
+
this.updateBoardPieces();
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
isGameOver() {
|
|
781
|
+
if (this.game.isGameOver()) {
|
|
782
|
+
if (this.game.inCheck()) return this.game.turn() === 'w' ? 'b' : 'w';
|
|
783
|
+
return 'd';
|
|
784
|
+
}
|
|
785
|
+
return null;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
orientation(color) {
|
|
789
|
+
if ((color === 'w' || color === 'b') && color !== this.config.orientation) this.flip();
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
resize(value) {
|
|
793
|
+
if (value === 'auto') {
|
|
794
|
+
let size;
|
|
795
|
+
if (this.board.offsetWidth === 0) {
|
|
796
|
+
size = this.board.offsetHeight;
|
|
797
|
+
} else if (this.board.offsetHeight === 0) {
|
|
798
|
+
size = this.board.offsetWidth;
|
|
799
|
+
} else {
|
|
800
|
+
size = Math.min(this.board.offsetWidth, this.board.offsetHeight);
|
|
801
|
+
}
|
|
802
|
+
this.resize(size);
|
|
803
|
+
} else if (typeof value !== 'number') {
|
|
804
|
+
throw new Error(this.error_messages['invalid_value'] + value);
|
|
805
|
+
} else {
|
|
806
|
+
document.documentElement.style.setProperty('--dimBoard', value + 'px');
|
|
807
|
+
this.updateBoardPieces();
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
destroy() {
|
|
812
|
+
this.removeSquares();
|
|
813
|
+
this.removeBoard();
|
|
814
|
+
this.game = null;
|
|
815
|
+
this.clicked = null;
|
|
816
|
+
this.movesHistory = [];
|
|
817
|
+
this.mosseIndietro = [];
|
|
818
|
+
this.clicked = null;
|
|
819
|
+
this.board = null;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
remove(square, animation = true) {
|
|
823
|
+
square = this.convertSquare(square);
|
|
824
|
+
square.check();
|
|
825
|
+
this.game.remove(square.id);
|
|
826
|
+
let piece = square.piece;
|
|
827
|
+
this.updateBoardPieces(animation);
|
|
828
|
+
return piece;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
piece(square) {
|
|
832
|
+
let piece = this.game.get(square);
|
|
833
|
+
return piece ? piece['type'] + piece['color'] : null;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
highlight(square) {
|
|
837
|
+
square = this.convertSquare(square);
|
|
838
|
+
square.check();
|
|
839
|
+
square.highlight();
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
dehighlight(square) {
|
|
843
|
+
square = this.convertSquare(square);
|
|
844
|
+
square.check();
|
|
845
|
+
square.dehighlight();
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
export default Chessboard;
|