From b3ffa54a8eb8927fe3a5d9031ac93f59ed722e8d Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Mon, 28 Dec 2020 19:21:13 -0500 Subject: [PATCH] Check for check --- src/index.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 38c16c3..2c31cf8 100644 --- a/src/index.js +++ b/src/index.js @@ -280,6 +280,35 @@ 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); + } + } + + 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 true; + } else if(moves[j][0] === whiteKing[0] && moves[j][1] === whiteKing[1]) { + return true; + } + } + } + + return false; + } + + checkmate() { + return false; + } + getValidMoves(source) { let [x, y] = this.getXandY(source); console.log([x, y]); @@ -376,9 +405,10 @@ class Board extends React.Component { render() { const winner = calculateWinner(this.state.squares); + let checkMsg = this.inCheck() ? "Check! " : ""; let status = winner ? 'Winner: ' + winner : - (this.state.blackIsNext ? 'Black' : 'White') + "'s Turn"; + checkMsg + (this.state.blackIsNext ? 'Black' : 'White') + "'s Turn"; var texttext =
@@ -391,6 +421,7 @@ class Board extends React.Component { {this.row(5)} {this.row(6)} {this.row(7)} + {this.inCheck()}
;