Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Returns an array of threats if the arrangement of
- // the pieces is a check, otherwise false
- var getMoves = function getMoves(p, board) {
- var x = p.x;
- var y = p.y;
- var getPawnMoves = function getPawnMoves(p, x, y) {
- if (p.owner == 0) {
- //white
- return [{
- x: x + 1,
- y: y - 1
- }, {
- x: x - 1,
- y: y - 1
- }];
- } else {
- //black
- return [{
- x: x + 1,
- y: y + 1
- }, {
- x: x - 1,
- y: y + 1
- }];
- }
- };
- var getRookMoves = function getRookMoves(p, x, y) {
- var moves = [];
- //move 8 vert and 8 hor - remove any negatives or any greater than 7
- var availableDirections = {
- UP: true,
- LEFT: true,
- RIGHT: true,
- DOWN: true
- };
- for (var i = 1; i < 8; i++) {
- var newX = {
- positive: x + i > 7 ? undefined : x + i,
- negative: x - i < 0 ? undefined : x - i
- };
- var newY = {
- positive: y + i > 7 ? undefined : y + i,
- negative: y - i < 0 ? undefined : y - i
- };
- availableDirections = getAvailableMovementDirections(p, newX, newY, availableDirections,board);
- //move right
- if (newX.positive && availableDirections.RIGHT) {
- moves.push({
- x: newX.positive,
- y: y
- });
- }
- if (newX.negative && availableDirections.LEFT) {
- moves.push({
- x: newX.negative,
- y: y
- });
- }
- if (newY.positive && availableDirections.DOWN) {
- moves.push({
- x: x,
- y: newY.positive
- });
- }
- if (newY.negative && availableDirections.UP) {
- moves.push({
- x: x,
- y: newY.negative
- });
- }
- }
- return moves;
- };
- var getKnightMoves = function getKnightMoves(p, x, y) {
- var moves = [];
- if (!isPositionOccupied(board, {
- x: x + 1,
- y: y - 2
- }) || isOppositeKing(p, {
- x: x + 1,
- y: y - 2
- },board )) {
- moves.push({
- x: x + 1,
- y: y - 2
- });
- }
- if (!isPositionOccupied(board,{
- x: x + 1,
- y: y + 2
- }) || isOppositeKing(p, {
- x: x + 1,
- y: y + 2
- },board )) {
- moves.push({
- x: x + 1,
- y: y + 2
- });
- }
- if (!isPositionOccupied(board,{
- x: x + 2,
- y: y - 1
- }) || isOppositeKing(p, {
- x: x + 2,
- y: y - 1
- },board )) {
- moves.push({
- x: x + 2,
- y: y - 1
- });
- }
- if (!isPositionOccupied(board,{
- x: x + 2,
- y: y + 1
- }) || isOppositeKing(p, {
- x: x + 2,
- y: y + 1
- },board )) {
- moves.push({
- x: x + 2,
- y: y + 1
- });
- }
- if (!isPositionOccupied(board,{
- x: x - 1,
- y: y - 2
- }) || isOppositeKing(p, {
- x: x - 1,
- y: y - 2
- },board )) {
- moves.push({
- x: x - 1,
- y: y - 2
- });
- }
- if (!isPositionOccupied(board,{
- x: x - 1,
- y: y + 2
- }) || isOppositeKing(p, {
- x: x - 1,
- y: y + 2
- },board )) {
- moves.push({
- x: x - 1,
- y: y + 2
- });
- }
- if (!isPositionOccupied(board,{
- x: x - 2,
- y: y - 1
- }) || isOppositeKing(p, {
- x: x - 2,
- y: y - 1
- },board )) {
- moves.push({
- x: x - 2,
- y: y - 1
- });
- }
- if (!isPositionOccupied(board,{
- x: x - 2,
- y: y + 1
- }) || isOppositeKing(p, {
- x: x - 2,
- y: y + 1
- },board )) {
- moves.push({
- x: x - 2,
- y: y + 1
- });
- }
- return moves;
- };
- var getQueenMoves = function getQueenMoves(p, x, y) {
- var moves = [];
- var availableDirections = {
- UP: true,
- LEFT: true,
- RIGHT: true,
- DOWN: true,
- UP_LEFT: true,
- UP_RIGHT: true,
- DOWN_LEFT: true,
- DOWN_RIGHT: true
- };
- for (var i = 1; i < 8; i++) {
- var newX = {
- positive: x + i > 7 ? undefined : x + i,
- negative: x - i < 0 ? undefined : x - i
- };
- var newY = {
- positive: y + i > 7 ? undefined : y + i,
- negative: y - i < 0 ? undefined : y - i
- };
- availableDirections = getAvailableMovementDirections(p, newX, newY, availableDirections,board);
- //move right
- if (availableDirections.RIGHT && newX.positive) {
- moves.push({
- x: newX.positive,
- y: y
- });
- }
- //move up and right
- if (availableDirections.UP_RIGHT && newX.positive && newY.negative) {
- moves.push({
- x: newX.positive,
- y: newY.negative
- });
- }
- //move up
- if (availableDirections.UP && newY.negative) {
- moves.push({
- x: x,
- y: newY.negative
- });
- }
- //move up and left
- if (availableDirections.UP_LEFT && newY.negative && newX.negative) {
- moves.push({
- x: newX.negative,
- y: newY.negative
- });
- }
- //move left
- if (availableDirections.LEFT && newX.negative) {
- moves.push({
- x: newX.negative,
- y: y
- });
- }
- //move left and down
- if (availableDirections.DOWN_LEFT && newX.negative && newY.positive) {
- moves.push({
- x: newX.negative,
- y: newY.positive
- });
- }
- //move down
- if (availableDirections.DOWN && newY.positive) {
- moves.push({
- x: x,
- y: newY.positive
- });
- }
- //move down and right
- if (availableDirections.DOWN_RIGHT && newX.positive && newY.positive) {
- moves.push({
- x: newX.positive,
- y: newY.positive
- });
- }
- }
- return moves;
- };
- var getBishopMoves = function getBishopMoves(p, x, y) {
- var moves = [];
- var availableDirections = {
- UP_LEFT: true,
- UP_RIGHT: true,
- DOWN_LEFT: true,
- DOWN_RIGHT: true
- };
- //move 8 vert and 8 hor - remove any negatives or any greater than 7
- for (var i = 1; i < 8; i++) {
- var newX = {
- positive: x + i > 7 ? undefined : x + i,
- negative: x - i < 0 ? undefined : x - i
- };
- var newY = {
- positive: y + i > 7 ? undefined : y + i,
- negative: y - i < 0 ? undefined : y - i
- };
- availableDirections = getAvailableMovementDirections(p, newX, newY, availableDirections,board);
- //move up and right
- if (availableDirections.UP_RIGHT && newX.positive && newY.negative) {
- moves.push({
- x: newX.positive,
- y: newY.negative
- });
- }
- //move up and left
- if (availableDirections.UP_LEFT && newY.negative && newX.negative) {
- moves.push({
- x: newX.negative,
- y: newY.negative
- });
- }
- //move left and down
- if (availableDirections.DOWN_LEFT && newX.negative && newY.positive) {
- moves.push({
- x: newX.negative,
- y: newY.positive
- });
- }
- //move down and right
- if (availableDirections.DOWN_RIGHT && newX.positive && newY.positive) {
- moves.push({
- x: newX.positive,
- y: newY.positive
- });
- }
- }
- return moves;
- };
- var getKingMoves = function getKingMoves(p, x, y) {
- let moves = [];
- var availableDirections = {
- UP: true,
- LEFT: true,
- RIGHT: true,
- DOWN: true
- };
- var newX = {
- positive: x + 1 > 7 ? undefined : x + 1,
- negative: x - 1 < 0 ? undefined : x - 1
- };
- var newY = {
- positive: y + 1 > 7 ? undefined : y + 1,
- negative: y - 1 < 0 ? undefined : y - 1
- };
- availableDirections = getAvailableMovementDirections(p, newX, newY, availableDirections, board);
- //move right
- if (availableDirections.RIGHT && newX.positive) {
- moves.push({
- x: newX.positive,
- y: y
- });
- }
- //move up and right
- if (availableDirections.UP_RIGHT && newX.positive && newY.negative) {
- moves.push({
- x: newX.positive,
- y: newY.negative
- });
- }
- //move up
- if (availableDirections.UP && newY.negative) {
- moves.push({
- x: x,
- y: newY.negative
- });
- }
- //move up and left
- if (availableDirections.UP_LEFT && newY.negative && newX.negative) {
- moves.push({
- x: newX.negative,
- y: newY.negative
- });
- }
- //move left
- if (availableDirections.LEFT && newX.negative) {
- moves.push({
- x: newX.negative,
- y: y
- });
- }
- //move left and down
- if (availableDirections.DOWN_LEFT && newX.negative && newY.positive) {
- moves.push({
- x: newX.negative,
- y: newY.positive
- });
- }
- //move down
- if (availableDirections.DOWN && newY.positive) {
- moves.push({
- x: x,
- y: newY.positive
- });
- }
- //move down and right
- if (availableDirections.DOWN_RIGHT && newX.positive && newY.positive) {
- moves.push({
- x: newX.positive,
- y: newY.positive
- });
- }
- return moves;
- };
- switch (p.piece) {
- case 'pawn':
- return getPawnMoves(p, x, y);
- case 'rook':
- return getRookMoves(p, x, y);
- case 'bishop':
- return getBishopMoves(p, x, y);
- case 'queen':
- return getQueenMoves(p, x, y);
- case 'knight':
- return getKnightMoves(p, x, y);
- case 'king':
- return getKingMoves(p, x, y);
- }
- };
- function getAvailableMovementDirections(piece, newX, newY, directions, board) {
- const x = piece.x;
- const y = piece.y;
- if (directions.RIGHT && isPositionOccupied(board, {
- x: newX.positive,
- y: y
- })) {
- directions.RIGHT = isOppositeKing(piece, {
- x: newX.positive,
- y: y
- }, board) ? directions.RIGHT : false;
- }
- if (directions.LEFT && isPositionOccupied(board, {
- x: newX.negative,
- y: y
- })) {
- directions.LEFT = isOppositeKing(piece, {
- x: newX.negative,
- y: y
- }, board) ? directions.LEFT : false;
- }
- if (directions.DOWN && isPositionOccupied(board, {
- x: x,
- y: newY.positive
- })) {
- directions.DOWN = isOppositeKing(piece, {
- x: x,
- y: newY.positive
- }, board) ? directions.DOWN : false;
- }
- if (directions.UP && isPositionOccupied(board, {
- x: x,
- y: newY.negative
- })) {
- directions.UP = isOppositeKing(piece, {
- x: x,
- y: newY.negative
- }, board) ? directions.UP : false;
- }
- if (directions.UP_RIGHT && isPositionOccupied(board, {
- x: newX.positive,
- y: newY.negative
- })) {
- directions.UP_RIGHT = isOppositeKing(piece, {
- x: newX.positive,
- y: newY.negative
- }, board) ? directions.UP_RIGHT : false;
- }
- if (directions.UP_LEFT && isPositionOccupied(board, {
- x: newX.negative,
- y: newY.negative
- })) {
- directions.UP_LEFT = isOppositeKing(piece, {
- x: newX.negative,
- y: newY.negative
- }, board) ? directions.UP_LEFT : false;
- }
- if (directions.DOWN_LEFT && isPositionOccupied(board, {
- x: newX.negative,
- y: newY.positive
- })) {
- directions.DOWN_LEFT = isOppositeKing(piece, {
- x: newX.negative,
- y: newY.positive
- }, board) ? directions.DOWN_LEFT : false;
- }
- if (directions.DOWN_RIGHT && isPositionOccupied(board, {
- x: newX.positive,
- y: newY.positive
- })) {
- directions.DOWN_RIGHT = isOppositeKing(piece, {
- x: newX.positive,
- y: newY.positive
- }, board) ? directions.DOWN_RIGHT : false;
- }
- return directions;
- }
- function getPiece(pieces, name, owner) {
- return pieces.find(function(piece) {
- return piece.owner == owner && piece.piece == name;
- });
- }
- function isPositionOccupied(pieces, pos) {
- return pieces.findIndex(p => p.x == pos.x && p.y == pos.y) > -1;
- }
- function isOppositeKing(piece, position, board) {
- let p = getPieceAtPosition(board, position);
- return p.owner != piece.owner && p.piece == 'king';
- }
- function getPieceAtPosition(pieces, pos) {
- return pieces.find(p => p.x == pos.x && p.y == pos.y);
- }
- function isCheck(pieces, player) {
- var king = getPiece(pieces, 'king', player);
- var isChecking = function isChecking(p, king) {
- if (getMoves(p, pieces).findIndex(m => m.x == king.x && m.y == king.y) > -1) {
- return true;
- } else {
- return false;
- }
- };
- var getCheckingPieces = function getCheckingPieces(candidatePieces) {
- let checkingPieces = [];
- candidatePieces.forEach(function(p) {
- if (p == king) return;
- if (isChecking(p, king)) {
- checkingPieces.push(p);
- }
- });
- return checkingPieces && checkingPieces.length > 0 ? checkingPieces : false;
- };
- return getCheckingPieces(pieces);
- }
- const isMate = (pieces, player) => {
- let king = getPiece(pieces, 'king', player);
- let checkMate = true;
- //check all possible moves from king against isCheck function
- let possibleMoves = getMoves(king, pieces);
- possibleMoves.forEach(move => {
- if(!checkMate) return;
- let kingIndex = pieces.findIndex(p => p.piece == 'king' && p.owner == player);
- let newKing = {piece: 'king', owner: player, x: move.x, y: move.y};
- //construct a new board with the potential move
- let board = [...pieces.slice(0, kingIndex), newKing, ...pieces.slice(kingIndex +1, pieces.length)];
- if(isCheck(board, player).length == 0){
- checkMate = false;
- return;
- }
- })
- return checkMate;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement