-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpositionsFromSinglePGN.js
61 lines (44 loc) · 1.48 KB
/
positionsFromSinglePGN.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var Chess = require('./chess').Chess;
var _ = require('lodash');
module.exports = function(pgn) {
console.log("NN: " + pgn.split('\n\n').length);
console.log("RN: " + pgn.split('\r\n').length);
console.log("R: " + pgn.split('\r').length);
console.log("N: " + pgn.split('\n').length);
//pgn = pgn.replace(new RegExp('\r\n', 'g'), '\n');
var chess = new Chess();
var succ = chess.load_pgn(pgn);
//succ ? console.log('true') : console.error('false');
if (!succ) {
var parts = pgn.split(" ");
var nge7 = parts.indexOf('Nge7');
var nge2 = parts.indexOf('Nge2');
if (nge7 !== -1 || nge2 !== -1) {
if (nge7 !== -1) parts[nge7] = 'Ne7';
if (nge2 !== -1) parts[nge2] = 'Ne2';
} else {
throw 'Chess.js pgn parse failed';
}
pgn = parts.join(" ");
console.log(pgn);
chess = new Chess();
succ = chess.load_pgn(pgn);
if (!succ) {
// Did not help
throw 'Chess.js pgn parse failed';
}
}
// Get fens for the game
return getPositions(chess.history({verbose: true}));
}
function getPositions(moves) {
// moves has 1st move at index 0
var moveNum = 1;
var newchess = new Chess(); // Temporary Chess instance to apply moves to
// Applying moves one by one and collecting positions
var positions = _.map(moves, function(move) {
newchess.move(move);
return {move: move.san, color: move.color, fen: newchess.fen(), eval: '?', movenum: moveNum++, bestmove: "?"};
});
return positions;
}