From e121755d2973e9c535f989491a0be486e5c0d481 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Wed, 30 Dec 2020 09:49:29 -0500 Subject: [PATCH] Added en passant. Moves now objects, not arrays --- README.md | 2 +- src/index.js | 50 ++++++++++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 84d84e6..e8312ba 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # QuickChess This is a simple implementation of Chess in React. It supports nearly all moves, -with en passant and castling still to be added. A live build can be seen here: +with castling still to be added. A live build can be seen here: [https://sagev.space/quickchess/](https://sagev.space/quickchess/). Assets borrowed from Wikipedia. diff --git a/src/index.js b/src/index.js index 07ff761..bf96000 100644 --- a/src/index.js +++ b/src/index.js @@ -87,6 +87,7 @@ class Piece { constructor(color, type) { this.color = color; this.type = type; + this.passantable = false; } setType(type) { this.type = type; @@ -103,7 +104,6 @@ class Board extends React.Component { heldPiece: null, }, }; - this.passantable = false; } setHand(hand) { @@ -189,11 +189,11 @@ class Board extends React.Component { var moves = []; let tryAddMove = (x, y) => { if (x >= 0 && x < 8 && y >= 0 && y < 8 && this.pieceAt(x, y) == null) { - moves.push([x, y]); + moves.push({x: x, y: y}); // Keep searching return 0; } else if (this.isEnemyOf(piece, x, y)) { - moves.push([x, y]); + moves.push({x: x, y: y}); } // Stop searching return 1; @@ -204,26 +204,26 @@ class Board extends React.Component { let shift = pieceIsBlack ? -1 : 1; let startLine = pieceIsBlack ? 6 : 1; - if (this.passantable) { - let left = this.pieceAt(x - 1, y); - let right = this.pieceAt(x + 1, y); - if(left != null && left.color !== piece.color) { - moves.push([x - 1, y + shift]/*.passant=[x - 1, y]*/) - } - if(right != null && right.color !== piece.color) { - moves.push([x + 1, y + shift]/*.passant=[x + 1, y]*/) - } + // Check for en passant + let left = this.pieceAt(x - 1, y); + let 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: y}}) + } + if (right != null && right.passantable && right.color !== piece.color) { + moves.push({x: x + 1, y: y + shift, passant: {x: x + 1, y: y}}) } + // Pawn moving two spaces becomes en-passantable if (this.pieceAt(x, y + shift) == null) { - moves.push([x, y + shift]); + moves.push({x: x, y: y + shift}); if (y === startLine && this.pieceAt(x, y + (shift * 2)) == null) { - moves.push([x, y + (shift * 2)]/*.passantable=true*/); + moves.push({x: x, y: y + (shift * 2), passantable: true}); } } [x + 1, x - 1].forEach(x => { if (x >= 0 && x < 8 && this.isEnemyOf(piece, x, y + shift)) { - moves.push([x, y + shift]); + moves.push({x: x, y: y + shift}); } }); } else if (isRook(piece)) { @@ -329,7 +329,7 @@ class Board extends React.Component { 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]) { + if(moves[j].x === kingPos[0] && moves[j].y === kingPos[1]) { return piece; } } @@ -366,7 +366,7 @@ class Board extends React.Component { // For each move of the above piece let moves = board.getValidMoves(i) for(var j = 0; j < moves.length; j++) { - let [x, y] = moves[j]; + let [x, y] = [moves[j].x, moves[j].y]; board.state.squares[x + (y * 8)] = board.state.squares[i]; board.state.squares[i] = null; let check = board.inCheck(checkedKing); @@ -393,7 +393,8 @@ class Board extends React.Component { let [destX, destY] = this.getXandY(dest); for (var move of this.getValidMoves(source)) { - let [x, y] = move; + let x = move.x; + let y = move.y; if (destX === x && destY === y) { return move; } @@ -414,8 +415,17 @@ class Board extends React.Component { const squares = this.state.squares.slice(); let move = this.isValidMove(from, to) if (move) { - if (move.passant != null) { - squares[move.passant[0] + (move.passant[1] * 8)] = null; + if (move.passant) { + squares[move.passant.x + (move.passant.y * 8)] = null; + } + // Remove existing passantable states + for(var square of squares) { + if (square) { + square.passantable = false; + } + } + if (move.passantable) { + squares[from].passantable = true; } let y = this.getXandY(to)[1]; squares[to] = squares[from];