notnil / chess

chess package for go
MIT License
508 stars 126 forks source link

How to loop over all the pieces? #123

Closed LouieMartin closed 4 months ago

LouieMartin commented 1 year ago

I was looking on how to loop for all the pieces like:

for _, piece := range position.Pieces() {
  // logic
}
jpope6 commented 6 months ago

My solution to looping through all the pieces was to use the 'SquareMap'. According to the documentation: "SquareMap returns a mapping of squares to pieces. A square is only added to the map if it is occupied."

squareMap := game.Position().Board().SquareMap()

for square, piece := range squareMap {
    // logic
}