js-chess-engine 1.0.3 → 2.0.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.
- package/README.md +586 -205
- package/dist/adapters/APIAdapter.d.ts +88 -0
- package/dist/adapters/APIAdapter.d.ts.map +1 -0
- package/dist/adapters/APIAdapter.js +225 -0
- package/dist/adapters/APIAdapter.js.map +1 -0
- package/dist/ai/AIEngine.d.ts +42 -0
- package/dist/ai/AIEngine.d.ts.map +1 -0
- package/dist/ai/AIEngine.js +62 -0
- package/dist/ai/AIEngine.js.map +1 -0
- package/dist/ai/Evaluator.d.ts +48 -0
- package/dist/ai/Evaluator.d.ts.map +1 -0
- package/dist/ai/Evaluator.js +248 -0
- package/dist/ai/Evaluator.js.map +1 -0
- package/dist/ai/MoveOrdering.d.ts +60 -0
- package/dist/ai/MoveOrdering.d.ts.map +1 -0
- package/dist/ai/MoveOrdering.js +173 -0
- package/dist/ai/MoveOrdering.js.map +1 -0
- package/dist/ai/Search.d.ts +62 -0
- package/dist/ai/Search.d.ts.map +1 -0
- package/dist/ai/Search.js +340 -0
- package/dist/ai/Search.js.map +1 -0
- package/dist/ai/TranspositionTable.d.ts +100 -0
- package/dist/ai/TranspositionTable.d.ts.map +1 -0
- package/dist/ai/TranspositionTable.js +176 -0
- package/dist/ai/TranspositionTable.js.map +1 -0
- package/dist/core/AttackDetector.d.ts +52 -0
- package/dist/core/AttackDetector.d.ts.map +1 -0
- package/dist/core/AttackDetector.js +397 -0
- package/dist/core/AttackDetector.js.map +1 -0
- package/dist/core/Board.d.ts +109 -0
- package/dist/core/Board.d.ts.map +1 -0
- package/dist/core/Board.js +410 -0
- package/dist/core/Board.js.map +1 -0
- package/dist/core/MoveGenerator.d.ts +48 -0
- package/dist/core/MoveGenerator.d.ts.map +1 -0
- package/dist/core/MoveGenerator.js +678 -0
- package/dist/core/MoveGenerator.js.map +1 -0
- package/dist/core/Position.d.ts +135 -0
- package/dist/core/Position.d.ts.map +1 -0
- package/dist/core/Position.js +351 -0
- package/dist/core/Position.js.map +1 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +25 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/zobrist.d.ts +93 -0
- package/dist/core/zobrist.d.ts.map +1 -0
- package/dist/core/zobrist.js +273 -0
- package/dist/core/zobrist.js.map +1 -0
- package/dist/index.d.ts +154 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +353 -0
- package/dist/index.js.map +1 -0
- package/dist/types/ai.types.d.ts +97 -0
- package/dist/types/ai.types.d.ts.map +1 -0
- package/dist/types/ai.types.js +17 -0
- package/dist/types/ai.types.js.map +1 -0
- package/dist/types/board.types.d.ts +140 -0
- package/dist/types/board.types.d.ts.map +1 -0
- package/dist/types/board.types.js +34 -0
- package/dist/types/board.types.js.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +26 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/move.types.d.ts +70 -0
- package/dist/types/move.types.d.ts.map +1 -0
- package/dist/types/move.types.js +53 -0
- package/dist/types/move.types.js.map +1 -0
- package/dist/utils/constants.d.ts +123 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +259 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/conversion.d.ts +152 -0
- package/dist/utils/conversion.d.ts.map +1 -0
- package/dist/utils/conversion.js +288 -0
- package/dist/utils/conversion.js.map +1 -0
- package/dist/utils/environment.d.ts +33 -0
- package/dist/utils/environment.d.ts.map +1 -0
- package/dist/utils/environment.js +71 -0
- package/dist/utils/environment.js.map +1 -0
- package/dist/utils/fen.d.ts +28 -0
- package/dist/utils/fen.d.ts.map +1 -0
- package/dist/utils/fen.js +203 -0
- package/dist/utils/fen.js.map +1 -0
- package/package.json +29 -30
- package/.eslintrc.json +0 -16
- package/.github/workflows/main.yml +0 -20
- package/.nvmrc +0 -1
- package/CHANGELOG.md +0 -597
- package/dist/js-chess-engine.js +0 -1
- package/example/aiMatch.mjs +0 -21
- package/example/console.mjs +0 -37
- package/example/server.mjs +0 -27
- package/lib/Board.mjs +0 -946
- package/lib/const/board.mjs +0 -838
- package/lib/js-chess-engine.mjs +0 -99
- package/lib/utils.mjs +0 -154
- package/test/.eslintrc.json +0 -11
- package/test/ai.test.mjs +0 -132
- package/test/badge.svg +0 -1
- package/test/importExport.mjs +0 -108
- package/test/moves.test.mjs +0 -798
- package/webpack.config.js +0 -12
package/test/moves.test.mjs
DELETED
|
@@ -1,798 +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: true,
|
|
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
|
-
it('should properly track check property through multiple moves', function () {
|
|
636
|
-
const game = new Game({
|
|
637
|
-
turn: 'white',
|
|
638
|
-
pieces: {
|
|
639
|
-
E1: 'K',
|
|
640
|
-
C3: 'R',
|
|
641
|
-
E8: 'k',
|
|
642
|
-
},
|
|
643
|
-
})
|
|
644
|
-
|
|
645
|
-
game.move('C3', 'E3')
|
|
646
|
-
game.move('E8', 'F8')
|
|
647
|
-
game.move('E3', 'F3')
|
|
648
|
-
|
|
649
|
-
const history = game.getHistory()
|
|
650
|
-
|
|
651
|
-
expect(history).to.have.length(3)
|
|
652
|
-
// eslint-disable-next-line no-unused-expressions
|
|
653
|
-
expect(history[0].configuration.check).to.be.false
|
|
654
|
-
// eslint-disable-next-line no-unused-expressions
|
|
655
|
-
expect(history[1].configuration.check).to.be.true
|
|
656
|
-
// eslint-disable-next-line no-unused-expressions
|
|
657
|
-
expect(history[2].configuration.check).to.be.false
|
|
658
|
-
})
|
|
659
|
-
})
|
|
660
|
-
|
|
661
|
-
describe('Set new piece should', function () {
|
|
662
|
-
it('succeed for valid data', function () {
|
|
663
|
-
const expectedConfiguration = {
|
|
664
|
-
castling: {
|
|
665
|
-
blackLong: true,
|
|
666
|
-
blackShort: true,
|
|
667
|
-
whiteLong: true,
|
|
668
|
-
whiteShort: true,
|
|
669
|
-
},
|
|
670
|
-
check: false,
|
|
671
|
-
checkMate: false,
|
|
672
|
-
enPassant: null,
|
|
673
|
-
fullMove: 1,
|
|
674
|
-
halfMove: 0,
|
|
675
|
-
isFinished: false,
|
|
676
|
-
pieces: {
|
|
677
|
-
E1: 'K',
|
|
678
|
-
E8: 'k',
|
|
679
|
-
A3: 'Q',
|
|
680
|
-
B3: 'R',
|
|
681
|
-
C3: 'B',
|
|
682
|
-
D3: 'N',
|
|
683
|
-
E3: 'P',
|
|
684
|
-
A4: 'q',
|
|
685
|
-
B4: 'r',
|
|
686
|
-
C4: 'b',
|
|
687
|
-
D4: 'n',
|
|
688
|
-
E4: 'p',
|
|
689
|
-
|
|
690
|
-
},
|
|
691
|
-
turn: 'white',
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
const game = new Game({
|
|
695
|
-
turn: 'white',
|
|
696
|
-
pieces: {
|
|
697
|
-
E1: 'K',
|
|
698
|
-
E8: 'k',
|
|
699
|
-
},
|
|
700
|
-
})
|
|
701
|
-
|
|
702
|
-
game.setPiece('A3', 'Q')
|
|
703
|
-
game.setPiece('B3', 'R')
|
|
704
|
-
game.setPiece('C3', 'B')
|
|
705
|
-
game.setPiece('D3', 'N')
|
|
706
|
-
game.setPiece('e3', 'P')
|
|
707
|
-
|
|
708
|
-
game.setPiece('A4', 'q')
|
|
709
|
-
game.setPiece('B4', 'r')
|
|
710
|
-
game.setPiece('c4', 'b')
|
|
711
|
-
game.setPiece('D4', 'n')
|
|
712
|
-
game.setPiece('E4', 'p')
|
|
713
|
-
|
|
714
|
-
expect(game.board.configuration).to.be.deep.equal(expectedConfiguration)
|
|
715
|
-
})
|
|
716
|
-
|
|
717
|
-
it('fail for invalid location', function () {
|
|
718
|
-
const game = new Game({
|
|
719
|
-
turn: 'white',
|
|
720
|
-
pieces: {
|
|
721
|
-
E1: 'K',
|
|
722
|
-
E8: 'k',
|
|
723
|
-
},
|
|
724
|
-
})
|
|
725
|
-
|
|
726
|
-
expect(() => {
|
|
727
|
-
game.setPiece('A9', 'Q')
|
|
728
|
-
}).to.throw(/Invalid location/)
|
|
729
|
-
})
|
|
730
|
-
|
|
731
|
-
it('fail for invalid piece', function () {
|
|
732
|
-
const game = new Game({
|
|
733
|
-
turn: 'white',
|
|
734
|
-
pieces: {
|
|
735
|
-
E1: 'K',
|
|
736
|
-
E8: 'k',
|
|
737
|
-
},
|
|
738
|
-
})
|
|
739
|
-
|
|
740
|
-
expect(() => {
|
|
741
|
-
game.setPiece('A8', 'F')
|
|
742
|
-
}).to.throw(/Invalid piece/)
|
|
743
|
-
})
|
|
744
|
-
})
|
|
745
|
-
|
|
746
|
-
describe('Delete piece should', function () {
|
|
747
|
-
it('succeed for valid data', function () {
|
|
748
|
-
const expectedConfiguration = {
|
|
749
|
-
castling: {
|
|
750
|
-
blackLong: true,
|
|
751
|
-
blackShort: true,
|
|
752
|
-
whiteLong: true,
|
|
753
|
-
whiteShort: true,
|
|
754
|
-
},
|
|
755
|
-
check: false,
|
|
756
|
-
checkMate: false,
|
|
757
|
-
enPassant: null,
|
|
758
|
-
fullMove: 1,
|
|
759
|
-
halfMove: 0,
|
|
760
|
-
isFinished: false,
|
|
761
|
-
pieces: {
|
|
762
|
-
E1: 'K',
|
|
763
|
-
E8: 'k',
|
|
764
|
-
},
|
|
765
|
-
turn: 'white',
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
const game = new Game({
|
|
769
|
-
turn: 'white',
|
|
770
|
-
pieces: {
|
|
771
|
-
E1: 'K',
|
|
772
|
-
E8: 'k',
|
|
773
|
-
D7: 'q',
|
|
774
|
-
D6: 'p',
|
|
775
|
-
},
|
|
776
|
-
})
|
|
777
|
-
|
|
778
|
-
game.removePiece('D7')
|
|
779
|
-
game.removePiece('d6')
|
|
780
|
-
|
|
781
|
-
expect(game.board.configuration).to.be.deep.equal(expectedConfiguration)
|
|
782
|
-
})
|
|
783
|
-
|
|
784
|
-
it('fail for invalid location', function () {
|
|
785
|
-
const game = new Game({
|
|
786
|
-
turn: 'white',
|
|
787
|
-
pieces: {
|
|
788
|
-
E1: 'K',
|
|
789
|
-
E8: 'k',
|
|
790
|
-
D7: 'q',
|
|
791
|
-
},
|
|
792
|
-
})
|
|
793
|
-
|
|
794
|
-
expect(() => {
|
|
795
|
-
game.removePiece('A9')
|
|
796
|
-
}).to.throw(/Invalid location/)
|
|
797
|
-
})
|
|
798
|
-
})
|