oakmac / chessboardjs

JavaScript chessboard
https://chessboardjs.com
MIT License
2.01k stars 408 forks source link

Overriding chess rules, programming valid moves #61

Closed kernc closed 10 years ago

kernc commented 10 years ago

Hi,

Could you make valid piece moves programmable? E.g.

pieces['typeX'].is_valid_move = function(from, to, board) { ... };

Or something like that. This way, more variations of chess are made implementable.

oakmac commented 10 years ago

This sort of thing is accomplishable through the API and outside the scope of chessboard.js

kernc commented 10 years ago

Sorry, I can't see on first glance how this is accomplishable through the API. Will the chessboard consider the move invalid, and revert it, if I return false on onChange event? Would this be simple to implement and document? I understand one could probably override the Game.moves and Game.move functions once the API is pronounced stable. Would that work?

oakmac commented 10 years ago

I think you're looking for the onDrop method, which allows you to 'snapback' or 'trash' a piece when it is dropped. See Examples 4004, 4005, 4006.

If you'd like to prevent a user from picking up an invalid piece you can return false from the onDragStart method like in Example 4002.

Keep in mind that chessboard.js (purposefully) doesn't understand the notion of "valid moves". If you're working with regular chess you can pair it with chess.js ala Example 5000. If you're trying to integrate with a different variant of chess you'll need the spiritual equivalent of chess.js for that particular variant (legal move validation, tracking game state, etc). As long as that variant of chess uses regular chess pieces and plays on an 8x8 board, chessboard.js should be able to handle it. If not, that's either a separate issue or possibly a different library / fork. (Here is an example of chessboard.js being used for something that is not "regular chess".)