Some refactor and tests
This commit is contained in:
@@ -1,18 +1,21 @@
|
||||
fileprivate func getDirectionalMoves(from pos: Square.Position, with dir: [(Int8, Int8)]) -> [Square.Position] {
|
||||
var squares = [Square.Position]()
|
||||
for i: (Int8, Int8) in dir {
|
||||
var currentSquare = pos + i
|
||||
while currentSquare.index != nil {
|
||||
squares.append(currentSquare)
|
||||
currentSquare += i
|
||||
}
|
||||
}
|
||||
return squares
|
||||
}
|
||||
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
|
||||
getDirectionalMoves(from: pos, with: [(1, -1), (1, 1), (-1, 1), (-1, -1)])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,14 +25,6 @@ protocol LinearMoves {
|
||||
|
||||
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
|
||||
getDirectionalMoves(from: pos, with: [(1, 0), (0, 1), (-1, 0), (0, -1)])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user