Linear + Diagonal moves
This commit is contained in:
@@ -1,40 +1,37 @@
|
||||
internal final class Knight: Piece {
|
||||
internal weak var board: Board?
|
||||
internal var kind: Kind = .Knight
|
||||
final class Knight: Piece {
|
||||
weak var board: Board?
|
||||
var kind: Kind = .Knight
|
||||
|
||||
internal var unicodeRepresentation: String {
|
||||
var unicodeRepresentation: String {
|
||||
return color == .Black ? "♞" : "♘"
|
||||
}
|
||||
internal var color: Color
|
||||
var color: Color
|
||||
|
||||
internal var position: Square.Position
|
||||
var position: Square.Position
|
||||
|
||||
internal var pseudoLegalPositions: [Square.Position] {
|
||||
var pseudoLegalPositions: [Square.Position] {
|
||||
let directions: [Square.Position] = [
|
||||
.init(file: position.file + 1, rank: position.rank + 2),
|
||||
.init(file: position.file - 1, rank: position.rank + 2),
|
||||
.init(file: position.file + 1, rank: position.rank - 2),
|
||||
.init(file: position.file - 1, rank: position.rank - 2),
|
||||
.init(file: position.file + 2, rank: position.rank + 1),
|
||||
.init(file: position.file - 2, rank: position.rank + 1),
|
||||
.init(file: position.file + 2, rank: position.rank - 1),
|
||||
.init(file: position.file - 2, rank: position.rank - 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 - 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 {
|
||||
let index = $0.index
|
||||
return index < 0 || index > 63
|
||||
}
|
||||
return directions.filter { $0.index != nil }
|
||||
}
|
||||
|
||||
internal var legalPositions: [Square.Position] {
|
||||
var legalPositions: [Square.Position] {
|
||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
||||
}
|
||||
|
||||
internal func move(to dst: Square.Position) -> Bool {
|
||||
func move(to dst: Square.Position) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
internal func isLegal(on pos: Square.Position) -> Bool {
|
||||
func isLegal(on pos: Square.Position) -> Bool {
|
||||
if let board = board, let s = board[pos] {
|
||||
if let p = s.piece {
|
||||
if p.color == color { return false }
|
||||
@@ -48,7 +45,7 @@ internal final class Knight: Piece {
|
||||
return true
|
||||
}
|
||||
|
||||
internal init(color: Color, on position: Square.Position) {
|
||||
init(color: Color, on position: Square.Position) {
|
||||
self.color = color
|
||||
self.position = position
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user