- isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) {
- for (let step of steps) {
- let rx = x + step[0],
- ry = y + step[1];
- while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
- rx += step[0];
- ry += step[1];
- }
- if (
- V.OnBoard(rx, ry) &&
- this.getPiece(rx, ry) == piece &&
- this.getColor(rx, ry) == color
- ) {
- // Continue some steps in the same direction (pull)
- rx += step[0];
- ry += step[1];
- while (
- V.OnBoard(rx, ry) &&
- this.board[rx][ry] == V.EMPTY &&
- !oneStep
- ) {
- rx += step[0];
- ry += step[1];
- }
- if (!V.OnBoard(rx, ry)) return true;
- // Step in the other direction (push)
- rx = x - step[0];
- ry = y - step[1];
- while (
- V.OnBoard(rx, ry) &&
- this.board[rx][ry] == V.EMPTY &&
- !oneStep
- ) {
- rx -= step[0];
- ry -= step[1];
- }
- if (!V.OnBoard(rx, ry)) return true;
- }
- }
- return false;
- }
-
- isAttackedByPawn([x, y], color) {
- // The king can be pushed out by a pawn on last rank or near the edge
- const pawnShift = (color == "w" ? 1 : -1);
- for (let i of [-1, 1]) {
- if (
- V.OnBoard(x + pawnShift, y + i) &&
- this.board[x + pawnShift][y + i] != V.EMPTY &&
- this.getPiece(x + pawnShift, y + i) == V.PAWN &&
- this.getColor(x + pawnShift, y + i) == color
- ) {
- if (!V.OnBoard(x - pawnShift, y - i)) return true;
- }
- }
- return false;
- }
-
- static OnTheEdge(x, y) {
- return (x == 0 || x == 7 || y == 0 || y == 7);
- }
-
- isAttackedByKing([x, y], color) {
- // Attacked if I'm on the edge and the opponent king just next,
- // but not on the edge.
- if (V.OnTheEdge(x, y)) {
- for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) {
- const [i, j] = [x + step[0], y + step[1]];
- if (
- V.OnBoard(i, j) &&
- !V.OnTheEdge(i, j) &&
- this.board[i][j] != V.EMPTY &&
- this.getPiece(i, j) == V.KING
- // NOTE: since only one king of each color, and (x, y) is occupied
- // by our king, no need to check other king's color.
- ) {
- return true;
- }
- }
- }
- return false;
- }
-
- // No consideration of color: all pieces could be played
- getAllPotentialMoves() {
- let potentialMoves = [];
- for (let i = 0; i < V.size.x; i++) {
- for (let j = 0; j < V.size.y; j++) {
- if (this.board[i][j] != V.EMPTY) {
- Array.prototype.push.apply(
- potentialMoves,
- this.getPotentialMovesFrom([i, j])
- );
- }
- }
- }
- return potentialMoves;
- }
-