js-chess-engine 1.0.2 → 2.0.1-rc.0

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.
Files changed (103) hide show
  1. package/README.md +586 -205
  2. package/dist/adapters/APIAdapter.d.ts +88 -0
  3. package/dist/adapters/APIAdapter.d.ts.map +1 -0
  4. package/dist/adapters/APIAdapter.js +225 -0
  5. package/dist/adapters/APIAdapter.js.map +1 -0
  6. package/dist/ai/AIEngine.d.ts +42 -0
  7. package/dist/ai/AIEngine.d.ts.map +1 -0
  8. package/dist/ai/AIEngine.js +62 -0
  9. package/dist/ai/AIEngine.js.map +1 -0
  10. package/dist/ai/Evaluator.d.ts +48 -0
  11. package/dist/ai/Evaluator.d.ts.map +1 -0
  12. package/dist/ai/Evaluator.js +248 -0
  13. package/dist/ai/Evaluator.js.map +1 -0
  14. package/dist/ai/MoveOrdering.d.ts +60 -0
  15. package/dist/ai/MoveOrdering.d.ts.map +1 -0
  16. package/dist/ai/MoveOrdering.js +173 -0
  17. package/dist/ai/MoveOrdering.js.map +1 -0
  18. package/dist/ai/Search.d.ts +62 -0
  19. package/dist/ai/Search.d.ts.map +1 -0
  20. package/dist/ai/Search.js +340 -0
  21. package/dist/ai/Search.js.map +1 -0
  22. package/dist/ai/TranspositionTable.d.ts +100 -0
  23. package/dist/ai/TranspositionTable.d.ts.map +1 -0
  24. package/dist/ai/TranspositionTable.js +176 -0
  25. package/dist/ai/TranspositionTable.js.map +1 -0
  26. package/dist/core/AttackDetector.d.ts +52 -0
  27. package/dist/core/AttackDetector.d.ts.map +1 -0
  28. package/dist/core/AttackDetector.js +387 -0
  29. package/dist/core/AttackDetector.js.map +1 -0
  30. package/dist/core/Board.d.ts +109 -0
  31. package/dist/core/Board.d.ts.map +1 -0
  32. package/dist/core/Board.js +410 -0
  33. package/dist/core/Board.js.map +1 -0
  34. package/dist/core/MoveGenerator.d.ts +48 -0
  35. package/dist/core/MoveGenerator.d.ts.map +1 -0
  36. package/dist/core/MoveGenerator.js +678 -0
  37. package/dist/core/MoveGenerator.js.map +1 -0
  38. package/dist/core/Position.d.ts +135 -0
  39. package/dist/core/Position.d.ts.map +1 -0
  40. package/dist/core/Position.js +351 -0
  41. package/dist/core/Position.js.map +1 -0
  42. package/dist/core/index.d.ts +9 -0
  43. package/dist/core/index.d.ts.map +1 -0
  44. package/dist/core/index.js +25 -0
  45. package/dist/core/index.js.map +1 -0
  46. package/dist/core/zobrist.d.ts +93 -0
  47. package/dist/core/zobrist.d.ts.map +1 -0
  48. package/dist/core/zobrist.js +273 -0
  49. package/dist/core/zobrist.js.map +1 -0
  50. package/dist/index.d.ts +154 -0
  51. package/dist/index.d.ts.map +1 -0
  52. package/dist/index.js +353 -0
  53. package/dist/index.js.map +1 -0
  54. package/dist/types/ai.types.d.ts +97 -0
  55. package/dist/types/ai.types.d.ts.map +1 -0
  56. package/dist/types/ai.types.js +17 -0
  57. package/dist/types/ai.types.js.map +1 -0
  58. package/dist/types/board.types.d.ts +140 -0
  59. package/dist/types/board.types.d.ts.map +1 -0
  60. package/dist/types/board.types.js +34 -0
  61. package/dist/types/board.types.js.map +1 -0
  62. package/dist/types/index.d.ts +7 -0
  63. package/dist/types/index.d.ts.map +1 -0
  64. package/dist/types/index.js +26 -0
  65. package/dist/types/index.js.map +1 -0
  66. package/dist/types/move.types.d.ts +70 -0
  67. package/dist/types/move.types.d.ts.map +1 -0
  68. package/dist/types/move.types.js +53 -0
  69. package/dist/types/move.types.js.map +1 -0
  70. package/dist/utils/constants.d.ts +123 -0
  71. package/dist/utils/constants.d.ts.map +1 -0
  72. package/dist/utils/constants.js +259 -0
  73. package/dist/utils/constants.js.map +1 -0
  74. package/dist/utils/conversion.d.ts +152 -0
  75. package/dist/utils/conversion.d.ts.map +1 -0
  76. package/dist/utils/conversion.js +288 -0
  77. package/dist/utils/conversion.js.map +1 -0
  78. package/dist/utils/environment.d.ts +33 -0
  79. package/dist/utils/environment.d.ts.map +1 -0
  80. package/dist/utils/environment.js +71 -0
  81. package/dist/utils/environment.js.map +1 -0
  82. package/dist/utils/fen.d.ts +28 -0
  83. package/dist/utils/fen.d.ts.map +1 -0
  84. package/dist/utils/fen.js +203 -0
  85. package/dist/utils/fen.js.map +1 -0
  86. package/package.json +27 -28
  87. package/.eslintrc.json +0 -16
  88. package/.github/workflows/main.yml +0 -20
  89. package/CHANGELOG.md +0 -587
  90. package/dist/js-chess-engine.js +0 -1
  91. package/example/aiMatch.mjs +0 -21
  92. package/example/console.mjs +0 -37
  93. package/example/server.mjs +0 -27
  94. package/lib/Board.mjs +0 -943
  95. package/lib/const/board.mjs +0 -838
  96. package/lib/js-chess-engine.mjs +0 -99
  97. package/lib/utils.mjs +0 -154
  98. package/test/.eslintrc.json +0 -11
  99. package/test/ai.test.mjs +0 -132
  100. package/test/badge.svg +0 -1
  101. package/test/importExport.mjs +0 -108
  102. package/test/moves.test.mjs +0 -773
  103. package/webpack.config.js +0 -12
