Use inline move function for early returns
This commit is contained in:
parent
d6ffd8a57d
commit
fddbd145f3
@ -312,10 +312,12 @@ 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 )
|
||||||
|
|
||||||
|
move_ := func() error {
|
||||||
if len(move) == 2 {
|
if len(move) == 2 {
|
||||||
err = b.movePawn(move, captureFrom)
|
return b.movePawn(move, captureFrom)
|
||||||
} else if len(move) == 3 {
|
}
|
||||||
|
|
||||||
|
if len(move) == 3 {
|
||||||
piece = move[0:1]
|
piece = move[0:1]
|
||||||
targetPosition = move[1:3]
|
targetPosition = move[1:3]
|
||||||
|
|
||||||
@ -328,23 +330,24 @@ func (b *Board) Move(move string) error {
|
|||||||
|
|
||||||
switch strings.ToLower(piece) {
|
switch strings.ToLower(piece) {
|
||||||
case "r":
|
case "r":
|
||||||
err = b.moveRook(targetPosition, false)
|
return b.moveRook(targetPosition, false)
|
||||||
case "b":
|
case "b":
|
||||||
err = b.moveBishop(targetPosition, false)
|
return b.moveBishop(targetPosition, false)
|
||||||
case "n":
|
case "n":
|
||||||
err = b.moveKnight(targetPosition)
|
return b.moveKnight(targetPosition)
|
||||||
case "q":
|
case "q":
|
||||||
err = b.moveQueen(targetPosition)
|
return b.moveQueen(targetPosition)
|
||||||
case "k":
|
case "k":
|
||||||
err = b.moveKing(targetPosition)
|
return b.moveKing(targetPosition)
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("invalid move %s: %v", move, err)
|
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