Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chess960 - Wrong Move - Possible Castling #441

Open
sowson opened this issue Oct 17, 2023 · 1 comment
Open

Chess960 - Wrong Move - Possible Castling #441

sowson opened this issue Oct 17, 2023 · 1 comment

Comments

@sowson
Copy link

sowson commented Oct 17, 2023

Ask for contributors
This is a possible example that is wrong. Can you help me block this potential move by a code example?

Error at move 7
Start FEN: rqnkbbnr/pppppppp/8/8/8/8/PPPPPPPP/RQNKBBNR w KQkq - 0 1
PGN: 1. d3 Nf6 2. Bd2 Nd6 3. e3 Kc8 4. Be2 Nd5 5. Nf3 Nxe3+ 6. fxe3 Ne4 7. O-O

Screenshot 2023-10-17 at 06 46 18

Description
At FEN: b1rk1bqr/pppppppp/8/8/4B3/4P2P/PPPP1PPQ/BNRK3R w KQ - 3 8
I can wrongly PGN: d1f1

The Chessquid on the move I checked is impossible, and not a blue circle appears, but I am not 100% sure.
Screenshot 2023-10-17 at 07 08 07

Thank you!

@dhirallin
Copy link

dhirallin commented Feb 26, 2024

chess.js has no Chess960 support right? Although someone has submitted support to the pull requests, so you could try that?

Are you trying to filter out Chess960 games or support them?
If just filtering them out, why don't you just check whether the FEN's castling rights match up with chess.js.
For example:

var fenCastlingRights = fen.split(/\s+/)[2];
chess.load(fen);
var whiteCastlingRights = chess.getCastlingRights('w');
var blackCastlingRights = chess.getCastlingRights('b');
if(whiteCastlingRights.k !== fenCastlingRights.includes('K')
  || whiteCastlingRights.q !== fenCastlingRights.includes('Q')
  || blackCastlingRights.k !== fenCastlingRights.includes('k')
  || blackCastlingRights.q !== fenCastlingRights.includes('q'))
  return false; // FEN invalid
return true; // FEN valid

If that doesn't work you can manually check whether the FEN's castling rights match the kings' and rooks' initial position.
E.g. if the king is not on e1 but white has castling rights, then don't load the pgn.

function checkCastlingRights(fen) {
  if(!checkColorCastlingRights(fen, 'w') || !checkColorCastlingRights(fen, 'b'))
    return false;

  return true;
}

function checkColorCastlingRights(fen, color) {
  chess.load(fen);  
  var castlingRights = fen.split(/\s+/)[2];
  var castlingRights = fen.replace(new RegExp((color === 'w' ? 'kq' : 'KQ', 'g')), '').toLowerCase();
  if(castlingRights.includes('k') || castlingRights.includes('q')) {
    piece = chess.get('e' + (color === 'w' ? '1' : '8'));
    if(!piece || piece.type !== 'k' || piece.color != color)
      return false;

    if(castlingRights.includes('k')) {
      piece = chess.get('h' + (color === 'w' ? '1' : '8'));
      if(!piece || piece.type !== 'k' || piece.color != color)
        return false;
    }

    if(castlingRights.includes('q')) {
      piece = chess.get('a' + (color === 'w' ? '1' : '8'));
      if(!piece || piece.type !== 'k' || piece.color != color)
        return false;
    }
  }
  return true;
}

If you're trying to hack your interface to support Chess960 without modifying chess.js, then you probably need to parse the pgn manually, one move at a time.
Then before each call to Chess.move() you check if the king is moving more than one space or is moving onto a rook (rook castling). If so, then modify the FEN manually.
You also have to modify castling rights when a rook is moved.
For all other moves, you have to remove the opponent's castling rights temporarily before calling move() otherwise chess.js will flag the position as illegal

It can be tricky to write support for chess960 when using vanilla chess.js. It took me a few days of writing hacks.
It would be somewhat easier if chess.js had an option to allow illegal game states, but the author doesn't seem interested in adding that. Some variants are virtually impossible to implement because of this, such as variants where two kings are adjacent or kings are missing. I will most likely patch chess.js to allow illegal game states in my interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants