cm-chessboard icon indicating copy to clipboard operation
cm-chessboard copied to clipboard

Castling on a chess960 pos doesnt work

Open LocutusOfPenguin opened this issue 8 months ago • 6 comments

Hi,

i did it similar to examples/enable-input.html As an example i used this fen:

// das kam von LiChess::: //const fen960 = "qnbbrkrn/pppppppp/8/8/8/8/PPPPPPPP/QNBBRKRN w KQkq - 0 1"; // das muss eigentlich so sein::: //const fen960 = "bbqnnrkr/pppppppp/8/8/8/8/PPPPPPPP/BBQNNRKR w FHfh - 0 1";

The problem is: when i click the (w) king, i get the rook as "possible TO field", but when i click the rook afterwards, i get a "moveInputCanceled" event , and nothing works.

So, i think, there should be some sort of option to accept the own piece as TO.

That would be similar: standard game to play a castle by clicking the king & rook, instead of the C/G file.

LocutusOfPenguin avatar Mar 20 '25 11:03 LocutusOfPenguin

Yes Freestyle Chess (960) is a problem because the input of castling is different than in standard chess. There is no trivial solution to this. Normally if you click another piece you want to cancel the input. The not canceling must only occur in castling in Freestyle (960), and only if the rook is clicked after the king. I am not sure how to solve this elegant, any ideas? AFAIK castling in chess 960 is handled different in chess.com and lichess. What is the best way? And what does that mean for the configuration and code?

shaack avatar Mar 20 '25 12:03 shaack

the simple solution for YOU would be a Flag at startup to enable two pieces clicked and the TRY to send a move finished event instead of a cancel one , or both send.

My problem is NOT that rules are different, fen codes are wrong etc...I can do this all in this "example-input", just instead of chess.js some better lib. My problem is: i dont even get a chance to accept a move.

LocutusOfPenguin avatar Mar 20 '25 12:03 LocutusOfPenguin

or send the old square and the new square at cancel...i only need the (2) squares, and im fine (the rest i can do inside the js code , similar to your example one)

HMMM....i took a CLOSER look at the code. I DO get these two squares...Perhaps thats enough for me.

console.log's cmBoard event:moveInputCanceled Rochade? {"from":"f1","to":"g1"} cmBoard event:moveInputStarted cmBoard event:moveInputFinished

So, i can filter the castle out on "cancel" event, the last two events are not good then (coming from clicking on rook), but since i update the board afterwards, these changes (like dots for the rook available To-squares), not harm me. I'll see!

LocutusOfPenguin avatar Mar 20 '25 13:03 LocutusOfPenguin

OK, that works.... Incl. on a standard game when i click the king & rook. Its abit ODD , that i need to do this on a "cancel" event, but after all its only me seeing the code inside ;-)

You can close this issue, if you want (=ignoring odd).

LocutusOfPenguin avatar Mar 20 '25 15:03 LocutusOfPenguin

Nice. Which library are you using instead of chess.js to handle the correct 960 rulers?

shaack avatar Mar 20 '25 17:03 shaack

This one: https://github.com/yo35/kokopu since there is no progress on my issue: https://github.com/jhlywa/chess.js/issues/122

kokopu works very well (for me)

LocutusOfPenguin avatar Mar 20 '25 21:03 LocutusOfPenguin

In Chess960 there are positions where the king can be moved to the same square where it is located in order to castle. Castling is thus possible by double-clicking on the square where the king is currently located.

import { COLOR, INPUT_EVENT_TYPE, MARKER_TYPE } from '@chesslablab/chessboard';
import AbstractWebSocket from '../AbstractWebSocket.js';
import chessboard from '../../pages/chessboard.js';

export default class AbstractGameWebSocket extends AbstractWebSocket {
  static PORT = 8443;

  chessboard;

  constructor() {
    super();
    this.chessboard = chessboard;
  }

  async connect() {
    await super.connect(AbstractGameWebSocket.PORT);
    this.socket.onmessage = (res) => {
      const data = JSON.parse(res.data);
      this.response[Object.keys(data)[0]] = Object.values(data)[0];
    };
  }

  inputHandler(event) {
    if (event.type === INPUT_EVENT_TYPE.movingOverSquare) {
      return;
    }
    if (event.type !== INPUT_EVENT_TYPE.moveInputFinished) {
      event.chessboard.removeMarkers(MARKER_TYPE.dot);
      event.chessboard.removeMarkers(MARKER_TYPE.bevel);
    }
    if (event.type === INPUT_EVENT_TYPE.moveInputStarted) {
      this.send('/legal', {
        square: event.square
      });
      return true;
    } else if (event.type === INPUT_EVENT_TYPE.validateMoveInput) {
      this.send('/play_lan', {
        color: event.piece.charAt(0),
        lan: event.squareFrom + event.squareTo
      });
      return true;
    } else if (event.type === INPUT_EVENT_TYPE.moveInputCanceled) {
      /**
       * In Chess960 there are positions where the king can be moved to the
       * same square where it is located in order to castle. Castling is
       * thus possible by double-clicking on the square where the king is
       * currently located.
       */
      const piece = event.chessboard.state.position.getPiece(event.squareFrom);
      if (piece.charAt(1) === 'k' && event.squareTo === null) {
        this.send('/play_lan', {
          color: piece.charAt(0),
          lan: event.squareFrom + event.squareFrom
        });
      }
      return true;
    }
  }

  end() {
    chessboard.state.inputWhiteEnabled = false;
    chessboard.state.inputBlackEnabled = false;
  }
}

rfc1123 avatar Jun 24 '25 10:06 rfc1123