Refactor en passant logic and improve test clarity

Refactor board logic and improve tests

- Refactor import statements in pieces.go
- Add String method to Piece type for better representation
- Add isEnPassant field to Move struct
- Update move logic to mark en passant captures
- Adjust handling of pawn moves related to en passant
- Change MakeMove to not return a move
- Update applyEnPassant to accept pointer to Move
- Track en passant captures within Move struct
- Remove redundant condition check in UndoMove
- Enhance readability of variable assignments
- Comment out parallel test execution for debugging
- Modify error handling for accurate performance logging
- Correct expected board state after undo in en passant scenario
- Clarify comments in tests regarding en passant logic
- Improve clarity of error messages in test assertions
This commit is contained in:
MSWS
2025-03-22 14:48:33 -07:00
parent 5cedc0b351
commit 57bba0ef59
5 changed files with 29 additions and 14 deletions

View File

@@ -76,7 +76,7 @@ func (board Board) Move(from Coordinate, to Coordinate) {
board.Board[fromRow][fromCol] = 0
}
func (board *Board) MakeMove(move Move) Move {
func (board *Board) MakeMove(move Move) {
board.WhiteCastleHistory = append(board.WhiteCastleHistory, board.WhiteCastling)
board.BlackCastleHistory = append(board.BlackCastleHistory, board.BlackCastling)
board.PreviousEnpassant = board.EnPassant
@@ -95,7 +95,7 @@ func (board *Board) MakeMove(move Move) Move {
board.Set(move.to, move.promotionTo|move.piece.GetColor())
}
board.applyEnPassant(move)
board.applyEnPassant(&move)
}
castlability := &board.WhiteCastling
@@ -122,7 +122,7 @@ func (board *Board) MakeMove(move Move) Move {
board.Active = (^board.Active).GetColor()
board.Moves = append(board.Moves, move)
return move
// return move
}
func (board *Board) UndoMove() {
@@ -135,8 +135,10 @@ func (board *Board) UndoMove() {
castling = &board.BlackCastling
}
toRow, toCol := move.to.GetCoords()
fromRow, _ := move.from.GetCoords()
if move.IsCastle() {
toRow, toCol := move.to.GetCoords()
if toCol == 0 {
castling.CanQueenSide = true
@@ -150,12 +152,15 @@ func (board *Board) UndoMove() {
}
}
if move.isEnPassant {
board.Set(move.to, 0)
captured := CreateCoordByte(fromRow, toCol)
board.Set(captured, move.capture)
}
board.Active = (^board.Active).GetColor()
board.EnPassant = board.PreviousEnpassant
board.Moves = board.Moves[0 : len(board.Moves)-1]
if len(board.Moves) > 0 {
board.applyEnPassant(board.Moves[len(board.Moves)-1])
}
board.WhiteCastling = board.WhiteCastleHistory[len(board.WhiteCastleHistory)-1]
board.BlackCastling = board.BlackCastleHistory[len(board.BlackCastleHistory)-1]
@@ -163,7 +168,7 @@ func (board *Board) UndoMove() {
board.BlackCastleHistory = board.BlackCastleHistory[0 : len(board.BlackCastleHistory)-1]
}
func (board *Board) applyEnPassant(move Move) {
func (board *Board) applyEnPassant(move *Move) {
toRow, toCol := move.to.GetCoords()
fromRow, fromCol := move.from.GetCoords()
if move.piece.GetType() != Pawn {
@@ -171,15 +176,16 @@ func (board *Board) applyEnPassant(move Move) {
return
}
enemyPiece := Pawn | (^move.piece).GetColor()
enemyPiece := Pawn | ((^move.piece).GetColor())
if board.EnPassant != nil && move.to == *board.EnPassant {
// En passant!
captured := CreateCoordByte(fromRow, toCol)
if board.Get(captured) != enemyPiece {
panic(fmt.Sprintf("En Passanted non-enemy piece on %v, got %v, expcted %v", captured, board.Get(captured), enemyPiece))
panic(fmt.Sprintf("En Passanted non-enemy piece on %v, got %v, expected %v", captured, board.Get(captured), enemyPiece))
}
board.Set(captured, 0)
move.capture = enemyPiece
move.isEnPassant = true
}
board.EnPassant = nil

View File

@@ -218,7 +218,7 @@ func TestUndoMove(t *testing.T) {
board.UndoMove()
if board.GetStr("d6") != 0 {
t.Errorf("board failed to delete passanting pawn upon undo, expected %v, got %v", nil, board.GetStr("d6"))
t.Errorf("board failed to delete passanting pawn upon undo, expected %v, got %v", 0, board.GetStr("d6"))
}
if board.GetStr("d5") != Black|Pawn {

View File

@@ -10,6 +10,7 @@ type Move struct {
piece Piece
capture Piece
promotionTo Piece
isEnPassant bool
}
func (move Move) IsCastle() bool {
@@ -221,7 +222,9 @@ func (game Board) getPawnMoves(coord Coordinate) []Move {
if int(enRow) == int(row)+direction {
diff := int(enCol) - int(col)
if diff == -1 || diff == 1 {
moves = append(moves, game.CreateMove(coord, *game.EnPassant))
move := game.CreateMove(coord, *game.EnPassant)
move.isEnPassant = true
moves = append(moves, move)
}
}
}

View File

@@ -87,7 +87,7 @@ func TestGetMoves(t *testing.T) {
func TestPerfs(t *testing.T) {
for name, test := range getPerfData() {
t.Run(name, func(t *testing.T) {
t.Parallel()
// t.Parallel()
start, err := FromFEN(test.FEN)
if err != nil {

View File

@@ -1,6 +1,8 @@
package board
import "fmt"
import (
"fmt"
)
type Piece byte
@@ -16,6 +18,10 @@ const (
King
)
func (piece Piece) String() string {
return string(piece.GetRune())
}
func GetPiece(c rune) (Piece, error) {
white := true
if c > 'Z' {