mchess-server/chess/free_squares.go
2023-06-27 22:32:24 +02:00

60 lines
2.3 KiB
Go

package chess
import "mchess_server/types"
func (b *Board) GetNonBlockedRowAndColumn(fromSquare types.Coordinate) []types.Coordinate {
squaresLeft := fromSquare.GetStraightInDirection(types.Coordinate.Left)
squaresRight := fromSquare.GetStraightInDirection(types.Coordinate.Right)
squaresAbove := fromSquare.GetStraightInDirection(types.Coordinate.Up)
squaresBelow := fromSquare.GetStraightInDirection(types.Coordinate.Down)
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) GetNonBlockedDiagonals(fromSquare types.Coordinate) []types.Coordinate {
rightUp := fromSquare.GetDiagonalInDirection(types.Coordinate.Right, types.Coordinate.Up)
leftUp := fromSquare.GetDiagonalInDirection(types.Coordinate.Left, types.Coordinate.Up)
leftDown := fromSquare.GetDiagonalInDirection(types.Coordinate.Left, types.Coordinate.Down)
rightDown := fromSquare.GetDiagonalInDirection(types.Coordinate.Right, types.Coordinate.Down)
nonBlocked := b.getNonBlocked(rightUp, fromSquare)
nonBlocked = append(nonBlocked, b.getNonBlocked(leftUp, fromSquare)...)
nonBlocked = append(nonBlocked, b.getNonBlocked(leftDown, fromSquare)...)
nonBlocked = append(nonBlocked, b.getNonBlocked(rightDown, fromSquare)...)
return nonBlocked
}
func (b *Board) GetNonBlockedKnightMoves(fromSquare types.Coordinate) []types.Coordinate {
allKnightMoves := fromSquare.GetAllKnightMoves()
return b.getNonBlocked(allKnightMoves, fromSquare)
}
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
}