Files
swift-chess/Sources/Engine/Pieces/CommonMoves.swift
2024-06-27 18:09:01 +02:00

35 lines
1.0 KiB
Swift

protocol DiagonalMoves {
func getDiagonalMoves(from pos: Square.Position) -> [Square.Position]
}
extension DiagonalMoves {
func getDiagonalMoves(from pos: Square.Position) -> [Square.Position] {
var squares = [Square.Position]()
for i: (Int8, Int8) in [(1, -1), (1, 1), (-1, 1), (-1, -1)] {
var currentSquare = pos + i
while currentSquare.index != nil {
squares.append(currentSquare)
currentSquare += i
}
}
return squares
}
}
protocol LinearMoves {
func getLinearMoves(from pos: Square.Position) -> [Square.Position]
}
extension LinearMoves {
func getLinearMoves(from pos: Square.Position) -> [Square.Position] {
var squares = [Square.Position]()
for i: (Int8, Int8) in [(1, 0), (0, 1), (-1, 0), (0, -1)] {
var currentSquare = pos + i
while currentSquare.index != nil {
squares.append(currentSquare)
currentSquare += i
}
}
return squares
}
}