From 4d927e18661ff63b43d9616fe26dc43d40306e0e Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Wed, 30 Dec 2020 16:56:57 -0500 Subject: [PATCH] More clean-up and correctness. More castling work --- src/index.js | 115 +++++++++++++++++++++++++++------------------------ 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/src/index.js b/src/index.js index 5ad4948..25cc578 100644 --- a/src/index.js +++ b/src/index.js @@ -28,6 +28,10 @@ const Images = [ './black_king.svg', ]; +function range(n) { + return Array.from(Array(n).keys()); +} + function isBlack(piece) { return piece != null && piece.color === BLACK; } @@ -231,13 +235,13 @@ class Board extends React.Component { } if (isPawn(piece)) { - let pieceIsBlack = isBlack(piece); - let shift = pieceIsBlack ? -1 : 1; - let startLine = pieceIsBlack ? 6 : 1; + const pieceIsBlack = isBlack(piece); + const shift = pieceIsBlack ? -1 : 1; + const startLine = pieceIsBlack ? 6 : 1; // Check for en passant - let left = this.pieceAt(x - 1, y); - let right = this.pieceAt(x + 1, y); + const left = this.pieceAt(x - 1, y); + const right = this.pieceAt(x + 1, y); if (left != null && left.passantable && left.color !== piece.color) { moves.push({x: x - 1, y: y + shift, passant: {x: x - 1, y}}) } @@ -268,28 +272,40 @@ class Board extends React.Component { addBunch(n => {return x + n;}, n => {return y - n;}); addBunch(n => {return x - n;}, n => {return y - n;}); } else if (isQueen(piece)) { - let [rook, bishop] = + const [rook, bishop] = [new Piece(piece.color, ROOK), new Piece(piece.color, BISHOP)]; moves = moves.concat(this.getValidMovesAt(rook, x, y)); moves = moves.concat(this.getValidMovesAt(bishop, x, y)); } else if (isKnight(piece)) { - let deltas = [ + [ [2, 1], [2, -1], [-2, 1], [-2, -1], [1, 2], [1, -2], [-1, 2], [-1, -2], - ]; - deltas.forEach(delta => tryAddMove(x + delta[0], y + delta[1])); + ].forEach(delta => tryAddMove(x + delta[0], y + delta[1])); } else if (isKing(piece)) { - let deltas = [ - [1, 1], [1, -1], [-1, 1], [-1, -1], [0, 1], [0, -1], [1, 0], [-1, 0] - ]; - deltas.forEach(delta => tryAddMove(x + delta[0], y + delta[1])); + [[1, 1], [1, -1], [-1, 1], [-1, -1], [0, 1], [0, -1], [1, 0], [-1, 0]] + .forEach(delta => tryAddMove(x + delta[0], y + delta[1])); + if (piece.moves === 0) { + const [x, y] = this.getXandY(this.findIndex(piece)); + let leftRook = this.pieceAt(0, y); + if(isRook(leftRook) && leftRook.moves === 0) { + // Check if between space puts king in check + // Check if spaces between rook and king are empty + // add move(x: x - 2, y, castle: [x, y]) + } + let rightRook = this.pieceAt(7, y); + if(isRook(rightRook) && rightRook.moves === 0) { + // Check if between space puts king in check + // Check if spaces between rook and king are empty + // add move(x: x + 2, y, castle: [x, y]) + } + } } return moves; } findIndex(piece) { for(let i = 0; i < this.squareCount(); i++) { - let check = this.state.squares[i]; + const check = this.state.squares[i]; if(check && check.type === piece.type && check.color === piece.color) { return i; } @@ -298,8 +314,8 @@ class Board extends React.Component { } distanceBetween(i1, i2) { - let [pos1X, pos1Y] = this.getXandY(i1); - let [pos2X, pos2Y] = this.getXandY(i2); + const [pos1X, pos1Y] = this.getXandY(i1); + const [pos2X, pos2Y] = this.getXandY(i2); let a = pos1X - pos2X; a = a * a; @@ -311,12 +327,12 @@ class Board extends React.Component { } inCheck(piece) { - let kingPos = this.getXandY(this.findIndex(piece)); + const kingPos = this.getXandY(this.findIndex(piece)); for(let i = 0; i < this.squareCount(); i++) { - let [x, y] = this.getXandY(i); + const [x, y] = this.getXandY(i); if(this.isEnemyOf(piece, x, y)) { - let moves = this.getValidMoves(i); + const moves = this.getValidMoves(i); for(let j = 0; j < moves.length; j++) { if(moves[j].x === kingPos[0] && moves[j].y === kingPos[1]) { return piece; @@ -329,26 +345,26 @@ class Board extends React.Component { } whoInCheck() { - let blackKing = this.inCheck(new Piece(BLACK, KING)); + const blackKing = this.inCheck(new Piece(BLACK, KING)); return blackKing ? blackKing : this.inCheck(new Piece(WHITE, KING)); } checkmate() { - let checkedKing = this.whoInCheck(); + const checkedKing = this.whoInCheck(); if (checkedKing != null) { // For each square for(let i = 0; i < this.squareCount(); i++) { - let piece = this.squareAt(i); + const piece = this.squareAt(i); // If that piece is on the checked team if (piece != null && isBlack(piece) === isBlack(checkedKing)) { // For each move of the above piece - let moves = this.getValidMoves(i) - for(let move of moves) { + const moves = this.getValidMoves(i) + for(const move of moves) { // Copy the board - let board = this.clone(); + const board = this.clone(); board.state.squares[board.getIndex(move.x, move.y)] = board.state.squares[i]; board.state.squares[i] = null; - let check = board.inCheck(checkedKing); + const check = board.inCheck(checkedKing); if (check == null || check.color !== checkedKing.color) { return false; } @@ -362,16 +378,16 @@ class Board extends React.Component { } getValidMoves(source) { - let [x, y] = this.getXandY(source); + const [x, y] = this.getXandY(source); - let piece = this.squareAt(source); + const piece = this.squareAt(source); return this.getValidMovesAt(piece, x, y); } isValidMove(source, dest) { - let [destX, destY] = this.getXandY(dest); + const [destX, destY] = this.getXandY(dest); - for (let move of this.getValidMoves(source)) { + for (const move of this.getValidMoves(source)) { if (destX === move.x && destY === move.y) { return move; } @@ -390,7 +406,7 @@ class Board extends React.Component { makeMove(from, to) { const squares = this.state.squares.slice(); - let move = this.isValidMove(from, to) + const move = this.isValidMove(from, to) if (move) { if (move.passant) { squares[this.getIndex(move.passant.x, move.passant.y)] = null; @@ -404,7 +420,7 @@ class Board extends React.Component { if (move.passantable) { squares[from].passantable = true; } - let y = this.getXandY(to)[1]; + const y = this.getXandY(to)[1]; squares[to] = squares[from]; squares[from] = null; if (squares[to].type === PAWN && (y === 0 || y === 7)) { @@ -433,7 +449,7 @@ class Board extends React.Component { board.state.squares[i] = board.state.squares[board.heldPiece()]; board.state.squares[this.heldPiece()] = null; - let moversKing = this.state.blackIsNext ? + const moversKing = this.state.blackIsNext ? new Piece(BLACK, KING) : new Piece(WHITE, KING); if (board.inCheck(moversKing) != null) { return; @@ -444,7 +460,7 @@ class Board extends React.Component { }); } } else if (this.state.squares[i] != null) { - let isSquareBlack = isBlack(this.state.squares[i]); + const isSquareBlack = isBlack(this.state.squares[i]); if(isSquareBlack === this.state.blackIsNext) { this.setHand({ heldPiece: i, @@ -454,10 +470,8 @@ class Board extends React.Component { } renderSquare(i) { - let plainBg = (i + (Math.floor(i / 8))) % 2 === 0 ? - "white" : "#666"; - let bgColor = this.heldPiece() === i ? - "#5D98E6" : plainBg; + const plainBg = (i + (Math.floor(i / 8))) % 2 === 0 ? "white" : "#666"; + const bgColor = this.heldPiece() === i ? "#5D98E6" : plainBg; return ( { - [0, 1, 2, 3, 4, 5, 6, 7] - .map(n => this.renderSquare(n + i)) - } +
+ {range(8).map(n => this.renderSquare(n + i))} +
); } render() { - let checkMsg = this.whoInCheck() ? "Check! " : ""; - let isCheckmate = this.checkmate(); - let namedPlayer = isCheckmate ? + const checkMsg = this.whoInCheck() ? "Check! " : ""; + const isCheckmate = this.checkmate(); + const namedPlayer = isCheckmate ? !this.state.blackIsNext : this.state.blackIsNext - let color = namedPlayer ? 'Black' : 'White'; + const color = namedPlayer ? 'Black' : 'White'; + const status = isCheckmate ? "Checkmate! " + color + " Wins!" : checkMsg + color + "'s Turn"; - let texttext = + return (

{status}

- {[0, 1, 2, 3, 4, 5, 6, 7] - .map(n => this.row(n))} + {range(8).map(n => this.row(n))}
- ; - - return ( - texttext ); } }