oakmac / chessboardjs

JavaScript chessboard
https://chessboardjs.com
MIT License
2k stars 406 forks source link

about onDragStart #222

Open jimmyyzr opened 3 years ago

jimmyyzr commented 3 years ago

Hi, I am trying to understand the code in onDragStart Preven Drag. I specifically want to ask what does piece.search(/^w/) mean? Is there any document that I can read? I am right now having a bug that when the player is in check, he cannot make any move. atthattime game.turn() === 'w' and piece.search(/^w/) === 0. Below is my onDragStart function. Could you please help me understanding this? Thank you so much!

var onDragStart = function(source, piece, position, orientation) {
  console.log("game turn: ", game.turn());
  console.log("player state: ", PlayerState);
  console.log("player turn: ", PlayerTurn);
  if (game.game_over() === true ||
      (game.turn() === 'w' && piece.search(/^b/) !== -1) ||
      (game.turn() === 'b' && piece.search(/^w/) !== -1) ||
      PlayerState === 'IDLE') {
    console.log("move forbidden");
    console.log("piece search b", piece.search(/^b/));
    console.log("piece search w", piece.search(/^w/));
    return false;
  }
};
banhmisg9509 commented 2 years ago

"I specifically want to ask what does piece.search(/^w/) mean? " It checks every string which starts with "w". That's mean It checks whether It was a white piece or not, for example wK, w,Q, wN, wB, wR, wP,