fix: correct captured piece extraction from Board.movePiece() return value
All checks were successful
CI Pipeline / Code Linting (pull_request) Successful in 13s
CI Pipeline / Run Tests (pull_request) Successful in 21s
CI Pipeline / Build Verification (pull_request) Successful in 13s
CI Pipeline / Generate Quality Report (pull_request) Successful in 19s

Fixes critical bug where moves with captures would crash the game.

Root Cause:
- Board.movePiece() returns an object: { captured: pieceOrNull }
- GameController.executeMove() was treating the return value as the piece itself
- This caused move.captured to be { captured: piece } instead of piece
- When GameState.recordMove() tried to access move.captured.color, it was undefined
- Error: "TypeError: undefined is not an object (evaluating 'this.capturedPieces[move.captured.color].push')"

The Fix:
Extract the captured piece from the return object:
  const moveResult = this.board.movePiece(fromRow, fromCol, toRow, toCol);
  captured = moveResult.captured;

This ensures move.captured is the actual Piece object (or null), not wrapped in an object.

Impact:
- Moves with captures now work correctly
- Captured pieces are properly tracked in game state
- UI can now display captured pieces
- Game flow works end-to-end

Testing:
- All 124 unit tests passing 
- Captures properly recorded in capturedPieces arrays
- No regression in non-capture moves

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Christoph Wagner 2025-11-23 15:37:05 +01:00
parent 9011e3b51e
commit 8390862a73

View File

@ -105,7 +105,8 @@ export class GameController {
captured = SpecialMoves.executeEnPassant(this.board, piece, toRow, toCol);
} else {
// Normal move
captured = this.board.movePiece(fromRow, fromCol, toRow, toCol);
const moveResult = this.board.movePiece(fromRow, fromCol, toRow, toCol);
captured = moveResult.captured;
// Check for promotion
if (specialMoveType === 'promotion' || (piece.type === 'pawn' && piece.canPromote())) {