cm-chess 3.2.1 → 3.3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chess",
3
- "version": "3.2.1",
3
+ "version": "3.3.0",
4
4
  "description": "It's like chess.js, but in ES6 and can handle variations (tree-structured move history)",
5
5
  "main": "./src/Chess.js",
6
6
  "type": "module",
package/src/Chess.js CHANGED
@@ -96,7 +96,11 @@ export class Chess {
96
96
  * @returns {boolean} true, if the game is over at that move
97
97
  */
98
98
  gameOver(move = this.lastMove()) {
99
- return move && move.gameOver
99
+ if(move) {
100
+ return move.gameOver
101
+ } else {
102
+ return new ChessJs(this.fen()).game_over()
103
+ }
100
104
  }
101
105
 
102
106
  /**
@@ -104,7 +108,11 @@ export class Chess {
104
108
  * @returns {boolean} true, if the game is in draw at that move
105
109
  */
106
110
  inDraw(move = this.lastMove()) {
107
- return move && move.inDraw === true
111
+ if(move) {
112
+ return move.inDraw === true
113
+ } else {
114
+ return new ChessJs(this.fen()).in_draw()
115
+ }
108
116
  }
109
117
 
110
118
  /**
@@ -112,7 +120,11 @@ export class Chess {
112
120
  * @returns {boolean} true, if the game is in statemate at that move
113
121
  */
114
122
  inStalemate(move = this.lastMove()) {
115
- return move && move.inStalemate === true
123
+ if(move) {
124
+ return move.inStalemate === true
125
+ } else {
126
+ return new ChessJs(this.fen()).in_stalemate()
127
+ }
116
128
  }
117
129
 
118
130
  /**
@@ -120,7 +132,11 @@ export class Chess {
120
132
  * @returns {boolean} true, if the game is in draw, because of unsufficiant material at that move
121
133
  */
122
134
  insufficientMaterial(move = this.lastMove()) {
123
- return move && move.insufficientMaterial === true
135
+ if(move) {
136
+ return move.insufficientMaterial === true
137
+ } else {
138
+ return new ChessJs(this.fen()).insufficient_material()
139
+ }
124
140
  }
125
141
 
126
142
  /**
@@ -136,7 +152,11 @@ export class Chess {
136
152
  * @returns {boolean} true, if the game is in checkmate at that move
137
153
  */
138
154
  inCheckmate(move = this.lastMove()) {
139
- return move && move.inCheckmate === true
155
+ if(move) {
156
+ return move.inCheckmate === true
157
+ } else {
158
+ return new ChessJs(this.fen()).in_checkmate()
159
+ }
140
160
  }
141
161
 
142
162
  /**
@@ -144,7 +164,11 @@ export class Chess {
144
164
  * @returns {boolean} true, if the game is in check at that move
145
165
  */
146
166
  inCheck(move = this.lastMove()) {
147
- return move && move.inCheck === true
167
+ if(move) {
168
+ return move.inCheck === true
169
+ } else {
170
+ return new ChessJs(this.fen()).in_check()
171
+ }
148
172
  }
149
173
 
150
174
  /**
@@ -232,11 +256,11 @@ export class Chess {
232
256
  * k - kingside castling
233
257
  * q - queenside castling
234
258
  * A flags value of pc would mean that a pawn captured a piece on the 8th rank and promoted.
235
- * @param previousMove
259
+ * @param move
236
260
  * @returns {{}}
237
261
  */
238
- moves(options = undefined, previousMove = this.lastMove()) {
239
- const chessJs = new ChessJs(this.fen(previousMove))
262
+ moves(options = undefined, move = this.lastMove()) {
263
+ const chessJs = new ChessJs(this.fen(move))
240
264
  return chessJs.moves(options)
241
265
  }
242
266
 
@@ -291,6 +315,17 @@ export class Chess {
291
315
  return result
292
316
  }
293
317
 
318
+ /**
319
+ * get the piece on a square
320
+ * @param square
321
+ * @param move
322
+ * @returns {{color: any, type: any}|null}
323
+ */
324
+ piece(square, move = this.lastMove()) {
325
+ const chessJs = move ? new ChessJs(move.fen) : new ChessJs()
326
+ return chessJs.get(square)
327
+ }
328
+
294
329
  /**
295
330
  * @returns {string} "b" or "w" the color to move in the main variation
296
331
  */
package/test/TestChess.js CHANGED
@@ -19,6 +19,8 @@ describe("Chess", function () {
19
19
  const chess = new Chess(fen)
20
20
  assert.equal(chess.pgn.header.tags[TAGS.FEN], fen)
21
21
  assert.equal(chess.fen(), fen)
22
+ assert.equal(chess.piece("e1").type, "k")
23
+ assert.equal(chess.piece("e1").color, "w")
22
24
  })
23
25
 
24
26
  it("should load a pgn with SetUp and FEN", function () {
@@ -247,4 +249,9 @@ Kc4 7. Qb4+ Kd5 8. Qc5#) 5. Bc5# 1-0`)
247
249
  assert.equal(JSON.stringify(chess.moves({ piece: "n" })), '["Na4","Nb5","Nd5","Ne4","Nb1","Nf3","Nh3"]')
248
250
  })
249
251
 
252
+ it("should detect a check in a game without moves", function() {
253
+ const chess = new Chess("4k3/1P6/8/b7/6r1/8/pp2PPPP/2R1KBNR w K - 0 1")
254
+ assert.true(chess.inCheck())
255
+ })
256
+
250
257
  })