Skip to content

Commit

Permalink
chess: don't store capture piece in Move
Browse files Browse the repository at this point in the history
  • Loading branch information
e0ff committed Nov 27, 2023
1 parent 14d0db6 commit f27e2e2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 44 deletions.
14 changes: 3 additions & 11 deletions internal/chess/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type Move struct {
flags MoveFlag

promotionPiece Piece
capturePiece Piece
}

var NullMove = NewMove(A1, A1, Null, NoMoveFlag)
Expand All @@ -60,7 +59,6 @@ func NewMove(from, to Square, moveType MoveType, flags MoveFlag) Move {
moveType: moveType,
flags: flags,
promotionPiece: EmptyPiece,
capturePiece: EmptyPiece,
}
}

Expand All @@ -80,12 +78,6 @@ func (m *Move) WithPromotion(piece Piece) {
m.promotionPiece = piece
}

// WithCapture sets that the move will result in the given piece being captured.
func (m *Move) WithCapture(piece Piece) {
m.flags |= CaputureMoveFlag
m.capturePiece = piece
}

// Type returns the type of the move.
func (m Move) Type() MoveType {
return m.moveType
Expand Down Expand Up @@ -117,9 +109,9 @@ func (m Move) IsPromotion() bool {
return m.promotionPiece != EmptyPiece
}

// Captures returns whether the move results in a capture.
func (m Move) Captures() bool {
return m.capturePiece != EmptyPiece
// IsCapture returns whether the move results in a capture.
func (m Move) IsCapture() bool {
return m.HasFlag(CaputureMoveFlag)
}

// HasFlag checks if the move has that flag set.
Expand Down
33 changes: 7 additions & 26 deletions internal/chess/movegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ func generatePawnMoves(position Position, genType MoveGenerationType) []Move {
if capturePiece != EmptyPiece && capturePiece.Color() != position.turn {
if captureSquare.Rank() == pawnPromotionRank(position.Turn()) {
for _, pieceType := range promotablePieces {
move := NewMove(square, captureSquare, NormalMove, NoMoveFlag)
move.WithCapture(capturePiece)
move := NewMove(square, captureSquare, NormalMove, CaputureMoveFlag)
move.WithPromotion(NewPiece(pieceType, position.Turn()))
moves = append(moves, move)
}
} else {
move := NewMove(square, captureSquare, NormalMove, NoMoveFlag)
move.WithCapture(capturePiece)
move := NewMove(square, captureSquare, NormalMove, CaputureMoveFlag)
moves = append(moves, move)
}
}
Expand All @@ -73,8 +71,7 @@ func generatePawnMoves(position Position, genType MoveGenerationType) []Move {

capturePiece, _ := position.GetPieceAt(captureSquare)
if capturePiece.Type() == Pawn && capturePiece.Color() == position.Turn().OpposingSide() {
move := NewMove(square, position.EnPassant(), EnPassantMove, NoMoveFlag)
move.WithCapture(capturePiece)
move := NewMove(square, position.EnPassant(), EnPassantMove, CaputureMoveFlag)
moves = append(moves, move)
}
}
Expand Down Expand Up @@ -108,11 +105,7 @@ func generateKnightMoves(position Position, genType MoveGenerationType) []Move {
capturesBB := attackBB & opponent
for capturesBB > 0 {
toSquare := Square(capturesBB.PopLsb())
piece, _ := position.GetPieceAt(toSquare)

move := NewMove(fromSquare, toSquare, NormalMove, NoMoveFlag)
move.WithCapture(piece)

move := NewMove(fromSquare, toSquare, NormalMove, CaputureMoveFlag)
moves = append(moves, move)
}
}
Expand Down Expand Up @@ -143,11 +136,7 @@ func generateBishopMoves(position Position, pieceBB BitBoard, genType MoveGenera
capturesBB := attackBB & opponent
for capturesBB > 0 {
toSquare := Square(capturesBB.PopLsb())
piece, _ := position.GetPieceAt(toSquare)

move := NewMove(fromSquare, toSquare, NormalMove, NoMoveFlag)
move.WithCapture(piece)

move := NewMove(fromSquare, toSquare, NormalMove, CaputureMoveFlag)
moves = append(moves, move)
}
}
Expand Down Expand Up @@ -178,11 +167,7 @@ func generateRookMoves(position Position, pieceBB BitBoard, genType MoveGenerati
capturesBB := attacks & opponent
for capturesBB > 0 {
toSquare := Square(capturesBB.PopLsb())
piece, _ := position.GetPieceAt(toSquare)

move := NewMove(fromSquare, toSquare, NormalMove, NoMoveFlag)
move.WithCapture(piece)

move := NewMove(fromSquare, toSquare, NormalMove, CaputureMoveFlag)
moves = append(moves, move)
}
}
Expand Down Expand Up @@ -224,11 +209,7 @@ func generateKingMoves(position Position, genType MoveGenerationType, includeCas
capturesBB := attacks & opponent
for capturesBB > 0 {
toSquare := Square(capturesBB.PopLsb())
piece, _ := position.GetPieceAt(toSquare)

move := NewMove(kingSquare, toSquare, NormalMove, NoMoveFlag)
move.WithCapture(piece)

move := NewMove(kingSquare, toSquare, NormalMove, CaputureMoveFlag)
moves = append(moves, move)
}

Expand Down
15 changes: 8 additions & 7 deletions internal/chess/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ func (p *Position) MakeMove(move Move) error {
return fmt.Errorf("%w: tyring to move opponent's piece with %s", ErrInvalidMove, move)
}

capturePiece := move.capturePiece
capturePiece, _ := p.GetPieceAt(move.To())
if movingPiece.Color() == capturePiece.Color() {
return fmt.Errorf("%w: trying to capture piece of same color with %s", ErrInvalidMove, move)
}
Expand Down Expand Up @@ -619,7 +619,7 @@ func (p *Position) MakeMove(move Move) error {
}
}

if move.Captures() {
if move.IsCapture() {
p.clearPiece(to)

if capturePiece.Type() == Rook {
Expand Down Expand Up @@ -765,13 +765,14 @@ func (p *Position) MakeUciMove(uci string) error {
moveType = EnPassantMove
}

move := NewMove(from, to, moveType, NoMoveFlag)

capturePiece, err := p.GetPieceAt(to)
if err == nil {
move.WithCapture(capturePiece)
flag := QuietMoveFlag
capturePiece, _ := p.GetPieceAt(to)
if capturePiece != EmptyPiece {
flag = CaputureMoveFlag
}

move := NewMove(from, to, moveType, flag)

if len(uci) > 4 {
switch uci[4] {
case 'n':
Expand Down

0 comments on commit f27e2e2

Please sign in to comment.