Use inline move function for early returns
This commit is contained in:
parent
d6ffd8a57d
commit
fddbd145f3
@ -312,39 +312,42 @@ func (b *Board) Move(move string) error {
|
|||||||
// TODO: make sure king is not in check after move
|
// TODO: make sure king is not in check after move
|
||||||
// ( this avoids moving into check and moving a piece that exposes the king to check e.g. pinned pieces )
|
// ( this avoids moving into check and moving a piece that exposes the king to check e.g. pinned pieces )
|
||||||
|
|
||||||
if len(move) == 2 {
|
move_ := func() error {
|
||||||
err = b.movePawn(move, captureFrom)
|
if len(move) == 2 {
|
||||||
} else if len(move) == 3 {
|
return b.movePawn(move, captureFrom)
|
||||||
|
|
||||||
piece = move[0:1]
|
|
||||||
targetPosition = move[1:3]
|
|
||||||
|
|
||||||
// collision detection
|
|
||||||
if collisionPiece, err = b.getCollision(targetPosition); err != nil {
|
|
||||||
return fmt.Errorf("invalid move %s: %v", move, err)
|
|
||||||
} else if collisionPiece != nil {
|
|
||||||
return fmt.Errorf("invalid move %s: position %s blocked by %s", move, targetPosition, collisionPiece)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch strings.ToLower(piece) {
|
if len(move) == 3 {
|
||||||
case "r":
|
piece = move[0:1]
|
||||||
err = b.moveRook(targetPosition, false)
|
targetPosition = move[1:3]
|
||||||
case "b":
|
|
||||||
err = b.moveBishop(targetPosition, false)
|
// collision detection
|
||||||
case "n":
|
if collisionPiece, err = b.getCollision(targetPosition); err != nil {
|
||||||
err = b.moveKnight(targetPosition)
|
return fmt.Errorf("invalid move %s: %v", move, err)
|
||||||
case "q":
|
} else if collisionPiece != nil {
|
||||||
err = b.moveQueen(targetPosition)
|
return fmt.Errorf("invalid move %s: position %s blocked by %s", move, targetPosition, collisionPiece)
|
||||||
case "k":
|
}
|
||||||
err = b.moveKing(targetPosition)
|
|
||||||
default:
|
switch strings.ToLower(piece) {
|
||||||
err = fmt.Errorf("invalid move %s: %v", move, err)
|
case "r":
|
||||||
|
return b.moveRook(targetPosition, false)
|
||||||
|
case "b":
|
||||||
|
return b.moveBishop(targetPosition, false)
|
||||||
|
case "n":
|
||||||
|
return b.moveKnight(targetPosition)
|
||||||
|
case "q":
|
||||||
|
return b.moveQueen(targetPosition)
|
||||||
|
case "k":
|
||||||
|
return b.moveKing(targetPosition)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid move %s: %v", move, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return fmt.Errorf("invalid move %s: %v", move, err)
|
return fmt.Errorf("invalid move %s: %v", move, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err = move_(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user