Checkmate framework in place
This commit is contained in:
parent
45a109f8f3
commit
0a721d111a
118
src/index.js
118
src/index.js
|
@ -223,7 +223,6 @@ class Board extends React.Component {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (isBishop(piece)) {
|
} else if (isBishop(piece)) {
|
||||||
console.log("Bishop");
|
|
||||||
// Down-Right
|
// Down-Right
|
||||||
for (i = 1; i < 8; i++) {
|
for (i = 1; i < 8; i++) {
|
||||||
if (tryAddMove(x + i, y + i) !== 0) {
|
if (tryAddMove(x + i, y + i) !== 0) {
|
||||||
|
@ -254,7 +253,6 @@ class Board extends React.Component {
|
||||||
[Pieces.WhiteRook, Pieces.WhiteBishop];
|
[Pieces.WhiteRook, Pieces.WhiteBishop];
|
||||||
moves = moves.concat(this.getValidMovesAt(rook, x, y));
|
moves = moves.concat(this.getValidMovesAt(rook, x, y));
|
||||||
moves = moves.concat(this.getValidMovesAt(bishop, x, y));
|
moves = moves.concat(this.getValidMovesAt(bishop, x, y));
|
||||||
console.log(moves);
|
|
||||||
} else if (isKnight(piece)) {
|
} else if (isKnight(piece)) {
|
||||||
tryAddMove(x + 2, y + 1);
|
tryAddMove(x + 2, y + 1);
|
||||||
tryAddMove(x + 2, y - 1);
|
tryAddMove(x + 2, y - 1);
|
||||||
|
@ -278,24 +276,22 @@ class Board extends React.Component {
|
||||||
return moves;
|
return moves;
|
||||||
}
|
}
|
||||||
|
|
||||||
inCheck() {
|
inCheck(piece) {
|
||||||
var blackKing = null;
|
var kingPos;
|
||||||
var whiteKing = null;
|
for(var i = 0; i < this.squareCount(); i++) {
|
||||||
for(var i = 0; i < this.state.squares.length; i++) {
|
if(this.state.squares[i] === piece) {
|
||||||
if(this.state.squares[i] === Pieces.BlackKing) {
|
kingPos = this.getXandY(i);
|
||||||
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++) {
|
for(i = 0; i < this.squareCount(); i++) {
|
||||||
let moves = this.getValidMoves(i);
|
let [x, y] = this.getXandY(i);
|
||||||
for(var j = 0; j < moves.length; j++) {
|
if(this.isEnemyOf(piece, x, y)) {
|
||||||
if(moves[j][0] === blackKing[0] && moves[j][1] === blackKing[1]) {
|
let moves = this.getValidMoves(i);
|
||||||
return Pieces.BlackKing;
|
for(var j = 0; j < moves.length; j++) {
|
||||||
} else if(moves[j][0] === whiteKing[0] && moves[j][1] === whiteKing[1]) {
|
if(moves[j][0] === kingPos[0] && moves[j][1] === kingPos[1]) {
|
||||||
return Pieces.WhiteKing;
|
return piece;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -303,13 +299,57 @@ class Board extends React.Component {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
whoInCheck() {
|
||||||
|
let blackKing = this.inCheck(Pieces.BlackKing);
|
||||||
|
return blackKing ? blackKing : this.inCheck(Pieces.WhiteKing);
|
||||||
|
}
|
||||||
|
|
||||||
|
squareCount() {
|
||||||
|
return this.state.squares.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
squareAt(i) {
|
||||||
|
return i >= 0 && i < 64 ? this.state.squares[i] : null;
|
||||||
|
}
|
||||||
|
|
||||||
checkmate() {
|
checkmate() {
|
||||||
|
let checkedKing = this.whoInCheck();
|
||||||
|
if (checkedKing != null) {
|
||||||
|
// For piece in board
|
||||||
|
// If piece is on the checked team,
|
||||||
|
// Copy the board
|
||||||
|
// Make one of the piece's moves on the board
|
||||||
|
// Return false if the king is no longer in check
|
||||||
|
|
||||||
|
// For each square
|
||||||
|
console.log("Square count: " + this.squareCount());
|
||||||
|
for(var i = 0; i < this.squareCount(); i++) {
|
||||||
|
let piece = this.squareAt(i);
|
||||||
|
// If that piece is on the checked team
|
||||||
|
if (piece != null && isBlack(piece) === isBlack(checkedKing)) {
|
||||||
|
console.log("Trying moves for " + Images[piece])
|
||||||
|
// Copy the board
|
||||||
|
var board = new Board();
|
||||||
|
board.state.squares = this.state.squares.slice();
|
||||||
|
// For each move of the above piece
|
||||||
|
let moves = board.getValidMoves(i)
|
||||||
|
for(var j = 0; j < moves.length; j++) {
|
||||||
|
let [from, to] = moves[j];
|
||||||
|
board.makeMove(from, to);
|
||||||
|
if (board.inCheck(checkedKing) !== checkedKing) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
getValidMoves(source) {
|
getValidMoves(source) {
|
||||||
let [x, y] = this.getXandY(source);
|
let [x, y] = this.getXandY(source);
|
||||||
console.log([x, y]);
|
|
||||||
|
|
||||||
let piece = this.state.squares[source];
|
let piece = this.state.squares[source];
|
||||||
return this.getValidMovesAt(piece, x, y);
|
return this.getValidMovesAt(piece, x, y);
|
||||||
|
@ -320,7 +360,6 @@ class Board extends React.Component {
|
||||||
|
|
||||||
let validMoves = this.getValidMoves(source);
|
let validMoves = this.getValidMoves(source);
|
||||||
if (validMoves == null) {
|
if (validMoves == null) {
|
||||||
console.log("Null moves");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -342,28 +381,35 @@ class Board extends React.Component {
|
||||||
return this.state.hand.heldPiece;
|
return this.state.hand.heldPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
handleClick(i) {
|
makeMove(from, to) {
|
||||||
const squares = this.state.squares.slice();
|
const squares = this.state.squares.slice();
|
||||||
|
if (this.isValidMove(from, to)) {
|
||||||
|
squares[to] = squares[from];
|
||||||
|
squares[from] = null;
|
||||||
|
this.setHand({
|
||||||
|
heldPiece: null,
|
||||||
|
});
|
||||||
|
this.setState({
|
||||||
|
squares: squares,
|
||||||
|
blackIsNext: !this.state.blackIsNext,
|
||||||
|
});
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
handleClick(i) {
|
||||||
if (this.isHoldingPiece()) {
|
if (this.isHoldingPiece()) {
|
||||||
if (this.isValidMove(this.heldPiece(), i)) {
|
if (this.makeMove(this.heldPiece(), i) !== 0) {
|
||||||
squares[i] = squares[this.heldPiece()];
|
|
||||||
squares[this.heldPiece()] = null;
|
|
||||||
this.setHand({
|
|
||||||
heldPiece: null,
|
|
||||||
});
|
|
||||||
this.setState({
|
|
||||||
squares: squares,
|
|
||||||
blackIsNext: !this.state.blackIsNext,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.setHand({
|
this.setHand({
|
||||||
heldPiece: null,
|
heldPiece: null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if (squares[i] != null) {
|
return;
|
||||||
let isSquareBlack = isBlack(squares[i]);
|
}
|
||||||
|
if (this.state.squares[i] != null) {
|
||||||
|
let isSquareBlack = isBlack(this.state.squares[i]);
|
||||||
if(isSquareBlack === this.state.blackIsNext) {
|
if(isSquareBlack === this.state.blackIsNext) {
|
||||||
console.log(this.getValidMoves(i));
|
|
||||||
this.setHand({
|
this.setHand({
|
||||||
heldPiece: i,
|
heldPiece: i,
|
||||||
});
|
});
|
||||||
|
@ -402,8 +448,8 @@ class Board extends React.Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
let checkMsg = this.inCheck() ? "Check! " : "";
|
let checkMsg = this.whoInCheck() ? "Check! " : "";
|
||||||
const status =
|
const status = this.checkmate() ? "CHECKMATE" :
|
||||||
checkMsg + (this.state.blackIsNext ? 'Black' : 'White') + "'s Turn";
|
checkMsg + (this.state.blackIsNext ? 'Black' : 'White') + "'s Turn";
|
||||||
|
|
||||||
var texttext =
|
var texttext =
|
||||||
|
|
Loading…
Reference in New Issue