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 (
x int
y int
y_ int
piece *Piece
err error
)
@ -174,19 +175,34 @@ func (b *Board) movePawn(position string) error {
// TODO: implement diagonal pawn attacks
for yi := 0; yi < 8; yi++ {
piece = b.tiles[x][yi]
if piece != nil && piece.Name == Pawn && piece.Color == b.turn {
// TODO: assert move is valid:
// * 2 moves from start position
// * 1 move otherwise
// * diagonal if attacking
b.tiles[x][y] = piece
b.tiles[x][yi] = nil
return nil
}
if b.turn == Light {
y_ = y + 1
} 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:
// * 2 moves from start position
// * 1 move otherwise
// * diagonal if attacking
return nil
}