Simplify pawn move

This commit is contained in:
ekzyis 2024-09-18 05:20:26 +02:00
parent 258d375dde
commit bd1c0b3c6e
1 changed files with 27 additions and 11 deletions

View File

@ -164,6 +164,7 @@ func (b *Board) movePawn(position string) error {
var ( var (
x int x int
y int y int
y_ int
piece *Piece piece *Piece
err error err error
) )
@ -174,18 +175,33 @@ func (b *Board) movePawn(position string) error {
// TODO: implement diagonal pawn attacks // TODO: implement diagonal pawn attacks
for yi := 0; yi < 8; yi++ { if b.turn == Light {
piece = b.tiles[x][yi] y_ = y + 1
if piece != nil && piece.Name == Pawn && piece.Color == b.turn { } else {
y_ = y - 1
}
piece = b.tiles[x][y_]
if piece == nil {
if b.turn == Light {
y_ = y + 2
} else {
y_ = y - 2
}
piece = b.tiles[x][y_]
}
if piece == nil {
return fmt.Errorf("no pawn found that can move to %s", position)
}
b.tiles[x][y_] = nil
b.tiles[x][y] = piece
// TODO: assert move is valid: // TODO: assert move is valid:
// * 2 moves from start position // * 2 moves from start position
// * 1 move otherwise // * 1 move otherwise
// * diagonal if attacking // * diagonal if attacking
b.tiles[x][y] = piece
b.tiles[x][yi] = nil
return nil
}
}
return nil return nil
} }