Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda