From 0a721d111a0f55b2a196f7d10918ccd7cf3a3a50 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Tue, 29 Dec 2020 08:25:17 -0500 Subject: [PATCH] Checkmate framework in place --- src/index.js | 118 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 82 insertions(+), 36 deletions(-) diff --git a/src/index.js b/src/index.js index 520c855..7e1bc67 100644 --- a/src/index.js +++ b/src/index.js @@ -223,7 +223,6 @@ class Board extends React.Component { } } } else if (isBishop(piece)) { - console.log("Bishop"); // Down-Right for (i = 1; i < 8; i++) { if (tryAddMove(x + i, y + i) !== 0) { @@ -254,7 +253,6 @@ class Board extends React.Component { [Pieces.WhiteRook, Pieces.WhiteBishop]; moves = moves.concat(this.getValidMovesAt(rook, x, y)); moves = moves.concat(this.getValidMovesAt(bishop, x, y)); - console.log(moves); } else if (isKnight(piece)) { tryAddMove(x + 2, y + 1); tryAddMove(x + 2, y - 1); @@ -278,24 +276,22 @@ class Board extends React.Component { return moves; } - inCheck() { - var blackKing = null; - var whiteKing = null; - for(var i = 0; i < this.state.squares.length; i++) { - if(this.state.squares[i] === Pieces.BlackKing) { - blackKing = this.getXandY(i); - } else if(this.state.squares[i] === Pieces.WhiteKing) { - whiteKing = this.getXandY(i); + inCheck(piece) { + var kingPos; + for(var i = 0; i < this.squareCount(); i++) { + if(this.state.squares[i] === piece) { + kingPos = this.getXandY(i); } } - for(i = 0; i < this.state.squares.length; i++) { - let moves = this.getValidMoves(i); - for(var j = 0; j < moves.length; j++) { - if(moves[j][0] === blackKing[0] && moves[j][1] === blackKing[1]) { - return Pieces.BlackKing; - } else if(moves[j][0] === whiteKing[0] && moves[j][1] === whiteKing[1]) { - return Pieces.WhiteKing; + for(i = 0; i < this.squareCount(); i++) { + let [x, y] = this.getXandY(i); + if(this.isEnemyOf(piece, x, y)) { + let moves = this.getValidMoves(i); + for(var j = 0; j < moves.length; j++) { + if(moves[j][0] === kingPos[0] && moves[j][1] === kingPos[1]) { + return piece; + } } } } @@ -303,13 +299,57 @@ class Board extends React.Component { return null; } + whoInCheck() { + let blackKing = this.inCheck(Pieces.BlackKing); + return blackKing ? blackKing : this.inCheck(Pieces.WhiteKing); + } + + squareCount() { + return this.state.squares.length; + } + + squareAt(i) { + return i >= 0 && i < 64 ? this.state.squares[i] : null; + } + checkmate() { + let checkedKing = this.whoInCheck(); + if (checkedKing != null) { + // For piece in board + // If piece is on the checked team, + // Copy the board + // Make one of the piece's moves on the board + // Return false if the king is no longer in check + + // For each square + console.log("Square count: " + this.squareCount()); + for(var i = 0; i < this.squareCount(); i++) { + let piece = this.squareAt(i); + // If that piece is on the checked team + if (piece != null && isBlack(piece) === isBlack(checkedKing)) { + console.log("Trying moves for " + Images[piece]) + // Copy the board + var board = new Board(); + board.state.squares = this.state.squares.slice(); + // For each move of the above piece + let moves = board.getValidMoves(i) + for(var j = 0; j < moves.length; j++) { + let [from, to] = moves[j]; + board.makeMove(from, to); + if (board.inCheck(checkedKing) !== checkedKing) { + return false; + } + } + } + } + return true; + } + return false; } getValidMoves(source) { let [x, y] = this.getXandY(source); - console.log([x, y]); let piece = this.state.squares[source]; return this.getValidMovesAt(piece, x, y); @@ -320,7 +360,6 @@ class Board extends React.Component { let validMoves = this.getValidMoves(source); if (validMoves == null) { - console.log("Null moves"); return false; } @@ -342,28 +381,35 @@ class Board extends React.Component { return this.state.hand.heldPiece; } - handleClick(i) { + makeMove(from, to) { const squares = this.state.squares.slice(); + if (this.isValidMove(from, to)) { + squares[to] = squares[from]; + squares[from] = null; + this.setHand({ + heldPiece: null, + }); + this.setState({ + squares: squares, + blackIsNext: !this.state.blackIsNext, + }); + return 0; + } + return 1; + } + + handleClick(i) { if (this.isHoldingPiece()) { - if (this.isValidMove(this.heldPiece(), i)) { - squares[i] = squares[this.heldPiece()]; - squares[this.heldPiece()] = null; - this.setHand({ - heldPiece: null, - }); - this.setState({ - squares: squares, - blackIsNext: !this.state.blackIsNext, - }); - } else { + if (this.makeMove(this.heldPiece(), i) !== 0) { this.setHand({ heldPiece: null, }); } - } else if (squares[i] != null) { - let isSquareBlack = isBlack(squares[i]); + return; + } + if (this.state.squares[i] != null) { + let isSquareBlack = isBlack(this.state.squares[i]); if(isSquareBlack === this.state.blackIsNext) { - console.log(this.getValidMoves(i)); this.setHand({ heldPiece: i, }); @@ -402,8 +448,8 @@ class Board extends React.Component { } render() { - let checkMsg = this.inCheck() ? "Check! " : ""; - const status = + let checkMsg = this.whoInCheck() ? "Check! " : ""; + const status = this.checkmate() ? "CHECKMATE" : checkMsg + (this.state.blackIsNext ? 'Black' : 'White') + "'s Turn"; var texttext =