Checkmate framework in place
This commit is contained in:
parent
45a109f8f3
commit
0a721d111a
102
src/index.js
102
src/index.js
|
@ -223,7 +223,6 @@ class Board extends React.Component {
|
|||
}
|
||||
}
|
||||
} else if (isBishop(piece)) {
|
||||
console.log("Bishop");
|
||||
// Down-Right
|
||||
for (i = 1; i < 8; i++) {
|
||||
if (tryAddMove(x + i, y + i) !== 0) {
|
||||
|
@ -254,7 +253,6 @@ class Board extends React.Component {
|
|||
[Pieces.WhiteRook, Pieces.WhiteBishop];
|
||||
moves = moves.concat(this.getValidMovesAt(rook, x, y));
|
||||
moves = moves.concat(this.getValidMovesAt(bishop, x, y));
|
||||
console.log(moves);
|
||||
} else if (isKnight(piece)) {
|
||||
tryAddMove(x + 2, y + 1);
|
||||
tryAddMove(x + 2, y - 1);
|
||||
|
@ -278,24 +276,22 @@ 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);
|
||||
inCheck(piece) {
|
||||
var kingPos;
|
||||
for(var i = 0; i < this.squareCount(); i++) {
|
||||
if(this.state.squares[i] === piece) {
|
||||
kingPos = this.getXandY(i);
|
||||
}
|
||||
}
|
||||
|
||||
for(i = 0; i < this.state.squares.length; i++) {
|
||||
for(i = 0; i < this.squareCount(); i++) {
|
||||
let [x, y] = this.getXandY(i);
|
||||
if(this.isEnemyOf(piece, x, y)) {
|
||||
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 Pieces.BlackKing;
|
||||
} else if(moves[j][0] === whiteKing[0] && moves[j][1] === whiteKing[1]) {
|
||||
return Pieces.WhiteKing;
|
||||
if(moves[j][0] === kingPos[0] && moves[j][1] === kingPos[1]) {
|
||||
return piece;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -303,13 +299,57 @@ class Board extends React.Component {
|
|||
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() {
|
||||
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;
|
||||
}
|
||||
|
||||
getValidMoves(source) {
|
||||
let [x, y] = this.getXandY(source);
|
||||
console.log([x, y]);
|
||||
|
||||
let piece = this.state.squares[source];
|
||||
return this.getValidMovesAt(piece, x, y);
|
||||
|
@ -320,7 +360,6 @@ class Board extends React.Component {
|
|||
|
||||
let validMoves = this.getValidMoves(source);
|
||||
if (validMoves == null) {
|
||||
console.log("Null moves");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -342,12 +381,11 @@ class Board extends React.Component {
|
|||
return this.state.hand.heldPiece;
|
||||
}
|
||||
|
||||
handleClick(i) {
|
||||
makeMove(from, to) {
|
||||
const squares = this.state.squares.slice();
|
||||
if (this.isHoldingPiece()) {
|
||||
if (this.isValidMove(this.heldPiece(), i)) {
|
||||
squares[i] = squares[this.heldPiece()];
|
||||
squares[this.heldPiece()] = null;
|
||||
if (this.isValidMove(from, to)) {
|
||||
squares[to] = squares[from];
|
||||
squares[from] = null;
|
||||
this.setHand({
|
||||
heldPiece: null,
|
||||
});
|
||||
|
@ -355,15 +393,23 @@ class Board extends React.Component {
|
|||
squares: squares,
|
||||
blackIsNext: !this.state.blackIsNext,
|
||||
});
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
handleClick(i) {
|
||||
if (this.isHoldingPiece()) {
|
||||
if (this.makeMove(this.heldPiece(), i) !== 0) {
|
||||
this.setHand({
|
||||
heldPiece: null,
|
||||
});
|
||||
}
|
||||
} else if (squares[i] != null) {
|
||||
let isSquareBlack = isBlack(squares[i]);
|
||||
return;
|
||||
}
|
||||
if (this.state.squares[i] != null) {
|
||||
let isSquareBlack = isBlack(this.state.squares[i]);
|
||||
if(isSquareBlack === this.state.blackIsNext) {
|
||||
console.log(this.getValidMoves(i));
|
||||
this.setHand({
|
||||
heldPiece: i,
|
||||
});
|
||||
|
@ -402,8 +448,8 @@ class Board extends React.Component {
|
|||
}
|
||||
|
||||
render() {
|
||||
let checkMsg = this.inCheck() ? "Check! " : "";
|
||||
const status =
|
||||
let checkMsg = this.whoInCheck() ? "Check! " : "";
|
||||
const status = this.checkmate() ? "CHECKMATE" :
|
||||
checkMsg + (this.state.blackIsNext ? 'Black' : 'White') + "'s Turn";
|
||||
|
||||
var texttext =
|
||||
|
|
Loading…
Reference in New Issue