From 6ca3a810c62b0182096f3305cb82e910afda9551 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Wed, 30 Dec 2020 00:09:02 -0500 Subject: [PATCH] Ban moving self into, or staying in check --- src/index.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index b5f31ba..07ff761 100644 --- a/src/index.js +++ b/src/index.js @@ -440,14 +440,23 @@ class Board extends React.Component { return; } if (this.isHoldingPiece()) { + // Copy the board + var board = new Board(); + board.state.squares = this.state.squares.slice(); + board.state.squares[i] = board.state.squares[this.heldPiece()]; + board.state.squares[this.heldPiece()] = null; + + let moversKing = this.state.blackIsNext ? + new Piece(BLACK, KING) : new Piece(WHITE, KING); + if (board.inCheck(moversKing) != null) { + return; + } if (this.makeMove(this.heldPiece(), i) !== 0) { this.setHand({ heldPiece: null, }); } - return; - } - if (this.state.squares[i] != null) { + } else if (this.state.squares[i] != null) { let isSquareBlack = isBlack(this.state.squares[i]); if(isSquareBlack === this.state.blackIsNext) { this.setHand({