Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
Ajedrez: implementación de check in chess

Estoy construyendo un juego de ajedrez, ahora estoy implementando un control. aquí está mi implementación:

 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; }

la función hace lo siguiente:

  1. encuentre el rey en la matriz de posiciones, que es una matriz de piezas de 8 * 8 2d.
  2. encontrar los movimientos legales del oponente
  3. comprobar si la casilla del rey se encuentra entre los posibles movimientos del oponente

aquí está el código para dejar caer la pieza en el nuevo cuadrado:

 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"; });

primero verifico si la pieza puede moverse a la nueva casilla, si es así, muevo la pieza y compruebo que el jugador que movió la pieza está en jaque, si es así, registro la verificación, pero siempre está registrando falso. Puedes ayudarme por favor. Gracias.

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Mi primera observación sería que está devolviendo verdadero desde su función forEach, que no hace nada. Entonces estás devolviendo falso siempre sin importar qué. Prueba esto:

 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 Report

0

aquí está la respuesta:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!