Added en passant. Moves now objects, not arrays
This commit is contained in:
parent
0c2d99e762
commit
e121755d29
|
@ -1,7 +1,7 @@
|
||||||
# QuickChess
|
# QuickChess
|
||||||
|
|
||||||
This is a simple implementation of Chess in React. It supports nearly all moves,
|
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
|
[https://sagev.space/quickchess/](https://sagev.space/quickchess/). Assets
|
||||||
borrowed from Wikipedia.
|
borrowed from Wikipedia.
|
||||||
|
|
||||||
|
|
50
src/index.js
50
src/index.js
|
@ -87,6 +87,7 @@ class Piece {
|
||||||
constructor(color, type) {
|
constructor(color, type) {
|
||||||
this.color = color;
|
this.color = color;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
this.passantable = false;
|
||||||
}
|
}
|
||||||
setType(type) {
|
setType(type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
@ -103,7 +104,6 @@ class Board extends React.Component {
|
||||||
heldPiece: null,
|
heldPiece: null,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
this.passantable = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setHand(hand) {
|
setHand(hand) {
|
||||||
|
@ -189,11 +189,11 @@ class Board extends React.Component {
|
||||||
var moves = [];
|
var moves = [];
|
||||||
let tryAddMove = (x, y) => {
|
let tryAddMove = (x, y) => {
|
||||||
if (x >= 0 && x < 8 && y >= 0 && y < 8 && this.pieceAt(x, y) == null) {
|
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
|
// Keep searching
|
||||||
return 0;
|
return 0;
|
||||||
} else if (this.isEnemyOf(piece, x, y)) {
|
} else if (this.isEnemyOf(piece, x, y)) {
|
||||||
moves.push([x, y]);
|
moves.push({x: x, y: y});
|
||||||
}
|
}
|
||||||
// Stop searching
|
// Stop searching
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -204,26 +204,26 @@ class Board extends React.Component {
|
||||||
let shift = pieceIsBlack ? -1 : 1;
|
let shift = pieceIsBlack ? -1 : 1;
|
||||||
let startLine = pieceIsBlack ? 6 : 1;
|
let startLine = pieceIsBlack ? 6 : 1;
|
||||||
|
|
||||||
if (this.passantable) {
|
// Check for en passant
|
||||||
let left = this.pieceAt(x - 1, y);
|
let left = this.pieceAt(x - 1, y);
|
||||||
let right = this.pieceAt(x + 1, y);
|
let right = this.pieceAt(x + 1, y);
|
||||||
if(left != null && left.color !== piece.color) {
|
if (left != null && left.passantable && left.color !== piece.color) {
|
||||||
moves.push([x - 1, y + shift]/*.passant=[x - 1, y]*/)
|
moves.push({x:x - 1, y:y + shift, passant: {x: x - 1, y: y}})
|
||||||
}
|
}
|
||||||
if(right != null && right.color !== piece.color) {
|
if (right != null && right.passantable && right.color !== piece.color) {
|
||||||
moves.push([x + 1, y + shift]/*.passant=[x + 1, y]*/)
|
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) {
|
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) {
|
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 => {
|
[x + 1, x - 1].forEach(x => {
|
||||||
if (x >= 0 && x < 8 && this.isEnemyOf(piece, x, y + shift)) {
|
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)) {
|
} else if (isRook(piece)) {
|
||||||
|
@ -329,7 +329,7 @@ class Board extends React.Component {
|
||||||
if(this.isEnemyOf(piece, x, y)) {
|
if(this.isEnemyOf(piece, x, y)) {
|
||||||
let moves = this.getValidMoves(i);
|
let moves = this.getValidMoves(i);
|
||||||
for(var j = 0; j < moves.length; j++) {
|
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;
|
return piece;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -366,7 +366,7 @@ class Board extends React.Component {
|
||||||
// For each move of the above piece
|
// For each move of the above piece
|
||||||
let moves = board.getValidMoves(i)
|
let moves = board.getValidMoves(i)
|
||||||
for(var j = 0; j < moves.length; j++) {
|
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[x + (y * 8)] = board.state.squares[i];
|
||||||
board.state.squares[i] = null;
|
board.state.squares[i] = null;
|
||||||
let check = board.inCheck(checkedKing);
|
let check = board.inCheck(checkedKing);
|
||||||
|
@ -393,7 +393,8 @@ class Board extends React.Component {
|
||||||
let [destX, destY] = this.getXandY(dest);
|
let [destX, destY] = this.getXandY(dest);
|
||||||
|
|
||||||
for (var move of this.getValidMoves(source)) {
|
for (var move of this.getValidMoves(source)) {
|
||||||
let [x, y] = move;
|
let x = move.x;
|
||||||
|
let y = move.y;
|
||||||
if (destX === x && destY === y) {
|
if (destX === x && destY === y) {
|
||||||
return move;
|
return move;
|
||||||
}
|
}
|
||||||
|
@ -414,8 +415,17 @@ class Board extends React.Component {
|
||||||
const squares = this.state.squares.slice();
|
const squares = this.state.squares.slice();
|
||||||
let move = this.isValidMove(from, to)
|
let move = this.isValidMove(from, to)
|
||||||
if (move) {
|
if (move) {
|
||||||
if (move.passant != null) {
|
if (move.passant) {
|
||||||
squares[move.passant[0] + (move.passant[1] * 8)] = null;
|
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];
|
let y = this.getXandY(to)[1];
|
||||||
squares[to] = squares[from];
|
squares[to] = squares[from];
|
||||||
|
|
Loading…
Reference in New Issue