Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
Chess : implementing check in chess

I am building a chess game , I am now implementing a check. here is my implementation:

export function check(position, color) {
  let king;
  position.forEach((row) => {
    row.forEach((piece) => {
      if (piece.symbol == "k" && piece.color == color) king = piece;
    });
  });
  getAllLegalMoves(position, color === "w" ? "b" : "w").forEach((el) => {
    if (el.row == king.row && el.col == king.col) {
      return true;
    }
  });
  return false;
}

the function do the following:

  1. find the king in the position array which is a 8 * 8 2d array of pieces.
  2. find the legal Moves of the opponent
  3. check if the king square is among the possible moves of the opponent

here is the code for dropping the piece in the new square:

document.addEventListener("drop", function (event) {
  const { row, col } = dragged.parentNode.dataset;
  const piece = position[row][col];

  if (!piece.canMoveTo(position, event.target.dataset) || piece.color != turn)
    return;

  piece.movePiece(position, event.target.dataset);

  if (check(position, turn)) console.log("check");

  dragged.parentNode.removeChild(dragged);
  event.target.appendChild(dragged);
  if (turn == "w") turn = "b";
  else turn = "w";
});

first I check if the piece can move to the new square, If so I move the piece and I check the player who moved the piece is in check , if so I log check , but it is always logging false. Can you help me please. Thank you.

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

My first observation would be that you are returning true from your forEach function, which does nothing. Then you are returning false always no matter what. Try this:

export function check(position, color) {
  let king;
  position.forEach((row) => {
    row.forEach((piece) => {
      if (piece.symbol == "k" && piece.color == color) king = piece;
    });
  });
  return getAllLegalMoves(position, color === "w" ? "b" : "w").reduce((carry, el) => {
    if (carry) return carry;
    return el.row == king.row && el.col == king.col;
  }, false);
}

about 4 years ago · Santiago Gelvez Denunciar

0

here is the answer:

export function check(position, color) {
  let king,
    inCheck = false;
  position.forEach((row) => {
    row.forEach((piece) => {
      if (piece.symbol == "k" && piece.color == color) king = piece;
    });
  });
  getAllLegalMoves(position, color === "w" ? "b" : "w").forEach((el) => {
    if (el.row == king.row && el.col == king.col) {
      inCheck = true;
    }
  });
  return inCheck;
}

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda