ajax333221 / isepic-chess

♞ JavaScript chess utility library.
MIT License
16 stars 3 forks source link

How to undo “MockMove” #37

Closed user1704 closed 2 years ago

ajax333221 commented 2 years ago

You don't need to undo a mock move, it should not have affected a board when it was used, you simply get the returned value as if the move(s) got played.

The way it works is that the move or moves get played on a temporal copy of the board, then we save the returned value without a reference, and then remove the temporal board, but the original board where it was called from should remain unchanged, there is no need to undo anything :)

You can quickly test it on the demo and sending on the console Ic.getBoard("main").playMove("Nc3", {isMockMove: true}), no move will be sent but you will get:

{
  "colorMoved": "w",
  "colorToPlay": "b",
  "fen": "rnbqkbnr/pppppppp/8/8/8/2N5/PPPPPPPP/R1BQKBNR b KQkq - 1 1",
  "san": "Nc3",
  "uci": "b1c3",
  "fromBos": "b1",
  "toBos": "c3",
  "enPassantBos": "",
  "piece": "n",
  "captured": "",
  "promotion": "",
  "comment": "",
  "moveResult": "",
  "canDraw": false,
  "isEnPassantCapture": false
}