From 227b342da43ed61c74258070d90e0c1161206fe4 Mon Sep 17 00:00:00 2001 From: Andrew DeChristopher Date: Mon, 1 Mar 2021 21:58:15 -0500 Subject: [PATCH] feat: 1.0.1 added InCheck and CheckSquare to Position --- engine.go | 5 +---- liad/liad.go | 3 +++ position.go | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/engine.go b/engine.go index 7033bd3..1262be2 100644 --- a/engine.go +++ b/engine.go @@ -109,10 +109,7 @@ func addTags(m *Move, pos *Position) { } func isInCheck(pos *Position) bool { - kingSq := pos.board.whiteKingSq - if pos.Turn() == Black { - kingSq = pos.board.blackKingSq - } + kingSq := pos.activeKingSquare() // king should only be missing in tests / examples if kingSq == NoSquare { return false diff --git a/liad/liad.go b/liad/liad.go index d21504f..4678145 100644 --- a/liad/liad.go +++ b/liad/liad.go @@ -50,6 +50,9 @@ func main() { if game.Position().EnPassantSquare() != octad.NoSquare { fmt.Printf(", enp: %s", game.Position().EnPassantSquare().String()) } + if game.Position().InCheck() { + fmt.Printf(", in check at %s", game.Position().CheckSquare()) + } } fmt.Println(game.Position().Board().Draw()) diff --git a/position.go b/position.go index 2f436fb..24cfe2f 100644 --- a/position.go +++ b/position.go @@ -137,6 +137,20 @@ func (pos *Position) CastleRights() CastleRights { return pos.castleRights } +// InCheck returns true if the king is in check in the position. +func (pos *Position) InCheck() bool { + return pos.inCheck +} + +// CheckSquare returns the square containing the checked king. +func (pos *Position) CheckSquare() Square { + if pos.inCheck { + return pos.activeKingSquare() + } + + return NoSquare +} + // String implements the fmt.Stringer interface and returns a // string with the OFEN format: ppkn/4/4/NKPP w NCFncf - 0 1 func (pos *Position) String() string { @@ -368,6 +382,14 @@ func removeCastlingRight(rights *string, removedRight string) { *rights = strings.Replace(*rights, removedRight, "", -1) } +func (pos *Position) activeKingSquare() Square { + kingSq := pos.board.whiteKingSq + if pos.Turn() == Black { + kingSq = pos.board.blackKingSq + } + return kingSq +} + func (pos *Position) updateEnPassantSquare(m *Move) Square { p := pos.board.Piece(m.s1) if p.Type() != Pawn {