cm-chess 3.4.3 → 3.5.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/package.json +1 -1
- package/src/Chess.js +25 -18
- package/test/TestChess.js +37 -0
package/package.json
CHANGED
package/src/Chess.js
CHANGED
|
@@ -49,16 +49,22 @@ export class Chess {
|
|
|
49
49
|
|
|
50
50
|
constructor(fenOrProps = FEN.start) {
|
|
51
51
|
this.observers = []
|
|
52
|
+
this.props = {
|
|
53
|
+
fen: undefined, // use a fen or a pgn with setUpFen
|
|
54
|
+
pgn: undefined,
|
|
55
|
+
sloppy: true // sloppy parsing allows small mistakes in SAN
|
|
56
|
+
}
|
|
52
57
|
if (typeof fenOrProps === "string") {
|
|
53
|
-
this.
|
|
58
|
+
this.props.fen = fenOrProps
|
|
59
|
+
} else if (typeof fenOrProps === "object") {
|
|
60
|
+
Object.assign(this.props, fenOrProps)
|
|
61
|
+
}
|
|
62
|
+
if (this.props.fen) {
|
|
63
|
+
this.load(this.props.fen)
|
|
64
|
+
} else if (this.props.pgn) {
|
|
65
|
+
this.loadPgn(this.props.pgn)
|
|
54
66
|
} else {
|
|
55
|
-
|
|
56
|
-
this.load(fenOrProps.fen)
|
|
57
|
-
} else if (fenOrProps.pgn) {
|
|
58
|
-
this.loadPgn(fenOrProps.pgn)
|
|
59
|
-
} else {
|
|
60
|
-
this.load(FEN.start)
|
|
61
|
-
}
|
|
67
|
+
this.load(FEN.start)
|
|
62
68
|
}
|
|
63
69
|
}
|
|
64
70
|
|
|
@@ -96,7 +102,7 @@ export class Chess {
|
|
|
96
102
|
* @returns {boolean} true, if the game is over at that move
|
|
97
103
|
*/
|
|
98
104
|
gameOver(move = this.lastMove()) {
|
|
99
|
-
if(move) {
|
|
105
|
+
if (move) {
|
|
100
106
|
return move.gameOver
|
|
101
107
|
} else {
|
|
102
108
|
return new ChessJs(this.fen()).game_over()
|
|
@@ -108,7 +114,7 @@ export class Chess {
|
|
|
108
114
|
* @returns {boolean} true, if the game is in draw at that move
|
|
109
115
|
*/
|
|
110
116
|
inDraw(move = this.lastMove()) {
|
|
111
|
-
if(move) {
|
|
117
|
+
if (move) {
|
|
112
118
|
return move.inDraw === true
|
|
113
119
|
} else {
|
|
114
120
|
return new ChessJs(this.fen()).in_draw()
|
|
@@ -120,7 +126,7 @@ export class Chess {
|
|
|
120
126
|
* @returns {boolean} true, if the game is in statemate at that move
|
|
121
127
|
*/
|
|
122
128
|
inStalemate(move = this.lastMove()) {
|
|
123
|
-
if(move) {
|
|
129
|
+
if (move) {
|
|
124
130
|
return move.inStalemate === true
|
|
125
131
|
} else {
|
|
126
132
|
return new ChessJs(this.fen()).in_stalemate()
|
|
@@ -132,7 +138,7 @@ export class Chess {
|
|
|
132
138
|
* @returns {boolean} true, if the game is in draw, because of unsufficiant material at that move
|
|
133
139
|
*/
|
|
134
140
|
insufficientMaterial(move = this.lastMove()) {
|
|
135
|
-
if(move) {
|
|
141
|
+
if (move) {
|
|
136
142
|
return move.insufficientMaterial === true
|
|
137
143
|
} else {
|
|
138
144
|
return new ChessJs(this.fen()).insufficient_material()
|
|
@@ -152,7 +158,7 @@ export class Chess {
|
|
|
152
158
|
* @returns {boolean} true, if the game is in checkmate at that move
|
|
153
159
|
*/
|
|
154
160
|
inCheckmate(move = this.lastMove()) {
|
|
155
|
-
if(move) {
|
|
161
|
+
if (move) {
|
|
156
162
|
return move.inCheckmate === true
|
|
157
163
|
} else {
|
|
158
164
|
return new ChessJs(this.fen()).in_checkmate()
|
|
@@ -164,7 +170,7 @@ export class Chess {
|
|
|
164
170
|
* @returns {boolean} true, if the game is in check at that move
|
|
165
171
|
*/
|
|
166
172
|
inCheck(move = this.lastMove()) {
|
|
167
|
-
if(move) {
|
|
173
|
+
if (move) {
|
|
168
174
|
return move.inCheck === true
|
|
169
175
|
} else {
|
|
170
176
|
return new ChessJs(this.fen()).in_check()
|
|
@@ -213,9 +219,10 @@ export class Chess {
|
|
|
213
219
|
* Load a PGN with variations, NAGs, header and annotations. cm-chess uses cm-pgn
|
|
214
220
|
* fot the header and history. See https://github.com/shaack/cm-pgn
|
|
215
221
|
* @param pgn
|
|
222
|
+
* @param sloppy to allow sloppy SAN
|
|
216
223
|
*/
|
|
217
|
-
loadPgn(pgn) {
|
|
218
|
-
this.pgn = new Pgn(pgn)
|
|
224
|
+
loadPgn(pgn, sloppy = this.props.sloppy) {
|
|
225
|
+
this.pgn = new Pgn(pgn, {sloppy: sloppy})
|
|
219
226
|
publishEvent(this.observers, {type: EVENT_TYPE.initialized, pgn: pgn})
|
|
220
227
|
}
|
|
221
228
|
|
|
@@ -226,7 +233,7 @@ export class Chess {
|
|
|
226
233
|
* @param sloppy to allow sloppy SAN
|
|
227
234
|
* @returns {{}|null}
|
|
228
235
|
*/
|
|
229
|
-
move(move, previousMove = undefined, sloppy =
|
|
236
|
+
move(move, previousMove = undefined, sloppy = this.props.sloppy) {
|
|
230
237
|
try {
|
|
231
238
|
const moveResult = this.pgn.history.addMove(move, previousMove, sloppy)
|
|
232
239
|
publishEvent(this.observers,
|
|
@@ -271,7 +278,7 @@ export class Chess {
|
|
|
271
278
|
* @param sloppy to allow sloppy SAN
|
|
272
279
|
* @returns the move object or null if not valid
|
|
273
280
|
*/
|
|
274
|
-
validateMove(move, previousMove = undefined, sloppy =
|
|
281
|
+
validateMove(move, previousMove = undefined, sloppy = this.props.sloppy) {
|
|
275
282
|
return this.pgn.history.validateMove(move, previousMove, sloppy)
|
|
276
283
|
}
|
|
277
284
|
|
package/test/TestChess.js
CHANGED
|
@@ -255,4 +255,41 @@ Kc4 7. Qb4+ Kd5 8. Qc5#) 5. Bc5# 1-0`)
|
|
|
255
255
|
assert.true(chess.inCheck())
|
|
256
256
|
})
|
|
257
257
|
|
|
258
|
+
// see https://www.chessmail.de/forum/thread.html?key=GINGFqlNiAjf&sv=6
|
|
259
|
+
it("should make a move with sloppy SAN", function () {
|
|
260
|
+
let chess = new Chess("r1R2r1k/1R6/1P2B2p/4pPp1/4N1P1/7P/5P2/2R3K1 w - - 1 44")
|
|
261
|
+
let result = chess.move("R8c7", undefined,false)
|
|
262
|
+
assert.true(result !== null)
|
|
263
|
+
chess = new Chess("r1R2r1k/1R6/1P2B2p/4pPp1/4N1P1/7P/5P2/2R3K1 w - - 1 44")
|
|
264
|
+
result = chess.move("Rc8c7", undefined,false)
|
|
265
|
+
assert.true(result == null)
|
|
266
|
+
chess = new Chess("r1R2r1k/1R6/1P2B2p/4pPp1/4N1P1/7P/5P2/2R3K1 w - - 1 44")
|
|
267
|
+
result = chess.move("Rc8-c7", undefined,false)
|
|
268
|
+
assert.true(result == null)
|
|
269
|
+
chess = new Chess("r1R2r1k/1R6/1P2B2p/4pPp1/4N1P1/7P/5P2/2R3K1 w - - 1 44")
|
|
270
|
+
result = chess.move("Rc8-c7", undefined,true)
|
|
271
|
+
assert.true(result != null)
|
|
272
|
+
chess = new Chess("r1R2r1k/1R6/1P2B2p/4pPp1/4N1P1/7P/5P2/2R3K1 w - - 1 44")
|
|
273
|
+
result = chess.move("Rc8c7", undefined,true)
|
|
274
|
+
assert.true(result != null)
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
it("should load a sloppy pgn", function () {
|
|
278
|
+
const pgn = `pgn [SetUp "1"]
|
|
279
|
+
[FEN "r1R2r1k/1R6/1P2B2p/4pPp1/4N1P1/7P/5P2/2R3K1 w - - 1 44"]
|
|
280
|
+
|
|
281
|
+
1.Rc8c7 *`
|
|
282
|
+
let chess
|
|
283
|
+
try {
|
|
284
|
+
chess = new Chess({pgn: pgn, sloppy: false})
|
|
285
|
+
assert.fail("should throw error")
|
|
286
|
+
} catch (e) {
|
|
287
|
+
// ok
|
|
288
|
+
}
|
|
289
|
+
assert.true(chess == null)
|
|
290
|
+
// should work with sloppy = true
|
|
291
|
+
chess = new Chess({pgn: pgn, sloppy: true})
|
|
292
|
+
assert.true(chess != null)
|
|
293
|
+
})
|
|
294
|
+
|
|
258
295
|
})
|