2

I have my own pgn parser which supports n parallel variation and nested variation.

I have written a wrapper to use chessboard.js with it.

I am facing two issues

  1. Removing pawn after en passant move.
  2. changing piece after pawn promotion.

These two issues have same common root that is to replace the piece on a particular position on board. One solution seems to provide FEN to board object and set it.

didn't find any thing in documentation. I want some other elegant solution where in I can replace a particular piece type any suggestions will be helpful.

EDIT It seems that the only way to achieve piece replacement for en passant according to example is

// for castling, en passant, pawn promotion
var onSnapEnd = function() {
  board.position(game.fen());// re positioning board by providing FEN
};   

Providing board.move("<en passant>") carries out the move but does not remove the pawn which is actually ahead of the destination position.

amar
  • 4,285
  • 8
  • 40
  • 52
  • 2
    relevant: https://github.com/oakmac/chessboardjs/issues/43 – DLeh Jun 10 '15 at 18:24
  • 1
    Here is the thing about this type of question. It basically is asking for someone who already has a working implementation of chessboard.js to test your claims against with custom written code. The thing is, that is a huge waste of time for someone or a group of people to do. Why should they have to prepare code for you just to test your assertion? Please include a working demo in situations like this to both save everyone time and also get a working solution. If you reproduce it, the community will more than likely solve it. – Travis J Jun 10 '15 at 18:31
  • @DLeh thank you for the right pointer. – amar Jun 10 '15 at 18:41
  • 1
    @amar - Clearly from your response you are just a rude entitled user of the site who expects too much from others. You have included nothing here to work on and the only relevant response was a link that you should have found by googling, not by asking the community. In the future, ask questions which contain either reproducible examples or specific programming problems. If you are not capable of "some other elegant solution where in I can replace a particular piece type" then perhaps you should hire a developer. – Travis J Jun 10 '15 at 18:47
  • @TravisJ i will definitely ask your expert opinion next time :) chill – amar Jun 10 '15 at 18:56
  • @DLeh is there some another documentation available for the API,other then the concise one on web site? – amar Jun 10 '15 at 19:07
  • 1
    no idea. i just found that when i searched for your question, i know nothing about chessboard.js – DLeh Jun 10 '15 at 19:08

2 Answers2

1

To move any pieces you have to register the onDrop handler when initializing the chessboard js object. This function should return:

  • true (boolean) for a valid move
  • 'trash' (string) if you want the dropped piece to be removed from the board (eg for pawn promotion)
  • 'snapback' (string) if the move is invalid (will return the piece to it's original square

When you return any of these values from onDrop the board will update accordingly.

Note that handling promotion is tricky as you need to select a piece to promote to.

Anytime you want to update the board (eg once user has selected a promotion piece) just use a new fen to do so, eg:

board.position(fen);
omarjebari
  • 4,861
  • 3
  • 34
  • 32
0

Try to add promotion: "q" to your on drag function like these:

  game.current.move({
    from: sourceSquare,
    to: targetSquare,
    promotion: "q",
  });
  setFen(game.current.fen());
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77