Check for check

This commit is contained in:
Sage Vaillancourt 2020-12-28 19:21:13 -05:00
parent 7041543bd0
commit b3ffa54a8e
1 changed files with 32 additions and 1 deletions

View File

@ -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 =
<div style={{textAlign: `center`,}}>
@ -391,6 +421,7 @@ class Board extends React.Component {
{this.row(5)}
{this.row(6)}
{this.row(7)}
{this.inCheck()}
</div>
;