jhlywa / chess.js Goto Github PK
View Code? Open in Web Editor NEWA TypeScript chess library for chess move generation/validation, piece placement/movement, and check/checkmate/draw detection
License: BSD 2-Clause "Simplified" License
A TypeScript chess library for chess move generation/validation, piece placement/movement, and check/checkmate/draw detection
License: BSD 2-Clause "Simplified" License
Hello everybody.
fine. I was not sure to send this issue, I'm rather satisfy a doubt, and nice if also helps to improve the project.
Well, i'm not sure if variables like Chess.BLACK and Chess.PAWN should be static. if so, you could use prototype property. Something like:
Chess.prototype = {
BLACK: 'b',
WHITE: 'w',
EMPTY: -1
};
It could also be used for the functions.
Sorry for my English.
Are there any functions to get notations in several formats like the notations at http://en.wikipedia.org/wiki/Chess_notation#Notation_systems_for_humans, except for san.
It would be great to have that.
var pos=new Chess('b3k2r/5p2/4p3/1p5p/6p1/2PR2P1/BP3qNP/6QK b k - 2 28')
undefined
pos.fen()
"b3k2r/5p2/4p3/1p5p/6p1/2PR2P1/BP3qNP/6QK b k - 2 28"
pos.move({from:'a8', to:'g2'})
Object {color: "b", from: "a8", to: "g2", flags: "c", piece: "b"β¦}
pos.fen()
"4k2r/5p2/4p3/1p5p/6p1/2PR2P1/BP3qbP/6QK w kq - 0 29"
The black king obtained the queenside castle! Magic! Losing castle status may happen, but restoring castle status is something very new in the chess!
Hi Jeff,
it would be nice to add a package.json and make this library available in npm. That's not exactly setting the world on fire but would be nice :)
Thanks and Greets from Germany,
Falco
What about the addition of multiple arrows for analysis and square colouring?
Is there a list of possible move flags combinations? In the docs, other than the single-flag string, a pc
value (promotion-capture) is mentioned.
I'm not sure if there are other possible combinations like that one. Can someone enlighten me? π
I might just not fully understand how to use chess.js, but I was hoping to parse some PGN then start from the first move for a PGN player. I'm able to make this work with minor modifications that would not affect how anybody might currently be relying on load_pgn to work (returning true), as follows at the very bottom of the method. Notice that I also return the moves so that I can start from the first move for a PGN. Again maybe I'm misunderstanding how to use chess.js so if this is not useful but somebody can nevertheless please share some insight, I don't mind if the issue gets closed but with comments please ;) Thanks in advance:
//return true;
if (options.rewind) {
reset();
return moves;
}
else {
return true;
}
FYI, I've done some refactoring in this branch using prototype, and class static for constant, cause I was having a hard time to follow the code in its current form. Let me know if you want to move it to master and I'll prepare a PR.
Tests are passing, and It should be backward compatible, except for the constants which are now on the Chess classes.
Just thought you should know, line 13 of package.json has a comma at the end when it shouldn't which causes chess.js to not install correctly with npm.
Hello, I'm trying to create a tutorial called Pawn Wars, which is a normal chess game with only pawns. I am loading the FEN "8/pppppppp/8/8/8/8/PPPPPPPP/8", but it keeps returning false (invalid). Can you think of any way I can load this fen into chessjs?
Hello jhlywa,
Could we have an example in your package (in "master" or in your jquery "widget") in which you can choose the piece you want to take when you are promoting a pawn.
It could be very good for people whom don't practise javascript every day.
Thanks for that tool, it is a good one π
Thierry
If no tags are used in the pgn, a newline is inserted at the start of the output.
First off, let me say that this is such a great library! It's been indispensable to me in writing other JS chess code.
I'm publishing node/browser code to npm that depends on chess.js and the last published version of this library is 0.1.0
which has some differences (based on a quick diff of the code). It'd be very helpful to have the latest code on npm (and for future updates). Bonus points if it follows semver π
Thanks!
bug description copy/pasted from jzacsh@232e5d0
var chess = new Chess();
chess.move({from: 'e2', to: 'e4'});
var pgn = chess.pgn({max_width: 5, newline_char: '\n'});
// output:
// [White "carolyn"]
// [Black "hippo"]
// [Date "2014-1-18"]
//
// 1. e4
var chessB = new Chess();
chessB.load_pgn(pgn);
// BUG HERE:
chessB.pgn({max_width: 5, newline_char: '\n'});
// outputs: [[White "carolyn"] [Black "hippo"] [Date "2014-1-18"] "[White "carolyn"] [Black "hippo"] [Date "2014-1-18"]"] 1. e4
test added that confirms this behavior in: jzacsh@fa55217
Just wondering, I guess it is performance related.
var SQUARES = {
a8: 0, b8: 1, c8: 2, d8: 3, e8: 4, f8: 5, g8: 6, h8: 7,
a7: 16, b7: 17, c7: 18, d7: 19, e7: 20, f7: 21, g7: 22, h7: 23,
a6: 32, b6: 33, c6: 34, d6: 35, e6: 36, f6: 37, g6: 38, h6: 39,
a5: 48, b5: 49, c5: 50, d5: 51, e5: 52, f5: 53, g5: 54, h5: 55,
a4: 64, b4: 65, c4: 66, d4: 67, e4: 68, f4: 69, g4: 70, h4: 71,
a3: 80, b3: 81, c3: 82, d3: 83, e3: 84, f3: 85, g3: 86, h3: 87,
a2: 96, b2: 97, c2: 98, d2: 99, e2: 100, f2: 101, g2: 102, h2: 103,
a1: 112, b1: 113, c1: 114, d1: 115, e1: 116, f1: 117, g1: 118, h1: 119
};
Hi guys,
I wanted to let you know about the chess game I made ontop of chess.js. You have building a jQuery interface and AI on your todo list. Feel free to steal any of it from my code or suggest how you'd like it best put into yours and I'll port it over (its currently a wrapper around chess.js)
In chess 2 knights are insufficient to force a mate and thus if on the board are only 2 knights, this should be counted as insufficient material (draw).
One can read more here: http://en.wikipedia.org/wiki/Two_knights_endgame
I'm seeing pgn output for games built using this lib, rendered as "2014-1-18
"
Will send a patch when I find time (unless someone beats me to it). In the meantime I'm just hacking around it.
for reference, see section 8.1.1.3
of ICC PGN spec
Hello,
I am using your script with chessboard.js.
Here is the FEN: '8/8/2BkbRp1/1pb1p3/7n/2N5/7P/r6K w - -'
It works fine with chessboard, but when I feed it into chess.js, the .load() returns false and the .moves() call returns empty.
I even tried feeding it to chessboard, then used the chessboard .fen() function to get a FEN for chess.js. It returned '8/8/2BkbRp1/1pb1p3/7n/2N5/7P/r6K'.
chess.js would not accept this either.
Here is my code:
var game = new Chess('8/8/2BkbRp1/1pb1p3/7n/2N5/7P/r6K');
var possibleMoves = game.moves();
take care,
lee
Hi Jeff,
dont' want to annoy you with my pull requests and issues... :)
It would be nice to have a function which returns all played moves so far. The pgn() method does exactly what I need if the .info() wasn't set before. With the current version the only workaround is to create the pgn before setting the PGN info, to get just the played moves.
Perhaps a simple method which returns a history as an array of SAN moves would be more flexible, that's your choice.
Thanks for your help!
Falco
I'd like to get a list of possible moves the piece on a particular space. Right now the "moves" function returns all possible moves for every piece of the color taking the turn. I can filter that list down to the piece I'm interested in, however it seems fairly inefficient to build the full list and then pare it back down.
User Story: As a player, I want to see all possible moves for the piece I've selected so that I can choose where to move the piece.
I'm playing with chessboardjs.com example (pieces highlighting 1). After about 5 minutes of the 'game' one of the bishop made wrong move - from white field to black field, as a result I can see two white bishops on black fields.
In comments in the chess.js on the site I can see
"// downloaded from https://github.com/jhlywa/chess.js/blob/master/chess.js
// on 27 July 2013
// commit: eed8f8a"
I hope that this issue was already fixed. In other case I hope my report helps to fix it.
Thanks!
Evaluation symbols like ??
(blunder) and !!
(brilliant) are valid after moves
var c = new Chess();
c.load_pgn('1. e4!'); // true
c.load_pgn('1. e4?'); // true
c.load_pgn('1. e4??'); // false, but should be true
c.load_pgn('1. e4?!'); // false, but should be true
c.load_pgn('1. e4!!'); // false, but should be true
More symbols here:
https://en.wikipedia.org/wiki/Chess_annotation_symbols#Move_evaluation_symbols
Looks like it was broken in this commit: 64b63d1
var chess = new Chess();
chess.move();
causes move and moves variables to be leaked into the global namespace. I'm guessing somewhere a 'var' is missing.
Which software license are you guys using for chess.js? You should probably pick one if you haven't already.
Just for convenience, it would be great to have a minified version.
I am really impressed with the quality and core readability.
Thanks for a beatiful product
The pgn output doesn't indicate what the starting position was. Use SetUp and FEN tags to indicate a starting position different than the usual.
Call:
Chess("8/3r4/8/8/8/2R5/1Q6/8 b KQkq - 0 1").game_over();
In file chess.js
:
Error: Cannot read property 'type' of undefined (line 474)
/* if pawn promotion */
if (board[from].type === PAWN &&
//.........etc...................
load_pgn doesn't load PGNs that contain move sequences in parentheses:
1. d4 d5 2. c4 e6 3. e3 Nf6 4. a3 c6 5. Nf3 Bd6 6. cxd5?!
(6. Bd3 O-O 7. O-O dxc4 8. Bxc4 c5 9. Nc3 Nc6 10. dxc5 Bxc5)
6... cxd5 7. Bd3 a6
This should be valid according to the PGN spec:
8.2.5: Movetext RAV (Recursive Annotation Variation)
An RAV (Recursive Annotation Variation) is a sequence of movetext containing
one or more moves enclosed in parentheses. An RAV is used to represent an
alternative variation. The alternate move sequence given by an RAV is one that
may be legally played by first unplaying the move that appears immediately
prior to the RAV. Because the RAV is a recursive construct, it may be nested.
*** The specification for import/export representation of RAV elements needs
further development.
Sources:
http://www6.chessclub.com/help/PGN-spec
http://en.wikipedia.org/wiki/Portable_Game_Notation#Movetext
Would be useful when reading a PGN file to have the option of reading only the game tags if the moves aren't required, to reduce overhead.
For example for a PGN viewer, where you might need a list of the contents of the PGN file, but the moves don't need to be parsed until a particular game is loaded.
https://github.com/siansell/pgnviewer is my first attempt at a PGN viewer with chess.js and chessboard.js.
Hello.
I've been working on an extensive rewrite of chess.js for several months. I was wondering if you or the community had any interest in my work -- or if I should just release it as a separate repo. ("chess-es6.js"...?)
The biggest feature I added was PGN parser support for variations, as well as cursor semantics for iterating back and forth across moves. Making a new move from anything other than the head of a variation implicitly creates a new variation.
test("many nested variations", () => {
const pgn = '1. d4 (1. c4 (1. b4 (1. a4 a5) 1... b5) 1... c5) (1. e4 (1. f4 (1. g4 (1. h4 h5) 1... g5) 1... f5) 1... e5) 1... d5 (1... c5 (1... b5 (1... a5))) (1... e5 (1... f5 (1... g5 2. g3) 2. f3) 2. e3) 2. e3 (2. f3 f6) (2. g3 g6) (2. h3 h6) 2... e6';
chess.loadPgn(pgn);
assert.equal(chess.toPgn(), pgn, "PGN parser failed to reassemble the given PGN text");
});
Thanks.
The regular expression PGN parser occasionally moves the wrong piece when parsing moves without a disambiguator. See below:
var chess = new ch.Chess();
var pgn = '1. d4 Nf6 2. c4 e6 3. Nf3 c5 4. Nc3 cxd4 5. Nxd4 Bb4 6. Nb5';
chess.load_pgn(pgn);
util.puts(chess.pgn());
1. d4 Nf6 2. c4 e6 3. Nf3 c5 4. Nc3 cxd4 5. Nxd4 Bb4 6. Ncb5
Normally the move 6. Nb5
would need a disamiguator; however, the knight on c3 is pinned in this case, so none is needed. Furthur, the PGN output 6. Ncb5
is an illegal move.
Hello!
Same problem in load_pgn (with black):
chess = new Chess();
pgn = ['[SetUp "1"]', '[FEN "r4r1k/1p4b1/3p3p/5qp1/1RP5/6P1/3NP3/2Q2RKB b KQkq - 0 1"]', "", '1. ... Qc5+'];
chess.load_pgn(pgn.join("\n")); // false. Why? It is correct PGN
There's this pull request:
#33
But the latest code in master doesn't allow pawn promotion without equal sign any more. Has this feature been removed?
Now chess.js understands "fxg8=Q", but not "fxg8Q".
Call:
chess = new Chess();
pgn = ['[SetUp "1"]', '[FEN "7k/5K2/4R3/8/8/8/8/8 w KQkq - 0 1"]', "", '1. Rh6#'];
chess.load_pgn(pgn.join("\n")); // false. Why? It is correct PGN
Then:
chess.pgn(); // '[SetUp "1"]\n'+'[FEN "7k/5K2/4R3/8/8/8/8/8 w KQkq - 0 1"]'
I would like to be able to query the game for how many of each piece type are still on the board at any given time. For example, game.count('bP') could return 8. I would like to do this so I can display captured pieces on a sidebar. The simplest method I can think of is to check if the number of pawns < 8, then put the remainder of pawn icons in the sidebar. Any suggestions? Thank you in advance :).
Hi,
It seems that the check detection fails in some situations. An example:
c = new Chess('N3k3/8/8/8/8/8/5b2/4K3 w - - 0 1');
c.ascii();
// Returns: +------------------------+
// 8 | N . . . k . . . |
// 7 | . . . . . . . . |
// 6 | . . . . . . . . |
// 5 | . . . . . . . . |
// 4 | . . . . . . . . |
// 3 | . . . . . . . . |
// 2 | . . . . . b . . |
// 1 | . . . . K . . . |
// +------------------------+
// a b c d e f g h
c.moves().join(' ');
// Returns: Kd2 Ke2 Kxf2 Kf1 Kd1
// -> The white knight in a8 cannot move, as the king is in check.
// Everything is correct.
c.put({type: 'k', color: 'w'}, 'a1');
c.put({type: 'q', color: 'w'}, 'a1');
c.remove('a1');
c.ascii();
// Returns: +------------------------+
// 8 | N . . . k . . . |
// 7 | . . . . . . . . |
// 6 | . . . . . . . . |
// 5 | . . . . . . . . |
// 4 | . . . . . . . . |
// 3 | . . . . . . . . |
// 2 | . . . . . b . . |
// 1 | . . . . K . . . |
// +------------------------+
// a b c d e f g h
// -> Seems that nothing has changed, however...
c.moves().join(' ');
// Returns: Nc7+ Nb6 Kd2 Ke2 Kxf2 Kf1 Kd1
// -> Now the knight can move, while the white king is still in check.
In this example, the internal variable kings['w']
, that normally points to the square where the white king is located, points to a1. That makes check detection fails.
Hello, the first great work.
Now my problem / question is that I think there is a problem because it always fails to be entirely correct movement.
If I write :
chess.load_pgn("1. e4 e5 2. Qf3 Nh6 3. Bc4 Bc5 4. Qxf7")
Return false
I think it is wrong qeu return that value. And of course there is the chess.fen (); returned do not see the move "Qxf7"
Right now, the script just replaces comments by the
ms = ms.replace(/(\{[^}]+\})+?/g, '');
Feature request : Extract comments and associate them with the corresponding moves
Thank you from someone very grateful for this library!
We'd like to use chess.js (with chessboard.js) on a competitive play site. The other js/css libraries we utilize have CDN (we use fallbacks to local downloads if they can't be reached). Has there been some thought to adding this to a CDN? I believe it's free to add javascript libraries to cdnjs
I tried to use your amazing library to play chess 960.
I am initializing it
var chess = new Chess('nrbqkrnb/pppppppp/8/8/8/8/PPPPPPPP/NRBQKRNB w KQkq -');
and then I can play it nicely. Till I have to castle. Is is possible to add ability to recognize castling in chess 960?
Hello Jeff!
My neme is Michael and I'm from Belarus and speak Russian )
At first I want to thanks you for high quality chest plugin.
Sorry for my english ))
I wat to ask you about one thing. When I using command "fen" and in case black start first, I can't do it. Just possible start white.
Can I make game.turn() = 'b' ? I got a problem with it. Default is white....
Thanks for your'e time )
Hi. Thank you for the great library first of all!
I was experimenting with Meteor and created a simple app http://chess.meteor.com first by myself, but then found this lib and it works almost great. First I used PGN - it worked perfectly with one exception - it's too slow to be reloaded several times per turn. Then I switched to FEN, it works fast, but now I've lost history and repeated position draw check. I've fixed it by reloading from PGN right before making a move, but it doesn't look good.
Maybe it's possible to use FEN to load current game position and quickly parse PGN just for history without moves validation?
[15:40:01.593] var chess = new Chess()
[15:40:01.594] undefined
[15:40:19.412] chess.load('rnbqk2r/ppp1pp1p/5n1b/3p2pQ/1P2P3/B1N5/P1PP1PPP/R3KBNR b KQkq - 3 5')
[15:40:19.414] true
[15:42:42.097] chess.moves({'square' : 'f1', 'verbose' : true})
[15:42:42.099] [{color:"b", from:"e8", to:"g8", flags:"k", piece:"k", san:"O-O"}]
It seems that there might be a bug in the load_pgn method.
When using following PGN:
[Event "Live Chess"]
[Site "Chess.com"]
[Date "2015.11.10"]
[White "Passero82"]
[Black "bgknight"]
[Result "0-1"]
[WhiteElo "1175"]
[BlackElo "1255"]
[TimeControl "30|0"]
[Termination "bgknight won by resignation"]
1.e4 e5 2.Nf3 d6 3.d4 Bg4 4.dxe5 Bxf3 5.Qxf3 dxe5 6.Qf5 Nc6 7.Bb5 Nge7 8.Qxe5 Qd7 9.O-O Nxe5 10.Bxd7+ Nxd7
11.Rd1 O-O-O 12.Nc3 Ng6 13.Be3 a6 14.Ba7 b6 15.Na4 Kb7 16.Bxb6 cxb6 17.b3 b5 18.Nb2 Nge5 19.f3 Rc8 20.Rac1 Ba3
21.Rb1 Rxc2 22.f4 Ng4 23.Rxd7+ Kc6 24.Rxf7 Bxb2 25.Rxg7 Ne3 26.Rg3 Bd4 27.Kh1 Rxa2 28.Rc1+ Kb6 29.e5 Rf8 30.e6 Rxf4
31.e7 Re4 32.Rg7 Bxg7 0-1
and use:
var chess = new Chess();
chess.load_pgn(pgn);
it returns false however applications like Fritz are able to read the PGN properly so I started to investigate and added some additional debug info in the load_pgn method.
In this specific PGN, it seems like the algoritm thinks that the move 7... Nge7 is not a valid move.
In this code from the load_pgn method:
for (var half_move = 0; half_move < moves.length - 1; half_move++) {
move = get_move_obj(moves[half_move]);
/* move not possible! (don't clear the board to examine to show the
* latest valid position)
*/
if (move == null) {
console.log("Invalid move for " + half_move + " " + moves[half_move]);
return false;
} else {
make_move(move);
}
}
move == null for the half move 13 which is 7... Nge7
I have another PGN with the exact move:
[Event "Live Chess"]
[Site "Chess.com"]
[Date "2015.11.10"]
[White "awhichi"]
[Black "Passero82"]
[Result "0-1"]
[WhiteElo "1103"]
[BlackElo "1165"]
[TimeControl "30|0"]
[Termination "Passero82 won by checkmate"]
1.e4 e5 2.d4 exd4 3.Qxd4 Nc6 4.Qe3 d6 5.Bc4 Be6 6.Bb5 Nge7 7.Bxc6+ Nxc6 8.Nc3 Be7 9.Bd2 O-O 10.O-O-O Ne5
11.Nf3 Nc4 12.Qe2 c5 13.a3 Qb6 14.Bf4 Qxb2# 0-1
load_pgn also fails to load the PGN here for the same move: Nge7.
var pgnDump = ['[White "carolyn"]', '[Black "hippo"]', '[Date "2014-1-18"]', "", "1. e4 d5", "2. exd5 c5", "3. dxc6 Nxc6", "4. Bb5 b6", "5. Bxc6+ Qd7", "6. Bxd7+ Bxd7", "7. Qg4 Bxg4", "8. Kf1 Be2+", "9. Kxe2 Rd8", "10. d4 Rxd4", "11. Be3 Rd2+", "12. Kxd2"].join('\n');
var chess = new Chess();
chess.load_pgn(pgnDump); // should load 23 moves
I haven't found the exact line of code that causes the bug, but notice the 12th move is missing (Kxd2
):
chess.history().length; // returns 22
chess.history().join('\n');
e4
d5
exd5
c5
dxc6
Nxc6
Bb5
b6
Bxc6+
Qd7
Bxd7+
Bxd7
Qg4
Bxg4
Kf1
Be2+
Kxe2
Rd8
d4
Rxd4
Be3
Rd2+
Hello,
Before calling the .moves() function, is it possible to set the turn to black or white?
lee
There is no exposed (or documented) function to read header from loaded PGN.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.