Skip to content

Commit

Permalink
feat: 1.0.1 added InCheck and CheckSquare to Position
Browse files Browse the repository at this point in the history
  • Loading branch information
dechristopher committed Mar 2, 2021
1 parent f3110aa commit 227b342
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
5 changes: 1 addition & 4 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions liad/liad.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
22 changes: 22 additions & 0 deletions position.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 227b342

Please sign in to comment.