Clarify en passant and castling move handling

Improve en passant handling and simplify castling logic

- Update handling of en passant moves to assign the `EnPassant` field correctly
- Remove redundant assignment of `EnPassant` at the end of the method
- Clarify castling conditions for better understanding
- Simplify test cases by removing unnecessary scenarios
- Add a test case for handling en passant on double undo
- Verify accurate game state restoration after multiple undos
- Ensure proper marking and unmarking of en passant under various conditions
This commit is contained in:
MSWS
2025-03-22 15:31:14 -07:00
parent c7d3614172
commit d5af1cdd3a
2 changed files with 43 additions and 32 deletions

View File

@@ -156,10 +156,12 @@ func (board *Board) UndoMove() {
board.Set(move.to, 0)
captured := CreateCoordByte(fromRow, toCol)
board.Set(captured, move.capture)
board.EnPassant = &move.to
} else {
board.EnPassant = board.PreviousEnpassant
}
board.Active = (^board.Active).GetColor()
board.EnPassant = board.PreviousEnpassant
board.Moves = board.Moves[0 : len(board.Moves)-1]
board.WhiteCastling = board.WhiteCastleHistory[len(board.WhiteCastleHistory)-1]
board.BlackCastling = board.BlackCastleHistory[len(board.BlackCastleHistory)-1]

View File

@@ -161,37 +161,6 @@ func TestMakeMove(t *testing.T) {
}
})
t.Run("OnTwoUndo", func(t *testing.T) {
board, err := FromFEN("rnbqkbnr/ppppp1pp/8/4Pp2/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2")
if err != nil {
t.Error(err)
}
board.MakeMove(board.CreateMoveStr("d7", "d5"))
board.MakeMove(board.CreateMoveStr("e5", "d6"))
board.MakeMove(board.CreateMoveStr("c7", "d6"))
if board.GetStr("d6") != Black|Pawn {
t.Errorf("board failed to properly capture, expected %v, got %v", Black|Pawn, board.GetStr("d6"))
}
if board.GetStr("c7") != 0 {
t.Errorf("board failed to properly move black pawn, expected %v, got %v", nil, board.GetStr("c7"))
}
board.UndoMove()
if board.GetStr("d6") != White|Pawn {
t.Errorf("board failed to properly capture, expected %v, got %v", White|Pawn, board.GetStr("d6"))
}
board.UndoMove()
if board.EnPassant != nil {
t.Errorf("board failed to unmark en passant, expected %v, got %v", nil, *board.EnPassant)
}
})
})
})
}
@@ -310,6 +279,46 @@ func TestUndoMove(t *testing.T) {
}
}
})
t.Run("OnTwoUndo", func(t *testing.T) {
board, err := FromFEN("rnbqkbnr/ppppp1pp/8/4Pp2/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2")
if err != nil {
t.Error(err)
}
board.MakeMove(board.CreateMoveStr("d7", "d5"))
board.MakeMove(board.CreateMoveStr("e5", "d6"))
board.MakeMove(board.CreateMoveStr("c7", "d6"))
if board.GetStr("d6") != Black|Pawn {
t.Errorf("board failed to properly capture, expected %v, got %v", Black|Pawn, board.GetStr("d6"))
}
if board.GetStr("c7") != 0 {
t.Errorf("board failed to properly move black pawn, expected %v, got %v", nil, board.GetStr("c7"))
}
board.UndoMove()
if board.GetStr("d6") != White|Pawn {
t.Errorf("board failed to properly capture, expected %v, got %v", White|Pawn, board.GetStr("d6"))
}
board.UndoMove()
expected := CreateCoordAlgebra("d6")
t.Log(board.ToFEN())
if board.EnPassant == nil || *board.EnPassant != expected {
if board.EnPassant == nil {
t.Errorf("board failed to restore en passant, expected %v, got %v", expected, nil)
} else {
t.Errorf("board failed to restore en passant, expected %v, got %v", expected, *board.EnPassant)
}
}
})
})
})
}