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)])
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,16 @@ final class King: Piece {
|
||||
var position: Square.Position
|
||||
|
||||
var pseudoLegalPositions: [Square.Position] {
|
||||
return []
|
||||
[
|
||||
position + (1, 0),
|
||||
position + (1, 1),
|
||||
position + (0, 1),
|
||||
position + (-1, 1),
|
||||
position + (-1, 0),
|
||||
position + (-1, -1),
|
||||
position + (0, -1),
|
||||
position + (1, -1)
|
||||
].filter {$0.index != nil}
|
||||
}
|
||||
|
||||
var legalPositions: [Square.Position] {
|
||||
|
||||
@@ -10,17 +10,16 @@ final class Knight: Piece {
|
||||
var position: Square.Position
|
||||
|
||||
var pseudoLegalPositions: [Square.Position] {
|
||||
let directions: [Square.Position] = [
|
||||
.init(rank: position.rank + 2, file: position.file + 1),
|
||||
.init(rank: position.rank + 2, file: position.file - 1),
|
||||
.init(rank: position.rank - 2, file: position.file + 1),
|
||||
.init(rank: position.rank - 2, file: position.file - 1),
|
||||
.init(rank: position.rank + 1, file: position.file + 2),
|
||||
.init(rank: position.rank + 1, file: position.file - 2),
|
||||
.init(rank: position.rank - 1, file: position.file + 2),
|
||||
.init(rank: position.rank - 1, file: position.file - 2)
|
||||
]
|
||||
return directions.filter { $0.index != nil }
|
||||
[
|
||||
position + (2, 1),
|
||||
position + (2, -1),
|
||||
position + (-2, 1),
|
||||
position + (-2, -1),
|
||||
position + (1, 2),
|
||||
position + (1, -2),
|
||||
position + (-1, 2),
|
||||
position + (-1, -2)
|
||||
].filter { $0.index != nil }
|
||||
}
|
||||
|
||||
var legalPositions: [Square.Position] {
|
||||
|
||||
@@ -5,15 +5,12 @@ final class Pawn: Piece {
|
||||
var position: Square.Position
|
||||
var pseudoLegalPositions: [Square.Position] {
|
||||
let sign: Int8 = color == .Black ? -1 : 1
|
||||
let rank = Int8(position.rank)
|
||||
let directions: [Square.Position] = [
|
||||
.init(rank: rank + 1 * sign, file: position.file),
|
||||
.init(rank: rank + 2 * sign, file: position.file),
|
||||
.init(rank: rank + 1 * sign, file: position.file + 1),
|
||||
.init(rank: rank + 1 * sign, file: position.file - 1)
|
||||
]
|
||||
// TODO: Handle en passant
|
||||
return directions.filter { $0.index != nil }
|
||||
return [
|
||||
position + (1 * sign, 0),
|
||||
position + (2 * sign, 0),
|
||||
position + (1 * sign, 1),
|
||||
position + (1 * sign, -1)
|
||||
].filter { $0.index != nil }
|
||||
}
|
||||
var legalPositions: [Square.Position] {
|
||||
pseudoLegalPositions.filter { isLegal(on: $0) }
|
||||
|
||||
@@ -14,7 +14,8 @@ final class EngineTests: XCTestCase {
|
||||
rank -= 1
|
||||
}
|
||||
let pos: Square.Position = .init(rank: rank, file: file)
|
||||
XCTAssertTrue(pos.index == Int(i), "Expected \(i) got \(pos.index!)")
|
||||
XCTAssertTrue(
|
||||
pos.index == Int(i), "Expected \(i) got \(pos.index!)")
|
||||
}
|
||||
}
|
||||
func testDiagonalMoves() throws {
|
||||
@@ -57,6 +58,53 @@ final class EngineTests: XCTestCase {
|
||||
]
|
||||
XCTAssertEqual(result, r.pseudoLegalPositions)
|
||||
}
|
||||
|
||||
func testKingMoves() throws {
|
||||
let k: King = .init(color: .White, on: .init(rank: 2, file: 8))
|
||||
let result = [
|
||||
Square.Position(rank: 3, file: 8),
|
||||
Square.Position(rank: 1, file: 8),
|
||||
Square.Position(rank: 1, file: 7),
|
||||
Square.Position(rank: 2, file: 7),
|
||||
Square.Position(rank: 3, file: 7),
|
||||
]
|
||||
XCTAssertEqual(result, k.pseudoLegalPositions)
|
||||
}
|
||||
|
||||
func testKnightMoves() throws {
|
||||
let n: Knight = .init(color: .White, on: .init(rank: 4, file: 4))
|
||||
let result = [
|
||||
Square.Position(rank: 6, file: 5),
|
||||
Square.Position(rank: 6, file: 3),
|
||||
Square.Position(rank: 2, file: 5),
|
||||
Square.Position(rank: 2, file: 3),
|
||||
Square.Position(rank: 5, file: 6),
|
||||
Square.Position(rank: 5, file: 2),
|
||||
Square.Position(rank: 3, file: 6),
|
||||
Square.Position(rank: 3, file: 2),
|
||||
]
|
||||
XCTAssertEqual(result, n.pseudoLegalPositions)
|
||||
}
|
||||
|
||||
func testPawnMoves() throws {
|
||||
let pw: Pawn = .init(color: .White, on: .init(rank: 4, file: 4))
|
||||
var result = [
|
||||
Square.Position(rank: 5, file: 4),
|
||||
Square.Position(rank: 6, file: 4),
|
||||
Square.Position(rank: 5, file: 5),
|
||||
Square.Position(rank: 5, file: 3),
|
||||
]
|
||||
XCTAssertEqual(result, pw.pseudoLegalPositions)
|
||||
let pb: Pawn = .init(color: .Black, on: .init(rank: 4, file: 4))
|
||||
result = [
|
||||
Square.Position(rank: 3, file: 4),
|
||||
Square.Position(rank: 2, file: 4),
|
||||
Square.Position(rank: 3, file: 5),
|
||||
Square.Position(rank: 3, file: 3),
|
||||
]
|
||||
XCTAssertEqual(result, pb.pseudoLegalPositions)
|
||||
}
|
||||
|
||||
// func testBoard() throws {
|
||||
// let board = Board()
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user