cm-chess 2.1.4 → 2.1.8
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 +3 -3
- package/package.json +3 -3
- package/src/cm-chess/Chess.js +16 -6
- package/test/TestChess.js +11 -13
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# cm-chess
|
|
2
2
|
|
|
3
3
|
It is like [chess.js](https://github.com/jhlywa/chess.js),
|
|
4
|
-
but is an ES6 module, can handle history
|
|
4
|
+
but is an ES6 module, can handle history variations (tree-structured move history),
|
|
5
5
|
PGN header values, Nags and Comments.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
@@ -11,9 +11,9 @@ PGN header values, Nags and Comments.
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
13
13
|
- It has a similar API to chess.js
|
|
14
|
-
- It can handle history
|
|
14
|
+
- It can handle history variations
|
|
15
15
|
- It can add Nags, Comments and Annotations
|
|
16
|
-
- It is used by the 7,000 users
|
|
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.
|
|
4
|
-
"description": "Like chess.js, but in ES6 and can handle
|
|
5
|
-
"main": "
|
|
3
|
+
"version": "2.1.8",
|
|
4
|
+
"description": "Like chess.js, but in ES6 and can handle variations (tree-structured move history)",
|
|
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"
|
package/src/cm-chess/Chess.js
CHANGED
|
@@ -32,8 +32,18 @@ export const FEN = {
|
|
|
32
32
|
*/
|
|
33
33
|
export class Chess {
|
|
34
34
|
|
|
35
|
-
constructor(
|
|
36
|
-
|
|
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
|
/**
|
|
@@ -162,7 +172,7 @@ export class Chess {
|
|
|
162
172
|
}
|
|
163
173
|
|
|
164
174
|
/**
|
|
165
|
-
* Load a PGN with
|
|
175
|
+
* Load a PGN with variations, NAGs, header and annotations. cm-chess uses cm-pgn
|
|
166
176
|
* fot the header and history. See https://github.com/shaack/cm-pgn
|
|
167
177
|
* @param pgn
|
|
168
178
|
*/
|
|
@@ -173,7 +183,7 @@ export class Chess {
|
|
|
173
183
|
/**
|
|
174
184
|
* Make a move in the game.
|
|
175
185
|
* @param move
|
|
176
|
-
* @param previousMove optional, the previous move (
|
|
186
|
+
* @param previousMove optional, the previous move (for variations)
|
|
177
187
|
* @param sloppy to allow sloppy SAN
|
|
178
188
|
* @returns {{}|undefined}
|
|
179
189
|
*/
|
|
@@ -190,7 +200,7 @@ export class Chess {
|
|
|
190
200
|
* @returns {string} the PGN of the game.
|
|
191
201
|
*/
|
|
192
202
|
renderPgn() {
|
|
193
|
-
// TODO create pgn with
|
|
203
|
+
// TODO create pgn with variations, annotations, nags (for now just render main variant)
|
|
194
204
|
const chess = new ChessJs(this.setUpFen())
|
|
195
205
|
const moves = this.pgn.history.moves
|
|
196
206
|
for (const move of moves) {
|
|
@@ -271,4 +281,4 @@ export class Chess {
|
|
|
271
281
|
}
|
|
272
282
|
|
|
273
283
|
|
|
274
|
-
}
|
|
284
|
+
}
|
package/test/TestChess.js
CHANGED
|
@@ -2,7 +2,7 @@ import {Chess, COLOR, FEN} from "../src/cm-chess/Chess.js"
|
|
|
2
2
|
import {TAGS} from "../lib/cm-pgn/Header.js"
|
|
3
3
|
import {describe, it, assert} from "../node_modules/teevi/src/teevi.js"
|
|
4
4
|
|
|
5
|
-
// TODO create a test, which demonstrates all features, including
|
|
5
|
+
// TODO create a test, which demonstrates all features, including variations
|
|
6
6
|
|
|
7
7
|
describe("Chess", function () {
|
|
8
8
|
|
|
@@ -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
|
|
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
|
|
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])
|
|
@@ -185,7 +183,7 @@ describe("Chess", function () {
|
|
|
185
183
|
assert.equals(chess.fenOfPly(3), "8/8/b2Bq3/8/4p3/3k1pP1/2Q5/3K4 b - - 3 2")
|
|
186
184
|
})
|
|
187
185
|
|
|
188
|
-
it("should not load incorrect FEN", function() {
|
|
186
|
+
it("should not load incorrect FEN", function () {
|
|
189
187
|
const fen = "4k3/pppppppp/8/8/8/8/PPPPPP/4K3 w - - 0 1"
|
|
190
188
|
try {
|
|
191
189
|
new Chess(fen)
|
|
@@ -202,13 +200,13 @@ describe("Chess", function () {
|
|
|
202
200
|
}
|
|
203
201
|
})
|
|
204
202
|
|
|
205
|
-
it("should load different PGNs and then work correctly", function() {
|
|
203
|
+
it("should load different PGNs and then work correctly", function () {
|
|
206
204
|
const fen = "ppppkppp/pppppppp/pppppppp/pppppppp/8/8/8/RNBQKBNR w KQ - 0 1"
|
|
207
|
-
const chess = new Chess(fen)
|
|
205
|
+
const chess = new Chess({fen: fen})
|
|
208
206
|
assert.true(chess.move("e4") === undefined)
|
|
209
207
|
assert.true(chess.move("Ke2") !== undefined)
|
|
210
208
|
chess.load(FEN.start)
|
|
211
209
|
assert.true(chess.move("e4"))
|
|
212
210
|
})
|
|
213
211
|
|
|
214
|
-
})
|
|
212
|
+
})
|