2023-06-26 21:58:40 +00:00
|
|
|
package chess
|
|
|
|
|
|
|
|
import "mchess_server/types"
|
|
|
|
|
|
|
|
func (b *Board) GetNonBlockedRowAndColumn(fromSquare types.Coordinate) []types.Coordinate {
|
2023-06-27 20:32:24 +00:00
|
|
|
squaresLeft := fromSquare.GetStraightInDirection(types.Coordinate.Left)
|
|
|
|
squaresRight := fromSquare.GetStraightInDirection(types.Coordinate.Right)
|
|
|
|
squaresAbove := fromSquare.GetStraightInDirection(types.Coordinate.Up)
|
|
|
|
squaresBelow := fromSquare.GetStraightInDirection(types.Coordinate.Down)
|
|
|
|
|
2023-06-27 21:47:24 +00:00
|
|
|
nonBlocked := b.getNonBlockedConsecutive(squaresLeft, fromSquare)
|
|
|
|
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresRight, fromSquare)...)
|
|
|
|
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresAbove, fromSquare)...)
|
|
|
|
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresBelow, fromSquare)...)
|
2023-06-27 20:32:24 +00:00
|
|
|
|
|
|
|
return nonBlocked
|
|
|
|
}
|
2023-06-26 21:58:40 +00:00
|
|
|
|
2023-06-27 20:32:24 +00:00
|
|
|
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)
|
|
|
|
|
2023-06-27 21:47:24 +00:00
|
|
|
nonBlocked := b.getNonBlockedConsecutive(rightUp, fromSquare)
|
|
|
|
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(leftUp, fromSquare)...)
|
|
|
|
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(leftDown, fromSquare)...)
|
|
|
|
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(rightDown, fromSquare)...)
|
2023-06-26 21:58:40 +00:00
|
|
|
|
|
|
|
return nonBlocked
|
|
|
|
}
|
|
|
|
|
2023-06-28 06:26:11 +00:00
|
|
|
func (b *Board) GetNonBlockedKingMoves(fromSquare types.Coordinate) []types.Coordinate {
|
|
|
|
return b.getNonBlockedConsecutive(fromSquare.GetAllKingMoves(), fromSquare)
|
|
|
|
}
|
|
|
|
|
2023-06-27 20:32:24 +00:00
|
|
|
func (b *Board) GetNonBlockedKnightMoves(fromSquare types.Coordinate) []types.Coordinate {
|
|
|
|
allKnightMoves := fromSquare.GetAllKnightMoves()
|
2023-06-27 21:47:24 +00:00
|
|
|
return b.getNonBlockedForKnights(allKnightMoves, fromSquare)
|
2023-06-27 20:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 21:47:24 +00:00
|
|
|
func (b *Board) getNonBlockedConsecutive(
|
2023-06-27 20:32:24 +00:00
|
|
|
squaresToCheck []types.Coordinate,
|
|
|
|
sourceSquare types.Coordinate,
|
2023-06-26 21:58:40 +00:00
|
|
|
) []types.Coordinate {
|
|
|
|
pieceOnSourceSquare := b.getPieceAt(sourceSquare)
|
2023-06-27 20:32:24 +00:00
|
|
|
nonBlocked := []types.Coordinate{}
|
2023-06-26 21:58:40 +00:00
|
|
|
|
|
|
|
for _, square := range squaresToCheck {
|
|
|
|
piece := b.getPieceAt(square)
|
|
|
|
if piece != nil {
|
|
|
|
if piece.GetColor() == pieceOnSourceSquare.GetColor() {
|
2023-06-27 21:47:24 +00:00
|
|
|
//We do not append squares with same-colored pieces
|
|
|
|
//and also not the squares behind it
|
2023-06-26 21:58:40 +00:00
|
|
|
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)
|
|
|
|
}
|
2023-06-27 20:32:24 +00:00
|
|
|
return nonBlocked
|
2023-06-26 21:58:40 +00:00
|
|
|
}
|
2023-06-27 21:47:24 +00:00
|
|
|
|
|
|
|
func (b *Board) getNonBlockedForKnights(
|
|
|
|
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 || piece.GetColor() != pieceOnSourceSquare.GetColor() {
|
|
|
|
nonBlocked = append(nonBlocked, square)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nonBlocked
|
|
|
|
}
|