@@ -1,773 +0,0 @@
1
- import chai from 'chai'
2
- import { Game } from '../lib/js-chess-engine.mjs'
3
- import { COLORS } from '../lib/const/board.mjs'
4
-
5
- const expect = chai.expect
6
-
7
- describe('Should properly calculate possible moves', function () {
8
- it('For rook', function () {
9
- const game = new Game({
10
- pieces: {
11
- B8: 'R',
12
- E8: 'k',
13
- },
14
- })
15
- const expectedMoves = {
16
- B8: ['B7', 'B6', 'B5', 'B4', 'B3', 'B2', 'B1', 'C8', 'D8', 'E8', 'A8'],
17
- }
18
- expect(game.moves()).to.deep.equal(expectedMoves)
19
- })
20
-
21
- it('For rook in corner', function () {
22
- const game = new Game({
23
- pieces: {
24
- H8: 'R',
25
- F7: 'k',
26
- H7: 'p',
27
- },
28
- })
29
- const expectedMoves = {
30
- H8: ['H7', 'G8', 'F8', 'E8', 'D8', 'C8', 'B8', 'A8'],
31
- }
32
- expect(game.moves()).to.deep.equal(expectedMoves)
33
- })
34
-
35
- it('New game when white is on move', function () {
36
- const game = new Game()
37
- const expectedMoves = {
38
- B1: ['C3', 'A3'],
39
- G1: ['H3', 'F3'],
40
- A2: ['A3', 'A4'],
41
- B2: ['B3', 'B4'],
42
- C2: ['C3', 'C4'],
43
- D2: ['D3', 'D4'],
44
- E2: ['E3', 'E4'],
45
- F2: ['F3', 'F4'],
46
- G2: ['G3', 'G4'],
47
- H2: ['H3', 'H4'],
48
- }
49
- expect(game.moves()).to.deep.equal(expectedMoves)
50
- })
51
-
52
- it('White can do a long castling', function () {
53
- const game = new Game({
54
- pieces: {
55
- E1: 'K',
56
- A1: 'R',
57
- E8: 'k',
58
- },
59
- })
60
-
61
- const expectedMovesCastle = ['A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'B1', 'C1', 'D1']
62
- const expectedMovesKing = ['E2', 'F1', 'D1', 'D2', 'F2', 'C1']
63
-
64
- expect(game.moves('E1')).to.have.members(expectedMovesKing)
65
- expect(game.moves('A1')).to.have.members(expectedMovesCastle)
66
- })
67
-
68
- it('White can do a short castling', function () {
69
- const game = new Game({
70
- pieces: {
71
- E1: 'K',
72
- H1: 'R',
73
- E8: 'k',
74
- },
75
- })
76
-
77
- const expectedMoves = {
78
- E1: ['E2', 'F1', 'D1', 'D2', 'F2', 'G1'],
79
- H1: ['H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'G1', 'F1'],
80
- }
81
-
82
- expect(game.moves()).to.deep.equal(expectedMoves)
83
- })
84
-
85
- it('White can`t do a long castling when his King was moved', function () {
86
- const game = new Game({
87
- pieces: {
88
- E1: 'K',
89
- A1: 'R',
90
- E8: 'k',
91
- },
92
- })
93
-
94
- const expectedMoves = {
95
- E1: ['E2', 'F1', 'D1', 'D2', 'F2'],
96
- A1: ['A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'B1', 'C1', 'D1'],
97
- }
98
- game.move('E1', 'D1')
99
- game.move('E8', 'E7')
100
- game.move('D1', 'E1')
101
- game.move('E7', 'E6')
102
-
103
- expect(game.moves()).to.deep.equal(expectedMoves)
104
- })
105
-
106
- it('White can`t do a short castling when his King was moved', function () {
107
- const game = new Game({
108
- pieces: {
109
- E1: 'K',
110
- H1: 'R',
111
- E8: 'k',
112
- },
113
- })
114
-
115
- const expectedMoves = {
116
- E1: ['E2', 'F1', 'D1', 'D2', 'F2'],
117
- H1: ['H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'G1', 'F1'],
118
- }
119
- game.move('E1', 'D1')
120
- game.move('E8', 'E7')
121
- game.move('D1', 'E1')
122
- game.move('E7', 'E6')
123
-
124
- expect(game.moves()).to.deep.equal(expectedMoves)
125
- })
126
-
127
- it('White can`t do castling when is in check', function () {
128
- const game = new Game({
129
- pieces: {
130
- E1: 'K',
131
- E2: 'P',
132
- H1: 'R',
133
- H4: 'b',
134
- E8: 'k',
135
- },
136
- })
137
-
138
- const expectedMoves = ['F1', 'D1', 'D2']
139
- expect(game.moves('E1')).to.have.members(expectedMoves)
140
- })
141
-
142
- it('White can`t do castling when jumped field is under attack', function () {
143
- const game = new Game({
144
- pieces: {
145
- E1: 'K',
146
- E2: 'P',
147
- H1: 'R',
148
- F4: 'r',
149
- E8: 'k',
150
- },
151
- })
152
- const expectedMoves = ['D1', 'D2']
153
- expect(game.moves('E1')).to.have.members(expectedMoves)
154
- })
155
-
156
- it('black can do castling after white castling (https://github.com/josefjadrny/js-chess-engine/issues/9)', function () {
157
- const expectedMoves = ['E7', 'D7', 'G8', 'F8']
158
-
159
- const game = new Game({
160
- pieces: {
161
- E1: 'K',
162
- H1: 'R',
163
- H2: 'P',
164
- G2: 'P',
165
- F2: 'P',
166
- H7: 'p',
167
- G7: 'p',
168
- F7: 'p',
169
- E8: 'k',
170
- D8: 'q',
171
- H8: 'r',
172
- },
173
- })
174
- game.move('E1', 'G1')
175
- expect(game.moves('E8')).to.have.members(expectedMoves)
176
- })
177
-
178
- it('black can not do castling over attacked fields', function () {
179
- const expectedMoves = ['E7', 'D7', 'D8']
180
-
181
- const game = new Game({
182
- pieces: {
183
- E1: 'K',
184
- H1: 'R',
185
- H2: 'P',
186
- H7: 'p',
187
- E8: 'k',
188
- H8: 'r',
189
- },
190
- })
191
- game.move('E1', 'G1')
192
- expect(game.moves('E8')).to.have.members(expectedMoves)
193
- })
194
-
195
- it('White can do a en Passant', function () {
196
- const expectedMoves = ['C6', 'B6']
197
-
198
- const game = new Game()
199
- game.move('C2', 'C4')
200
- game.move('A7', 'A5')
201
- game.move('C4', 'C5')
202
- game.move('B7', 'B5')
203
-
204
- expect(game.moves('C5')).to.deep.equal(expectedMoves)
205
-
206
- game.move('C5', 'B6')
207
-
208
- expect(game.board.getPiece('B5')).to.be.a('undefined')
209
- })
210
-
211
- it('White can do a en Passant from FEN (#11)', function () {
212
- const expectedMoves = ['D6']
213
- const game = new Game('rn1qk2r/pbp1nppp/1p2p3/3pP3/1bBP4/2N2N2/PPP2PPP/R1BQK2R w KQkq d6 0 7')
214
-
215
- expect(game.moves('E5')).to.deep.equal(expectedMoves)
216
- })
217
-
218
- it('White cannot do a en Passant later', function () {
219
- const expectedMoves = ['C6']
220
-
221
- const game = new Game()
222
- game.move('C2', 'C4')
223
- game.move('A7', 'A5')
224
- game.move('C4', 'C5')
225
- game.move('B7', 'B5')
226
- game.move('A2', 'A4')
227
- game.move('H7', 'H6')
228
-
229
- expect(game.moves('C5')).to.deep.equal(expectedMoves)
230
- })
231
-
232
- it('Black can do a en Passant', function () {
233
- const expectedMoves = ['A3', 'B3']
234
-
235
- const game = new Game()
236
- game.move('C2', 'C3')
237
- game.move('A7', 'A5')
238
- game.move('C3', 'C4')
239
- game.move('A5', 'A4')
240
- game.move('B2', 'B4')
241
-
242
- expect(game.moves('A4')).to.deep.equal(expectedMoves)
243
-
244
- game.move('A4', 'B3')
245
-
246
- expect(game.board.getPiece('B4')).to.be.a('undefined')
247
- })
248
- })
249
-
250
- describe('Should properly calculate score', function () {
251
- it('Black player score is -128', function () {
252
- const game = new Game({
253
- turn: 'black',
254
- pieces: {
255
- // There is a piece value multiplier = 10
256
- E1: 'K', // 10
257
- C6: 'R', // 9
258
- H1: 'R', // 5
259
- B1: 'N', // 3
260
- E8: 'k', // 10
261
- E5: 'p', // 1
262
- D5: 'b', // 3
263
- },
264
- })
265
-
266
- expect(game.board.calculateScore()).to.be.equal(-90)
267
- })
268
- it('White player score is +129', function () {
269
- const game = new Game({
270
- turn: 'white',
271
- pieces: {
272
- // There is a piece value multiplier = 10
273
- E1: 'K', // 10
274
- c6: 'Q', // 9
275
- H1: 'R', // 5
276
- B1: 'N', // 3
277
- E8: 'k', // 10
278
- E5: 'p', // 1
279
- D5: 'b', // 3
280
- },
281
- })
282
- expect(game.board.calculateScore()).to.be.equal(130)
283
- })
284
- it('Only piece values for new game', function () {
285
- const game = new Game()
286
-
287
- expect(game.board.getIngamePiecesValue()).to.be.equal(98)
288
- })
289
- })
290
-
291
- describe('Should properly calculate score by pieces locations', function () {
292
- before(function () {
293
- this.multiplier = 0.5
294
- })
295
-
296
- it('for pawns', function () {
297
- const game = new Game({
298
- pieces: {
299
- A2: 'P', // 0.5
300
- C7: 'P', // 5
301
- H3: 'P', // 0.5
302
- D2: 'P', // -2
303
- A6: 'p', // -0.5
304
- E3: 'p', // -3
305
- },
306
- })
307
-
308
- expect(game.board.calculateScoreByPiecesLocation()).to.be.equal(0.5 * this.multiplier)
309
- })
310
- })
311
-
312
- describe('Should properly calculate check', function () {
313
- it('when Rook is attacking from left', function () {
314
- const game = new Game({
315
- turn: 'black',
316
- pieces: {
317
- E1: 'K',
318
- C8: 'R',
319
- E8: 'k',
320
- },
321
- })
322
-
323
- // eslint-disable-next-line no-unused-expressions
324
- expect(game.exportJson().check).to.be.true
325
- })
326
- it('when Rook is attacking from right', function () {
327
- const game = new Game({
328
- turn: 'black',
329
- pieces: {
330
- E1: 'K',
331
- H8: 'R',
332
- E8: 'k',
333
- },
334
- })
335
-
336
- // eslint-disable-next-line no-unused-expressions
337
- expect(game.exportJson().check).to.be.true
338
- })
339
- it('when Queen is attacking from bottom', function () {
340
- const game = new Game({
341
- turn: 'black',
342
- pieces: {
343
- E1: 'K',
344
- E4: 'Q',
345
- E8: 'k',
346
- },
347
- })
348
-
349
- // eslint-disable-next-line no-unused-expressions
350
- expect(game.exportJson().check).to.be.true
351
- })
352
- it('when Bishop is attacking from left down', function () {
353
- const game = new Game({
354
- turn: 'black',
355
- pieces: {
356
- E1: 'K',
357
- C6: 'B',
358
- E8: 'k',
359
- },
360
- })
361
-
362
- // eslint-disable-next-line no-unused-expressions
363
- expect(game.exportJson().check).to.be.true
364
- })
365
- it('when Queen is attacking from right down', function () {
366
- const game = new Game({
367
- turn: 'black',
368
- pieces: {
369
- E1: 'K',
370
- G6: 'Q',
371
- E8: 'k',
372
- },
373
- })
374
-
375
- // eslint-disable-next-line no-unused-expressions
376
- expect(game.exportJson().check).to.be.true
377
- })
378
- it('when and opponent is in check', function () {
379
- const game = new Game({
380
- turn: 'white',
381
- pieces: {
382
- E1: 'K',
383
- G6: 'Q',
384
- E8: 'k',
385
- },
386
- })
387
-
388
- // eslint-disable-next-line no-unused-expressions
389
- expect(game.exportJson().check).to.be.false
390
- // eslint-disable-next-line no-unused-expressions
391
- expect(game.board.isAttackingKing()).to.be.true
392
- })
393
- it('when Bishop is blocked by pawn', function () {
394
- const game = new Game({
395
- turn: 'black',
396
- pieces: {
397
- E1: 'K',
398
- C6: 'B',
399
- D7: 'p',
400
- E8: 'k',
401
- },
402
- })
403
-
404
- // eslint-disable-next-line no-unused-expressions
405
- expect(game.exportJson().check).to.be.false
406
- })
407
- it('when Pawn is attacking from left', function () {
408
- const game = new Game({
409
- turn: 'black',
410
- pieces: {
411
- E1: 'K',
412
- D7: 'P',
413
- E8: 'k',
414
- },
415
- })
416
-
417
- // eslint-disable-next-line no-unused-expressions
418
- expect(game.exportJson().check).to.be.true
419
- // eslint-disable-next-line no-unused-expressions
420
- expect(game.board.isAttackingKing(COLORS.BLACK)).to.be.false
421
- })
422
- it('when Pawn is attacking from right', function () {
423
- const game = new Game({
424
- turn: 'white',
425
- pieces: {
426
- E1: 'K',
427
- F2: 'p',
428
- E8: 'k',
429
- },
430
- })
431
-
432
- // eslint-disable-next-line no-unused-expressions
433
- expect(game.exportJson().check).to.be.true
434
- // eslint-disable-next-line no-unused-expressions
435
- expect(game.board.isAttackingKing(COLORS.BLACK)).to.be.true
436
- })
437
- it('when Knight is attacking from left', function () {
438
- const game = new Game({
439
- turn: 'white',
440
- pieces: {
441
- E1: 'K',
442
- C2: 'n',
443
- E8: 'k',
444
- },
445
- })
446
-
447
- // eslint-disable-next-line no-unused-expressions
448
- expect(game.exportJson().check).to.be.true
449
- // eslint-disable-next-line no-unused-expressions
450
- expect(game.board.isAttackingKing(COLORS.BLACK)).to.be.true
451
- })
452
- it('when Knight was attacking from left', function () {
453
- const game = new Game({
454
- turn: 'white',
455
- pieces: {
456
- E1: 'K',
457
- C2: 'n',
458
- E8: 'k',
459
- },
460
- })
461
-
462
- // eslint-disable-next-line no-unused-expressions
463
- expect(game.exportJson().check).to.be.true
464
- // eslint-disable-next-line no-unused-expressions
465
- expect(game.board.isAttackingKing(COLORS.BLACK)).to.be.true
466
-
467
- game.move('e1', 'e2')
468
-
469
- // eslint-disable-next-line no-unused-expressions
470
- expect(game.exportJson().check).to.be.false
471
- // eslint-disable-next-line no-unused-expressions
472
- expect(game.board.isAttackingKing(COLORS.BLACK)).to.be.false
473
- })
474
- it('when Knight is attacking from left up', function () {
475
- const game = new Game({
476
- turn: 'white',
477
- pieces: {
478
- E1: 'K',
479
- D3: 'n',
480
- E8: 'k',
481
- },
482
- })
483
-
484
- // eslint-disable-next-line no-unused-expressions
485
- expect(game.exportJson().check).to.be.true
486
- // eslint-disable-next-line no-unused-expressions
487
- expect(game.board.isAttackingKing(COLORS.BLACK)).to.be.true
488
- })
489
- it('when Knight is attacking from right up', function () {
490
- const game = new Game({
491
- turn: 'white',
492
- pieces: {
493
- E1: 'K',
494
- F3: 'n',
495
- E8: 'k',
496
- },
497
- })
498
-
499
- // eslint-disable-next-line no-unused-expressions
500
- expect(game.exportJson().check).to.be.true
501
- // eslint-disable-next-line no-unused-expressions
502
- expect(game.board.isAttackingKing(COLORS.BLACK)).to.be.true
503
- })
504
- it('when Knight is attacking from right', function () {
505
- const game = new Game({
506
- turn: 'white',
507
- pieces: {
508
- E1: 'K',
509
- G2: 'n',
510
- E8: 'k',
511
- },
512
- })
513
-
514
- // eslint-disable-next-line no-unused-expressions
515
- expect(game.exportJson().check).to.be.true
516
- // eslint-disable-next-line no-unused-expressions
517
- expect(game.board.isAttackingKing(COLORS.BLACK)).to.be.true
518
- })
519
- })
520
-
521
- describe('Should properly get history', function () {
522
- it('reversed and not reversed', function () {
523
- const game = new Game({
524
- turn: 'white',
525
- pieces: {
526
- E1: 'K',
527
- G2: 'n',
528
- E8: 'k',
529
- },
530
- })
531
-
532
- const expectedHistory1 = {
533
- from: 'E1',
534
- to: 'E2',
535
- configuration: {
536
- castling: {
537
- blackLong: true,
538
- blackShort: true,
539
- whiteLong: true,
540
- whiteShort: true,
541
- },
542
- check: false,
543
- checkMate: false,
544
- enPassant: null,
545
- fullMove: 1,
546
- halfMove: 0,
547
- isFinished: false,
548
- pieces: {
549
- E1: 'K',
550
- E8: 'k',
551
- G2: 'n',
552
- },
553
- turn: 'white',
554
- },
555
- }
556
-
557
- const expectedHistory2 = {
558
- from: 'E8',
559
- to: 'E7',
560
- configuration: {
561
- castling: {
562
- blackLong: true,
563
- blackShort: true,
564
- whiteLong: false,
565
- whiteShort: false,
566
- },
567
- check: false,
568
- checkMate: false,
569
- enPassant: null,
570
- fullMove: 1,
571
- halfMove: 1,
572
- isFinished: false,
573
- pieces: {
574
- E2: 'K',
575
- E8: 'k',
576
- G2: 'n',
577
- },
578
- turn: 'black',
579
- },
580
- }
581
-
582
- game.move('e1', 'e2')
583
-
584
- expect(game.getHistory()).to.be.deep.equal([expectedHistory1])
585
- expect(game.getHistory(true)).to.be.deep.equal([expectedHistory1])
586
-
587
- game.move('e8', 'e7')
588
-
589
- expect(game.getHistory()).to.be.deep.equal([expectedHistory1, expectedHistory2])
590
- expect(game.getHistory(true)).to.be.deep.equal([expectedHistory2, expectedHistory1])
591
- })
592
-
593
- it('when set and remove pieces were used', function () {
594
- const game = new Game({
595
- turn: 'white',
596
- pieces: {
597
- E1: 'K',
598
- G2: 'n',
599
- E8: 'k',
600
- },
601
- })
602
-
603
- const expectedHistory1 = {
604
- from: 'E1',
605
- to: 'E2',
606
- configuration: {
607
- castling: {
608
- blackLong: true,
609
- blackShort: true,
610
- whiteLong: true,
611
- whiteShort: true,
612
- },
613
- check: false,
614
- checkMate: false,
615
- enPassant: null,
616
- fullMove: 1,
617
- halfMove: 0,
618
- isFinished: false,
619
- pieces: {
620
- E1: 'K',
621
- E8: 'k',
622
- F2: 'P',
623
- },
624
- turn: 'white',
625
- },
626
- }
627
-
628
- game.setPiece('f2', 'P')
629
- game.removePiece('g2')
630
- game.move('e1', 'e2')
631
-
632
- expect(game.getHistory()).to.be.deep.equal([expectedHistory1])
633
- })
634
- })
635
-
636
- describe('Set new piece should', function () {
637
- it('succeed for valid data', function () {
638
- const expectedConfiguration = {
639
- castling: {
640
- blackLong: true,
641
- blackShort: true,
642
- whiteLong: true,
643
- whiteShort: true,
644
- },
645
- check: false,
646
- checkMate: false,
647
- enPassant: null,
648
- fullMove: 1,
649
- halfMove: 0,
650
- isFinished: false,
651
- pieces: {
652
- E1: 'K',
653
- E8: 'k',
654
- A3: 'Q',
655
- B3: 'R',
656
- C3: 'B',
657
- D3: 'N',
658
- E3: 'P',
659
- A4: 'q',
660
- B4: 'r',
661
- C4: 'b',
662
- D4: 'n',
663
- E4: 'p',
664
-
665
- },
666
- turn: 'white',
667
- }
668
-
669
- const game = new Game({
670
- turn: 'white',
671
- pieces: {
672
- E1: 'K',
673
- E8: 'k',
674
- },
675
- })
676
-
677
- game.setPiece('A3', 'Q')
678
- game.setPiece('B3', 'R')
679
- game.setPiece('C3', 'B')
680
- game.setPiece('D3', 'N')
681
- game.setPiece('e3', 'P')
682
-
683
- game.setPiece('A4', 'q')
684
- game.setPiece('B4', 'r')
685
- game.setPiece('c4', 'b')
686
- game.setPiece('D4', 'n')
687
- game.setPiece('E4', 'p')
688
-
689
- expect(game.board.configuration).to.be.deep.equal(expectedConfiguration)
690
- })
691
-
692
- it('fail for invalid location', function () {
693
- const game = new Game({
694
- turn: 'white',
695
- pieces: {
696
- E1: 'K',
697
- E8: 'k',
698
- },
699
- })
700
-
701
- expect(() => {
702
- game.setPiece('A9', 'Q')
703
- }).to.throw(/Invalid location/)
704
- })
705
-
706
- it('fail for invalid piece', function () {
707
- const game = new Game({
708
- turn: 'white',
709
- pieces: {
710
- E1: 'K',
711
- E8: 'k',
712
- },
713
- })
714
-
715
- expect(() => {
716
- game.setPiece('A8', 'F')
717
- }).to.throw(/Invalid piece/)
718
- })
719
- })
720
-
721
- describe('Delete piece should', function () {
722
- it('succeed for valid data', function () {
723
- const expectedConfiguration = {
724
- castling: {
725
- blackLong: true,
726
- blackShort: true,
727
- whiteLong: true,
728
- whiteShort: true,
729
- },
730
- check: false,
731
- checkMate: false,
732
- enPassant: null,
733
- fullMove: 1,
734
- halfMove: 0,
735
- isFinished: false,
736
- pieces: {
737
- E1: 'K',
738
- E8: 'k',
739
- },
740
- turn: 'white',
741
- }
742
-
743
- const game = new Game({
744
- turn: 'white',
745
- pieces: {
746
- E1: 'K',
747
- E8: 'k',
748
- D7: 'q',
749
- D6: 'p',
750
- },
751
- })
752
-
753
- game.removePiece('D7')
754
- game.removePiece('d6')
755
-
756
- expect(game.board.configuration).to.be.deep.equal(expectedConfiguration)
757
- })
758
-
759
- it('fail for invalid location', function () {
760
- const game = new Game({
761
- turn: 'white',
762
- pieces: {
763
- E1: 'K',
764
- E8: 'k',
765
- D7: 'q',
766
- },
767
- })
768
-
769
- expect(() => {
770
- game.removePiece('A9')
771
- }).to.throw(/Invalid location/)
772
- })
773
- })