mchess-server/chess/free_squares.go

42 lines
1.3 KiB
Go

package chess
import "mchess_server/types"
func (b *Board) GetNonBlockedRowAndColumn(fromSquare types.Coordinate) []types.Coordinate {
squaresLeft := fromSquare.GetAllSquaresLeft()
squaresRight := fromSquare.GetAllSquaresRight()
squaresAbove := fromSquare.GetAllSquaresAbove()
squaresBelow := fromSquare.GetAllSquaresBelow()
nonBlocked := []types.Coordinate{}
nonBlocked = append(nonBlocked, b.getNonBlocked(squaresLeft, fromSquare)...)
nonBlocked = append(nonBlocked, b.getNonBlocked(squaresRight, fromSquare)...)
nonBlocked = append(nonBlocked, b.getNonBlocked(squaresAbove, fromSquare)...)
nonBlocked = append(nonBlocked, b.getNonBlocked(squaresBelow, fromSquare)...)
return nonBlocked
}
func (b *Board) getNonBlocked(
squaresToCheck []types.Coordinate,
sourceSquare types.Coordinate,
) []types.Coordinate {
pieceOnSourceSquare := b.getPieceAt(sourceSquare)
nonBlocked := []types.Coordinate{}
for _, square := range squaresToCheck {
piece := b.getPieceAt(square)
if piece != nil {
if piece.GetColor() == pieceOnSourceSquare.GetColor() {
break
}
// if there is an opposite colored piece we append it but
// stop appending the squares behind it
nonBlocked = append(nonBlocked, square)
break
}
nonBlocked = append(nonBlocked, square)
}
return nonBlocked
}