cm-chess 2.1.3 → 2.1.7

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 CHANGED
@@ -13,7 +13,7 @@ PGN header values, Nags and Comments.
13
13
  - It has a similar API to chess.js
14
14
  - It can handle history variants
15
15
  - It can add Nags, Comments and Annotations
16
- - It is used by the 7,000 users from [chessmail](https://www.chessmail.eu) (it works)
16
+ - It is used by the 7,000 users of [chessmail](https://www.chessmail.de) (it works)
17
17
 
18
18
  ## API
19
19
 
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "cm-chess",
3
- "version": "2.1.3",
3
+ "version": "2.1.7",
4
4
  "description": "Like chess.js, but in ES6 and can handle variants (tree-structured move history)",
5
- "main": "index.html",
5
+ "main": "./src/cm-chess/Chess.js",
6
6
  "scripts": {
7
7
  "test": "mocha --require babel-core/register ./test/*.js",
8
8
  "postinstall": "node postinstall.js"
@@ -32,8 +32,18 @@ export const FEN = {
32
32
  */
33
33
  export class Chess {
34
34
 
35
- constructor(fen = FEN.start) {
36
- this.load(fen)
35
+ constructor(fenOrProps = FEN.start) {
36
+ if(typeof fenOrProps === "string") {
37
+ this.load(fenOrProps)
38
+ } else {
39
+ if(fenOrProps.fen) {
40
+ this.load(fenOrProps.fen)
41
+ } else if(fenOrProps.pgn) {
42
+ this.loadPgn(fenOrProps.pgn)
43
+ } else {
44
+ this.load(FEN.start)
45
+ }
46
+ }
37
47
  }
38
48
 
39
49
  /**
@@ -258,8 +268,17 @@ export class Chess {
258
268
  move.variation = move.variation.splice(index)
259
269
  }
260
270
 
261
- static plyToTurnNumber(ply) {
262
- return (ply - 1) / 2 + 1
271
+ plyCount() {
272
+ return this.history().length
263
273
  }
264
274
 
275
+ fenOfPly(plyNumber) {
276
+ if(plyNumber > 0) {
277
+ return this.history()[plyNumber - 1].fen
278
+ } else {
279
+ return this.setUpFen()
280
+ }
281
+ }
282
+
283
+
265
284
  }
@@ -14,12 +14,22 @@ export class Fen {
14
14
  return this._fen
15
15
  }
16
16
 
17
+ /**
18
+ * https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
19
+ * @param fen
20
+ */
17
21
  set fen(fen) {
18
22
  this._fen = fen
19
23
  const fenParts = fen.split(" ")
20
24
  this.position = fenParts[0]
21
25
  this.colorToPlay = fenParts[1]
22
26
  this.castlings = fenParts[2].split("")
27
+ this.enPassantTargetSquare = fenParts[3]
28
+ if(this.enPassantTargetSquare === "-") {
29
+ this.enPassantTargetSquare = undefined
30
+ }
31
+ this.plyClock = parseInt(fenParts[4], 10)
32
+ this.moveNumber = parseInt(fenParts[5], 10)
23
33
  }
24
34
 
25
35
  }
package/test/TestChess.js CHANGED
@@ -13,26 +13,25 @@ describe("Chess", function () {
13
13
  assert.equals(chess.fen(), FEN.start)
14
14
  })
15
15
 
16
- it("should load a game from FEN", function() {
16
+ it("should load a game from FEN", function () {
17
17
  const fen = "4k3/pppppppp/8/8/8/8/PPPPPPPP/4K3 w - - 0 1"
18
18
  const chess = new Chess(fen)
19
19
  assert.equals(chess.pgn.header.tags[TAGS.FEN], fen)
20
20
  assert.equals(chess.fen(), fen)
21
21
  })
22
22
 
23
- it("should load a pgn with SetUp and FEN", function() {
23
+ it("should load a pgn with SetUp and FEN", function () {
24
24
  const pgn = `[SetUp "1"]
25
25
  [FEN "4k3/pppppppp/8/8/8/8/PPPPPPPP/4K3 w - - 0 1"]
26
26
 
27
27
  1. e4 (1. d4 {Die Variante} d5) e5 {Ein Kommentar} 2. a3`
28
- const chess = new Chess()
29
- chess.loadPgn(pgn)
28
+ const chess = new Chess({pgn: pgn})
30
29
  assert.equals(chess.move("Nc6"), undefined)
31
30
  const result = chess.move("h6")
32
31
  assert.equals(result.fen, "4k3/pppp1pp1/7p/4p3/4P3/P7/1PPP1PPP/4K3 w - - 0 3")
33
32
  })
34
33
 
35
- it("should load a pgn", function() {
34
+ it("should load a pgn", function () {
36
35
  const chess = new Chess()
37
36
  const pgn = `[Event "IBM Kasparov vs. Deep Blue Rematch"]
38
37
  [Site "New York, NY USA"]
@@ -59,7 +58,6 @@ describe("Chess", function () {
59
58
  })
60
59
 
61
60
  it("should load a pgn with SetUp", () => {
62
- const chess = new Chess()
63
61
  const pgn = `[Event "?"]
64
62
  [White "?"]
65
63
  [Black "?"]
@@ -69,7 +67,7 @@ describe("Chess", function () {
69
67
 
70
68
  1. Qc1 Qe6 2. Qxc7
71
69
  0-1`
72
- chess.loadPgn(pgn)
70
+ const chess = new Chess({pgn: pgn})
73
71
  assert.equals(chess.history()[2].san, "Qxc7")
74
72
  })
75
73
 
@@ -89,7 +87,7 @@ describe("Chess", function () {
89
87
 
90
88
  1... Bf8 (1... Qf8? 2. Qxf8+ Bxf8 3. exd4) 2. exd4 Qxd4+ {%Q} 3. Kh1 Bh3
91
89
  0-1`
92
- chess.loadPgn(pgn, {}, true)
90
+ chess.loadPgn(pgn)
93
91
  assert.equals(5, chess.pgn.history.moves.length)
94
92
  assert.equals("Schaak opheffen", chess.pgn.header.tags[TAGS.White])
95
93
  assert.equals("app 037-1", chess.pgn.header.tags[TAGS.Annotator])
@@ -180,9 +178,12 @@ describe("Chess", function () {
180
178
  chess.undo(chess.history()[5])
181
179
  // console.log(chess.history());
182
180
  assert.equals(chess.history().length, 5)
181
+ assert.equals(chess.plyCount(), 5)
182
+ assert.equals(chess.fenOfPly(0), "8/8/b2Bq3/7Q/3kp3/5pP1/8/3K4 w - - 0 1")
183
+ assert.equals(chess.fenOfPly(3), "8/8/b2Bq3/8/4p3/3k1pP1/2Q5/3K4 b - - 3 2")
183
184
  })
184
185
 
185
- it("should not load incorrect FEN", function() {
186
+ it("should not load incorrect FEN", function () {
186
187
  const fen = "4k3/pppppppp/8/8/8/8/PPPPPP/4K3 w - - 0 1"
187
188
  try {
188
189
  new Chess(fen)
@@ -199,9 +200,9 @@ describe("Chess", function () {
199
200
  }
200
201
  })
201
202
 
202
- it("should load different PGNs and then work correctly", function() {
203
+ it("should load different PGNs and then work correctly", function () {
203
204
  const fen = "ppppkppp/pppppppp/pppppppp/pppppppp/8/8/8/RNBQKBNR w KQ - 0 1"
204
- const chess = new Chess(fen)
205
+ const chess = new Chess({fen: fen})
205
206
  assert.true(chess.move("e4") === undefined)
206
207
  assert.true(chess.move("Ke2") !== undefined)
207
208
  chess.load(FEN.start)
package/test/TestFen.js CHANGED
@@ -8,5 +8,8 @@ describe("Fen", function () {
8
8
  assert.equals(fen.position, "rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R")
9
9
  assert.equals(fen.colorToPlay, "b")
10
10
  assert.equals(fen.castlings.length, 4)
11
+ assert.equals(fen.enPassantTargetSquare, undefined)
12
+ assert.equals(fen.plyClock, 1)
13
+ assert.equals(fen.moveNumber, 2)
11
14
  })
12
15
  })