siggame / Cerveau

Node.js implementation of a multi-game server intended for game with AIs.
MIT License
10 stars 15 forks source link

chess SAN castling notation with zeroes '0-0' is unsupported #28

Closed sprdk4 closed 5 years ago

sprdk4 commented 5 years ago

Currently the server will claim 0-0 and 0-0-0 (using zeroes) are illegal moves for designating castling. both zeroes '0' and the capital letter 'O' should be acceptable ways to designate castling SAN notation.

The implementation could be as simple as replacing '0-0' and '0-0-0' (zeroes) with 'O-O' and 'O-O-O' on the server side before continuing with validation logic.

I expect AI implementers to google how to implement SAN notation, and the top links for 'chess SAN' either only mention using zeroes, or only mention the alternative of using capital letter 'O' as a sidenote:

https://en.wikipedia.org/wiki/Algebraicnotation(chess)#Castling Castling is indicated by the special notations 0-0 (for kingside castling) and 0-0-0 (queenside castling).

While the FIDE Handbook, appendix C.13[5] uses the digit zero (0-0 and 0-0-0), PGN requires the uppercase letter O (O-O and O-O-O).

   +-----------------+
 8 | r n b q k . . r |
 7 | p p p p b p p p |
 6 | . . . . p n . . |
 5 | . . . . . . . . |
 4 | . . . . . . . . |
 3 | . . . P . N P B |
 2 | P P P . P P . P |
 1 | R N B Q K . . R |
   +-----------------+
     a b c d e f g h
Game is over. I lost :( because: Made an invalid move ("0-0").
JacobFischer commented 5 years ago

Interesting edge case.

Just for reference all move are fed through chess.js for all logic related stuff. We've enabled a flag they support called sloppy that I was hoping would handle cases such as this.

I'll add a quick 0 -> O conversion for you guys, as you mentioned. However be aware that the moves stored in game.history will probably always be O.

sprdk4 commented 5 years ago

I took a look at move_from_san in chess.js and I'm noticing the logic wouldn't properly handle the notation for enpassant either (e.p.). I'm guessing only the PGN format for SAN notation was referenced when it was implemented

https://en.wikipedia.org/wiki/Algebraicnotation(chess)#Captures En passant captures are indicated by specifying the capturing pawn's file of departure, the "x", the destination square (not the square of the captured pawn), and (optionally) the suffix "e.p." indicating the capture was en passant.[2] For example, exd6e.p.

(I've also seen some documentation that puts a space before the e.p. ex exd6 e.p.)

Also, I think it won't properly handle '++' notation for designating checkmate, instead of '#'

I would encourage replacing stripped_san of chess.js to

(untested)
return move.replace(/=/, '').replace(/ ?e.p./,'').replace(/((++?)|(#))?[?!]*$/, '');

I also noticed a comment at line 1115 of chess.js saying SAN 'Rc1c4' is technically invalid. Am I missing something or is that actually a perfectly valid SAN?

   +-----------------+
 8 | . . . k . K . . |
 7 | . . . . . . . . |
 6 | . . R . . . . . |
 5 | . . . . . . . . |
 4 | . . p . R . . . |
 3 | . . . . . . . . |
 2 | . . . . . . . . |
 1 | . . R . . . . . |
   +-----------------+
     a b c d e f g h

[edit], actually I see now the 'R1c4' is the correct way to represent this.

JacobFischer commented 5 years ago

c1c4 looks valid to me, assuming it is that side's turn. Not sure how moody it will be on the start of that string having an R or not (I would assume it won't care with sloppy on). Though I am no chess master.

I seriously doubt there's an error in chess.js's logic for standard chess games. They are used by a ton of projects, and in my experience literally every time someone thinks chess.js screwed up, they did instead.

sprdk4 commented 5 years ago

In that case, it sounds like only PGN format for SAN notation is intended to be supported, and that it should be better documented to developers that they shouldn't be using FIDE format

JacobFischer commented 5 years ago

Yes that sounds reasonable. If you are in the MST AI class this semester that's probably something the whole class should know so they don't have to fight the SAN formatting, as you've just done, and can focus on coding.

sprdk4 commented 5 years ago

I'm just an alumni that's simply updating my engine with the latest framework to see if anyone can best me. So if theres any mailing list for this years AI class, I'm not on it.

JacobFischer commented 5 years ago

Ah ok. Chess just started for the students this semester so I've been fielding questions over a variety of platforms about this stuff. Sorry about that :P

JacobFischer commented 5 years ago

Implemented with 7538170fa97135f9885b5f583216409f74ae9ad3

If you or anyone else has any questions about the chess re-work, of any part of the Cadre AI framework feel free to make an issue of ask me :D