Fix pawn capture

This commit is contained in:
ekzyis 2024-09-25 23:30:13 +02:00
parent 516478d258
commit 4e12b40478
1 changed files with 22 additions and 22 deletions

View File

@ -348,43 +348,43 @@ func (b *Board) Move(position string) error {
func (b *Board) movePawn(position string, captureFrom string) error { func (b *Board) movePawn(position string, captureFrom string) error {
var ( var (
x int toX int
y int toY int
cX int fromX int
cY int fromY int
yPrev int yPrev int
piece *Piece piece *Piece
err error err error
) )
if x, y, err = getXY(position); err != nil { if toX, toY, err = getXY(position); err != nil {
return err return err
} }
if captureFrom != "" { if captureFrom != "" {
if cX, cY, err = getXY(captureFrom + position[0:1]); err != nil { if fromX, fromY, err = getXY(captureFrom + position[1:2]); err != nil {
return err return err
} }
if b.turn == Light { if b.turn == Light {
cY = y + 1 fromY = toY + 1
} else { } else {
cY = y - 1 fromY = toY - 1
} }
piece = b.getPiece(cX, cY) piece = b.getPiece(fromX, fromY)
if piece == nil || piece.Name != Pawn || piece.Color != b.turn { if piece == nil || piece.Name != Pawn || piece.Color != b.turn {
// not your pawn // not your pawn
return fmt.Errorf("invalid capture move for pawn: %s", position) return fmt.Errorf("invalid capture move for pawn: %s", position)
} }
if cX != x-1 && cX != x+1 || (b.turn == Light && cY != y+1) || (b.turn == Dark && cY != y-1) { if fromX != toX-1 && fromX != toX+1 || (b.turn == Light && fromY != toY+1) || (b.turn == Dark && fromY != toY-1) {
// invalid capture move // invalid capture move
return fmt.Errorf("invalid capture move for pawn: %s", position) return fmt.Errorf("invalid capture move for pawn: %s", position)
} }
b.tiles[cX][cY] = nil b.tiles[fromX][fromY] = nil
b.tiles[x][y] = piece b.tiles[toX][toY] = piece
return nil return nil
} }
@ -395,28 +395,28 @@ func (b *Board) movePawn(position string, captureFrom string) error {
// * no collision with other pieces // * no collision with other pieces
if b.turn == Light { if b.turn == Light {
yPrev = y + 1 yPrev = toY + 1
} else { } else {
yPrev = y - 1 yPrev = toY - 1
} }
piece = b.tiles[x][yPrev] piece = b.tiles[toX][yPrev]
if piece != nil && piece.Name == Pawn && piece.Color == b.turn { if piece != nil && piece.Name == Pawn && piece.Color == b.turn {
b.tiles[x][yPrev] = nil b.tiles[toX][yPrev] = nil
b.tiles[x][y] = piece b.tiles[toX][toY] = piece
return nil return nil
} }
if b.turn == Light { if b.turn == Light {
yPrev = y + 2 yPrev = toY + 2
} else { } else {
yPrev = y - 2 yPrev = toY - 2
} }
piece = b.tiles[x][yPrev] piece = b.tiles[toX][yPrev]
if piece != nil && piece.Name == Pawn && piece.Color == b.turn { if piece != nil && piece.Name == Pawn && piece.Color == b.turn {
b.tiles[x][yPrev] = nil b.tiles[toX][yPrev] = nil
b.tiles[x][y] = piece b.tiles[toX][toY] = piece
return nil return nil
} }