Lots of changes
This commit is contained in:
@@ -1,4 +1,12 @@
|
|||||||
public class Board: CustomStringConvertible {
|
enum Event {
|
||||||
|
case kingInCheck(_ by: Piece)
|
||||||
|
case piecePinned(from: Piece, on: Piece)
|
||||||
|
}
|
||||||
|
protocol EventDelegate {
|
||||||
|
func notify(_ event: Event)
|
||||||
|
func movePiece(_ piece: Piece, to dst: Square.Position)
|
||||||
|
}
|
||||||
|
public class Board: CustomStringConvertible, EventDelegate {
|
||||||
public typealias Grid = [[Square]]
|
public typealias Grid = [[Square]]
|
||||||
|
|
||||||
private enum UnicodeBar: String, CustomStringConvertible {
|
private enum UnicodeBar: String, CustomStringConvertible {
|
||||||
@@ -12,6 +20,18 @@ public class Board: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func notify(_ event: Event) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func movePiece(_ piece: Piece, to dst: Square.Position) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public func getSquareInfo(on pos: Square.Position) -> Square? {
|
||||||
|
self[pos]
|
||||||
|
}
|
||||||
|
|
||||||
private var squares = [Square]()
|
private var squares = [Square]()
|
||||||
private var board: Grid {
|
private var board: Grid {
|
||||||
var board = Grid()
|
var board = Grid()
|
||||||
@@ -37,7 +57,8 @@ public class Board: CustomStringConvertible {
|
|||||||
let r = 8 - Int(rank)
|
let r = 8 - Int(rank)
|
||||||
if c == "/" {
|
if c == "/" {
|
||||||
if file != 9 {
|
if file != 9 {
|
||||||
throw Fen.FenError.NotAppropriateLength(n: file, column: index)
|
throw Fen.FenError.NotAppropriateLength(
|
||||||
|
n: file, column: index)
|
||||||
}
|
}
|
||||||
rank -= 1
|
rank -= 1
|
||||||
file = 0
|
file = 0
|
||||||
@@ -60,18 +81,19 @@ public class Board: CustomStringConvertible {
|
|||||||
let piece: Piece =
|
let piece: Piece =
|
||||||
switch k {
|
switch k {
|
||||||
case .Pawn:
|
case .Pawn:
|
||||||
Pawn(color: c, on: .init(rank: rank, file: file))
|
Pawn(with: c, on: .init(rank: rank, file: file))
|
||||||
case .Knight:
|
case .Knight:
|
||||||
Knight(color: c, on: .init(rank: rank, file: file))
|
Knight(with: c, on: .init(rank: rank, file: file))
|
||||||
case .Bishop:
|
case .Bishop:
|
||||||
Bishop(color: c, on: .init(rank: rank, file: file))
|
Bishop(with: c, on: .init(rank: rank, file: file))
|
||||||
case .Rook:
|
case .Rook:
|
||||||
Rook(color: c, on: .init(rank: rank, file: file))
|
Rook(with: c, on: .init(rank: rank, file: file))
|
||||||
case .Queen:
|
case .Queen:
|
||||||
Queen(color: c, on: .init(rank: rank, file: file))
|
Queen(with: c, on: .init(rank: rank, file: file))
|
||||||
case .King:
|
case .King:
|
||||||
King(color: c, on: .init(rank: rank, file: file))
|
King(with: c, on: .init(rank: rank, file: file))
|
||||||
}
|
}
|
||||||
|
piece.delegate = self
|
||||||
b[8 * r + Int(file) - 1].piece = piece
|
b[8 * r + Int(file) - 1].piece = piece
|
||||||
case .none:
|
case .none:
|
||||||
throw Fen.FenError.InvalidCharacter(
|
throw Fen.FenError.InvalidCharacter(
|
||||||
@@ -88,8 +110,10 @@ public class Board: CustomStringConvertible {
|
|||||||
squares = b
|
squares = b
|
||||||
}
|
}
|
||||||
|
|
||||||
public required init(fen: Fen =
|
public required init(
|
||||||
.init(fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
|
fen: Fen =
|
||||||
|
.init(
|
||||||
|
fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
|
||||||
) {
|
) {
|
||||||
var rank: Int8 = 8
|
var rank: Int8 = 8
|
||||||
self.fen = fen
|
self.fen = fen
|
||||||
@@ -134,7 +158,7 @@ public class Board: CustomStringConvertible {
|
|||||||
return boardString
|
return boardString
|
||||||
}
|
}
|
||||||
|
|
||||||
public subscript(pos: Square.Position) -> Square? {
|
internal subscript(pos: Square.Position) -> Square? {
|
||||||
guard let i = pos.index else {
|
guard let i = pos.index else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +1,26 @@
|
|||||||
final class Bishop: Piece, DiagonalMoves {
|
final class Bishop: Piece, DiagonalMoves {
|
||||||
weak var board: Board?
|
|
||||||
var kind: Kind = .Bishop
|
|
||||||
|
|
||||||
var unicodeRepresentation: String {
|
override var unicodeRepresentation: String {
|
||||||
return color == .Black ? "♝" : "♗"
|
return color == .Black ? "♝" : "♗"
|
||||||
}
|
}
|
||||||
var color: Color
|
|
||||||
|
|
||||||
var position: Square.Position
|
override var pseudoLegalPositions: [Square.Position] {
|
||||||
|
|
||||||
var pseudoLegalPositions: [Square.Position] {
|
|
||||||
return getDiagonalMoves(from: position)
|
return getDiagonalMoves(from: position)
|
||||||
}
|
}
|
||||||
|
|
||||||
var legalPositions: [Square.Position] {
|
override var legalPositions: [Square.Position] {
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
||||||
}
|
}
|
||||||
|
|
||||||
func move(to dst: Square.Position) -> Bool {
|
override func move(to dst: Square.Position) {
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
if let board = board, let s = board[pos] {
|
if let board = board, let s = board[pos] {
|
||||||
if let p = s.piece {
|
if let p = s.piece {
|
||||||
if p.color == color { return false }
|
if p.color == color { return false }
|
||||||
if p.kind == .King {
|
if p.kind == .King {
|
||||||
// TODO: Notify board of check
|
delegate?.notify(.kingInCheck(self))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,9 +28,8 @@ final class Bishop: Piece, DiagonalMoves {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
self.color = color
|
super.init(kind: .Bishop, on: position, with: color)
|
||||||
self.position = position
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,10 @@
|
|||||||
final class King: Piece {
|
final class King: Piece {
|
||||||
weak var board: Board?
|
|
||||||
var kind: Kind = .King
|
|
||||||
|
|
||||||
var unicodeRepresentation: String {
|
override var unicodeRepresentation: String {
|
||||||
return color == .Black ? "♛" : "♕"
|
return color == .Black ? "♛" : "♕"
|
||||||
}
|
}
|
||||||
var color: Color
|
|
||||||
|
|
||||||
var position: Square.Position
|
override var pseudoLegalPositions: [Square.Position] {
|
||||||
|
|
||||||
var pseudoLegalPositions: [Square.Position] {
|
|
||||||
[
|
[
|
||||||
position + (1, 0),
|
position + (1, 0),
|
||||||
position + (1, 1),
|
position + (1, 1),
|
||||||
@@ -18,19 +13,18 @@ final class King: Piece {
|
|||||||
position + (-1, 0),
|
position + (-1, 0),
|
||||||
position + (-1, -1),
|
position + (-1, -1),
|
||||||
position + (0, -1),
|
position + (0, -1),
|
||||||
position + (1, -1)
|
position + (1, -1),
|
||||||
].filter { $0.index != nil }
|
].filter { $0.index != nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
var legalPositions: [Square.Position] {
|
override var legalPositions: [Square.Position] {
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
||||||
}
|
}
|
||||||
|
|
||||||
func move(to dst: Square.Position) -> Bool {
|
override func move(to dst: Square.Position) {
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
if let board = board, let s = board[pos] {
|
if let board = board, let s = board[pos] {
|
||||||
if let p = s.piece {
|
if let p = s.piece {
|
||||||
if p.color == color { return false }
|
if p.color == color { return false }
|
||||||
@@ -44,9 +38,8 @@ final class King: Piece {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
self.color = color
|
super.init(kind: .King, on: position, with: color)
|
||||||
self.position = position
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,9 @@
|
|||||||
final class Knight: Piece {
|
final class Knight: Piece {
|
||||||
weak var board: Board?
|
override var unicodeRepresentation: String {
|
||||||
var kind: Kind = .Knight
|
|
||||||
|
|
||||||
var unicodeRepresentation: String {
|
|
||||||
return color == .Black ? "♞" : "♘"
|
return color == .Black ? "♞" : "♘"
|
||||||
}
|
}
|
||||||
var color: Color
|
|
||||||
|
|
||||||
var position: Square.Position
|
override var pseudoLegalPositions: [Square.Position] {
|
||||||
|
|
||||||
var pseudoLegalPositions: [Square.Position] {
|
|
||||||
[
|
[
|
||||||
position + (2, 1),
|
position + (2, 1),
|
||||||
position + (2, -1),
|
position + (2, -1),
|
||||||
@@ -18,24 +12,24 @@ final class Knight: Piece {
|
|||||||
position + (1, 2),
|
position + (1, 2),
|
||||||
position + (1, -2),
|
position + (1, -2),
|
||||||
position + (-1, 2),
|
position + (-1, 2),
|
||||||
position + (-1, -2)
|
position + (-1, -2),
|
||||||
].filter { $0.index != nil }
|
].filter { $0.index != nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
var legalPositions: [Square.Position] {
|
override var legalPositions: [Square.Position] {
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
||||||
}
|
}
|
||||||
|
|
||||||
func move(to dst: Square.Position) -> Bool {
|
override func move(to dst: Square.Position) {
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
if let board = board, let s = board[pos] {
|
if let board = board, let s = board[pos] {
|
||||||
if let p = s.piece {
|
if let p = s.piece {
|
||||||
if p.color == color { return false }
|
if p.color == color { return false }
|
||||||
if p.kind == .King {
|
if p.kind == .King {
|
||||||
// TODO: Notify board of check
|
// TODO: Notify board of check
|
||||||
|
delegate?.notify(.kingInCheck(self))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -44,9 +38,8 @@ final class Knight: Piece {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
self.color = color
|
super.init(kind: .Knight, on: position, with: color)
|
||||||
self.position = position
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,51 +1,47 @@
|
|||||||
final class Pawn: Piece {
|
final class Pawn: Piece {
|
||||||
var kind: Kind = .Pawn
|
override var pseudoLegalPositions: [Square.Position] {
|
||||||
weak var board: Board?
|
|
||||||
var color: Color
|
|
||||||
var position: Square.Position
|
|
||||||
var pseudoLegalPositions: [Square.Position] {
|
|
||||||
let sign: Int8 = color == .Black ? -1 : 1
|
let sign: Int8 = color == .Black ? -1 : 1
|
||||||
return [
|
return [
|
||||||
position + (1 * sign, 0),
|
position + (1 * sign, 0),
|
||||||
position + (2 * sign, 0),
|
position + (2 * sign, 0),
|
||||||
position + (1 * sign, 1),
|
position + (1 * sign, 1),
|
||||||
position + (1 * sign, -1)
|
position + (1 * sign, -1),
|
||||||
].filter { $0.index != nil }
|
].filter { $0.index != nil }
|
||||||
}
|
}
|
||||||
var legalPositions: [Square.Position] {
|
|
||||||
|
override var legalPositions: [Square.Position] {
|
||||||
pseudoLegalPositions.filter { isLegal(on: $0) }
|
pseudoLegalPositions.filter { isLegal(on: $0) }
|
||||||
}
|
}
|
||||||
|
|
||||||
var unicodeRepresentation: String {
|
override var unicodeRepresentation: String {
|
||||||
color == .Black ? "♟" : "♙"
|
color == .Black ? "♟" : "♙"
|
||||||
}
|
}
|
||||||
|
|
||||||
func move(to dst: Square.Position) -> Bool {
|
override func move(to dst: Square.Position) {
|
||||||
guard board != nil else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if !(legalPositions.contains { $0 == dst }) {
|
if !(legalPositions.contains { $0 == dst }) {
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if let board = board, var s = board[position], var d = board[dst] {
|
delegate?.movePiece(self, to: dst)
|
||||||
s.piece = self
|
|
||||||
d.piece = nil
|
// if let board = board, var s = board[position], var d = board[dst] {
|
||||||
|
// s.piece = self
|
||||||
|
// d.piece = nil
|
||||||
|
// }
|
||||||
|
|
||||||
|
// position = dst
|
||||||
|
|
||||||
|
// return true
|
||||||
}
|
}
|
||||||
|
|
||||||
position = dst
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func isLegal(on pos: Square.Position) -> Bool {
|
|
||||||
// TODO: Handle "En-Passant"
|
// TODO: Handle "En-Passant"
|
||||||
if let board = board, let s = board[pos] {
|
if let board = board, let s = board[pos] {
|
||||||
if let p = s.piece {
|
if let p = s.piece {
|
||||||
if p.color == color { return false }
|
if p.color == color { return false }
|
||||||
if p.kind == .King {
|
if p.kind == .King {
|
||||||
// TODO: Notify board of check
|
// TODO: Notify board of check
|
||||||
|
delegate?.notify(.kingInCheck(self))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,10 +50,7 @@ final class Pawn: Piece {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(
|
init(with color: Color, on position: Square.Position) {
|
||||||
color: Color, on position: Square.Position
|
super.init(kind: .Pawn, on: position, with: color)
|
||||||
) {
|
|
||||||
self.color = color
|
|
||||||
self.position = position
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
enum Kind: String, CaseIterable {
|
public enum Kind: String, CaseIterable {
|
||||||
case Pawn, Knight, Bishop, Rook, Queen, King
|
case Pawn, Knight, Bishop, Rook, Queen, King
|
||||||
|
|
||||||
var value: Int8 {
|
var value: Int8 {
|
||||||
@@ -39,14 +39,28 @@ enum Kind: String, CaseIterable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protocol Piece {
|
public class Piece {
|
||||||
var board: Board? { get }
|
#warning("TODO: TO be removed, handle everything through the delegate")
|
||||||
var color: Color { get }
|
internal weak var board: Board?
|
||||||
var unicodeRepresentation: String { get }
|
public internal(set) var color: Color
|
||||||
var kind: Kind { get }
|
public var unicodeRepresentation: String {
|
||||||
var position: Square.Position { get }
|
return ""
|
||||||
var pseudoLegalPositions: [Square.Position] { get }
|
}
|
||||||
var legalPositions: [Square.Position] { get }
|
public internal(set) var kind: Kind
|
||||||
func move(to dst: Square.Position) -> Bool
|
public internal(set) var position: Square.Position
|
||||||
func isLegal(on pos: Square.Position) -> Bool
|
internal var pseudoLegalPositions: [Square.Position] {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
internal var legalPositions: [Square.Position] {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
internal var delegate: EventDelegate?
|
||||||
|
internal func move(to dst: Square.Position) {}
|
||||||
|
internal func isLegal(on pos: Square.Position) -> Bool { false }
|
||||||
|
|
||||||
|
internal init(kind: Kind, on pos: Square.Position, with col: Color) {
|
||||||
|
self.kind = kind
|
||||||
|
self.position = pos
|
||||||
|
self.color = col
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +1,27 @@
|
|||||||
final class Queen: Piece, LinearMoves, DiagonalMoves {
|
final class Queen: Piece, LinearMoves, DiagonalMoves {
|
||||||
weak var board: Board?
|
override var unicodeRepresentation: String {
|
||||||
var kind: Kind = .Queen
|
|
||||||
|
|
||||||
var unicodeRepresentation: String {
|
|
||||||
return color == .Black ? "♛" : "♕"
|
return color == .Black ? "♛" : "♕"
|
||||||
}
|
}
|
||||||
var color: Color
|
|
||||||
|
|
||||||
var position: Square.Position
|
override var pseudoLegalPositions: [Square.Position] {
|
||||||
|
|
||||||
var pseudoLegalPositions: [Square.Position] {
|
|
||||||
return getDiagonalMoves(from: position) + getLinearMoves(from: position)
|
return getDiagonalMoves(from: position) + getLinearMoves(from: position)
|
||||||
}
|
}
|
||||||
|
|
||||||
var legalPositions: [Square.Position] {
|
override var legalPositions: [Square.Position] {
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
||||||
}
|
}
|
||||||
|
|
||||||
func move(to dst: Square.Position) -> Bool {
|
override func move(to dst: Square.Position) {
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
if let board = board, let s = board[pos] {
|
if let board = board, let s = board[pos] {
|
||||||
if let p = s.piece {
|
if let p = s.piece {
|
||||||
if p.color == color { return false }
|
if p.color == color { return false }
|
||||||
if p.kind == .King {
|
if p.kind == .King {
|
||||||
// TODO: Notify board of check
|
// TODO: Notify board of check
|
||||||
|
delegate?.notify(.kingInCheck(self))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,9 +30,8 @@ final class Queen: Piece, LinearMoves, DiagonalMoves {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
self.color = color
|
super.init(kind: .Queen, on: position, with: color)
|
||||||
self.position = position
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +1,27 @@
|
|||||||
final class Rook: Piece, LinearMoves {
|
final class Rook: Piece, LinearMoves {
|
||||||
weak var board: Board?
|
|
||||||
var kind: Kind = .Rook
|
|
||||||
|
|
||||||
var unicodeRepresentation: String {
|
override var unicodeRepresentation: String {
|
||||||
return color == .Black ? "♜" : "♖"
|
return color == .Black ? "♜" : "♖"
|
||||||
}
|
}
|
||||||
var color: Color
|
|
||||||
|
|
||||||
var position: Square.Position
|
override var pseudoLegalPositions: [Square.Position] {
|
||||||
|
|
||||||
var pseudoLegalPositions: [Square.Position] {
|
|
||||||
return getLinearMoves(from: position)
|
return getLinearMoves(from: position)
|
||||||
}
|
}
|
||||||
|
|
||||||
var legalPositions: [Square.Position] {
|
override var legalPositions: [Square.Position] {
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
||||||
}
|
}
|
||||||
|
|
||||||
func move(to dst: Square.Position) -> Bool {
|
override func move(to dst: Square.Position) {
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
if let board = board, let s = board[pos] {
|
if let board = board, let s = board[pos] {
|
||||||
if let p = s.piece {
|
if let p = s.piece {
|
||||||
if p.color == color { return false }
|
if p.color == color { return false }
|
||||||
if p.kind == .King {
|
if p.kind == .King {
|
||||||
// TODO: Notify board of check
|
// TODO: Notify board of check
|
||||||
|
delegate?.notify(.kingInCheck(self))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,8 +30,10 @@ final class Rook: Piece, LinearMoves {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
|
super.init(kind: .Rook, on: position, with: color)
|
||||||
self.color = color
|
self.color = color
|
||||||
|
self.kind = .Rook
|
||||||
self.position = position
|
self.position = position
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,33 @@ public struct Square: Equatable {
|
|||||||
return Int(8 * (8 - rank) + file - 1)
|
return Int(8 * (8 - rank) + file - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public init(rank r: Int8, file f: Int8) {
|
||||||
|
rank = r
|
||||||
|
file = f
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum File: String, CustomStringConvertible {
|
||||||
|
case a, b, c, d, e, f, g, h
|
||||||
|
|
||||||
|
public static subscript(_ f: Int8) -> Self? {
|
||||||
|
return switch f {
|
||||||
|
case 1: Self.a
|
||||||
|
case 2: Self.b
|
||||||
|
case 3: Self.c
|
||||||
|
case 4: Self.d
|
||||||
|
case 5: Self.e
|
||||||
|
case 6: Self.f
|
||||||
|
case 7: Self.g
|
||||||
|
case 8: Self.h
|
||||||
|
default: nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
self.rawValue.uppercased()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static func == (lhs: Position, rhs: Position) -> Bool {
|
public static func == (lhs: Position, rhs: Position) -> Bool {
|
||||||
return lhs.index == rhs.index
|
return lhs.index == rhs.index
|
||||||
}
|
}
|
||||||
@@ -34,7 +61,7 @@ public struct Square: Equatable {
|
|||||||
|
|
||||||
public let position: Position
|
public let position: Position
|
||||||
public let index: Int
|
public let index: Int
|
||||||
var piece: Piece? = nil
|
public internal(set) var piece: Piece? = nil
|
||||||
public let color: Color
|
public let color: Color
|
||||||
|
|
||||||
public static func == (lhs: Square, rhs: Square) -> Bool {
|
public static func == (lhs: Square, rhs: Square) -> Bool {
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
import Engine
|
import Engine
|
||||||
|
|
||||||
let board = Board(fen: .init(fen: "rnbqkb1r/pppppppp/2n5/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"))
|
let board = Board(
|
||||||
|
fen: .init(
|
||||||
|
fen: "rnbqkb1r/pppppppp/2n5/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"))
|
||||||
// let board: Board = .init()
|
// let board: Board = .init()
|
||||||
print(board)
|
print(board)
|
||||||
|
|
||||||
|
// Execute every time the timer changes
|
||||||
|
// board.on(.timer) { time in
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// let square = board.getSquareInfo(on: .init(rank: 2, file: 7))
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ final class EngineTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
func testDiagonalMoves() throws {
|
func testDiagonalMoves() throws {
|
||||||
let b: Bishop = .init(color: .White, on: .init(rank: 4, file: 4))
|
let b: Bishop = .init(with: .White, on: .init(rank: 4, file: 4))
|
||||||
let result = [
|
let result = [
|
||||||
Square.Position(rank: 5, file: 3),
|
Square.Position(rank: 5, file: 3),
|
||||||
Square.Position(rank: 6, file: 2),
|
Square.Position(rank: 6, file: 2),
|
||||||
@@ -39,7 +39,7 @@ final class EngineTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testLinearMoves() throws {
|
func testLinearMoves() throws {
|
||||||
let r: Rook = .init(color: .White, on: .init(rank: 4, file: 4))
|
let r: Rook = .init(with: .White, on: .init(rank: 4, file: 4))
|
||||||
let result = [
|
let result = [
|
||||||
Square.Position(rank: 5, file: 4),
|
Square.Position(rank: 5, file: 4),
|
||||||
Square.Position(rank: 6, file: 4),
|
Square.Position(rank: 6, file: 4),
|
||||||
@@ -60,7 +60,7 @@ final class EngineTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testKingMoves() throws {
|
func testKingMoves() throws {
|
||||||
let k: King = .init(color: .White, on: .init(rank: 2, file: 8))
|
let k: King = .init(with: .White, on: .init(rank: 2, file: 8))
|
||||||
let result = [
|
let result = [
|
||||||
Square.Position(rank: 3, file: 8),
|
Square.Position(rank: 3, file: 8),
|
||||||
Square.Position(rank: 1, file: 8),
|
Square.Position(rank: 1, file: 8),
|
||||||
@@ -72,7 +72,7 @@ final class EngineTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testKnightMoves() throws {
|
func testKnightMoves() throws {
|
||||||
let n: Knight = .init(color: .White, on: .init(rank: 4, file: 4))
|
let n: Knight = .init(with: .White, on: .init(rank: 4, file: 4))
|
||||||
let result = [
|
let result = [
|
||||||
Square.Position(rank: 6, file: 5),
|
Square.Position(rank: 6, file: 5),
|
||||||
Square.Position(rank: 6, file: 3),
|
Square.Position(rank: 6, file: 3),
|
||||||
@@ -87,7 +87,7 @@ final class EngineTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testPawnMoves() throws {
|
func testPawnMoves() throws {
|
||||||
let pw: Pawn = .init(color: .White, on: .init(rank: 4, file: 4))
|
let pw: Pawn = .init(with: .White, on: .init(rank: 4, file: 4))
|
||||||
var result = [
|
var result = [
|
||||||
Square.Position(rank: 5, file: 4),
|
Square.Position(rank: 5, file: 4),
|
||||||
Square.Position(rank: 6, file: 4),
|
Square.Position(rank: 6, file: 4),
|
||||||
@@ -95,7 +95,7 @@ final class EngineTests: XCTestCase {
|
|||||||
Square.Position(rank: 5, file: 3),
|
Square.Position(rank: 5, file: 3),
|
||||||
]
|
]
|
||||||
XCTAssertEqual(result, pw.pseudoLegalPositions)
|
XCTAssertEqual(result, pw.pseudoLegalPositions)
|
||||||
let pb: Pawn = .init(color: .Black, on: .init(rank: 4, file: 4))
|
let pb: Pawn = .init(with: .Black, on: .init(rank: 4, file: 4))
|
||||||
result = [
|
result = [
|
||||||
Square.Position(rank: 3, file: 4),
|
Square.Position(rank: 3, file: 4),
|
||||||
Square.Position(rank: 2, file: 4),
|
Square.Position(rank: 2, file: 4),
|
||||||
|
|||||||
Reference in New Issue
Block a user