From bc145da022475fd0138386a7caf1b6d3a0874789 Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:46:58 +0200 Subject: [PATCH] Changed visibility from package to interal --- Sources/Engine/Board.swift | 20 +++++----- Sources/Engine/FEN.swift | 8 ++-- Sources/Engine/Pieces/Bishop.swift | 37 +++++++++++------- Sources/Engine/Pieces/CommonMoves.swift | 19 +++++++++ Sources/Engine/Pieces/King.swift | 36 ++++++++++------- Sources/Engine/Pieces/Knight.swift | 51 ++++++++++++++++++------- Sources/Engine/Pieces/Pawn.swift | 44 ++++++++++++++------- Sources/Engine/Pieces/Piece.swift | 12 +++--- Sources/Engine/Pieces/Queen.swift | 39 ++++++++++++------- Sources/Engine/Pieces/Rook.swift | 38 +++++++++++------- Sources/Engine/Square.swift | 11 +++--- Sources/exe/main.swift | 3 +- 12 files changed, 208 insertions(+), 110 deletions(-) create mode 100644 Sources/Engine/Pieces/CommonMoves.swift diff --git a/Sources/Engine/Board.swift b/Sources/Engine/Board.swift index fe81c7f..c84db00 100644 --- a/Sources/Engine/Board.swift +++ b/Sources/Engine/Board.swift @@ -29,19 +29,19 @@ public class Board: CustomStringConvertible { public private(set) var fen: Fen public func setBoard() throws { - var file: UInt8 = 1 - var rank: UInt8 = 8 - var index: UInt8 = 0 + var file: Int8 = 1 + var rank: Int8 = 8 + var index: Int8 = 0 var b: [Square] = squares for c in fen.placement { - let r = (8 - Int(rank)) % 8 + let r = 8 - Int(rank) if c == "/" { if file != 9 { throw Fen.FenError.NotAppropriateLength(n: file, column: index) } rank -= 1 file = 0 - } else if c.isWholeNumber, let n = UInt8(String(c)) { + } else if c.isWholeNumber, let n = Int8(String(c)) { if n < 1 { throw Fen.FenError.NumberTooSmall(n: n, column: index) } else if n > 8 { @@ -82,20 +82,22 @@ public class Board: CustomStringConvertible { file += 1 index += 1 } - print(b) + #if DEBUG + print(b) + #endif squares = b } public required init(fen: Fen = .init(fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") ) { - var rank: UInt8 = 8 + var rank: Int8 = 8 self.fen = fen for i in 0...63 { - let index = UInt8(i) + let index = Int8(i) let square = Square( position: .init(file: (index % 8) + 1, rank: rank), - index: index, + index: i, color: index % 2 != rank % 2 ? .Black : .White) squares.append(square) if (index + 1) % 8 == 0 { diff --git a/Sources/Engine/FEN.swift b/Sources/Engine/FEN.swift index aea6fc5..62419ec 100644 --- a/Sources/Engine/FEN.swift +++ b/Sources/Engine/FEN.swift @@ -64,10 +64,10 @@ public struct Fen: CustomStringConvertible { } public enum FenError: Error { - case InvalidCharacter(c: String, column: UInt8) - case NumberTooBig(n: UInt8, column: UInt8) - case NumberTooSmall(n: UInt8, column: UInt8) - case NotAppropriateLength(n: UInt8, column: UInt8) + case InvalidCharacter(c: String, column: Int8) + case NumberTooBig(n: Int8, column: Int8) + case NumberTooSmall(n: Int8, column: Int8) + case NotAppropriateLength(n: Int8, column: Int8) } public var description: String { diff --git a/Sources/Engine/Pieces/Bishop.swift b/Sources/Engine/Pieces/Bishop.swift index 932ff01..42de3c4 100644 --- a/Sources/Engine/Pieces/Bishop.swift +++ b/Sources/Engine/Pieces/Bishop.swift @@ -1,31 +1,40 @@ -package class Bishop: Piece { - package weak var board: Board? - package var kind: Kind = .Bishop +internal final class Bishop: Piece, DiagonalMoves { + internal weak var board: Board? + internal var kind: Kind = .Bishop - package var unicodeRepresentation: String { + internal var unicodeRepresentation: String { return color == .Black ? "♝" : "♗" } - package var color: Color + internal var color: Color - package var position: Square.Position + internal var position: Square.Position - package var pseudoLegalPositions: [Square.Position] { - return [] + internal var pseudoLegalPositions: [Square.Position] { + return getDiagonalMoves(from: position) } - package var legalPositions: [Square.Position] { - return pseudoLegalPositions.filter { isLegal(pos: $0) } + internal var legalPositions: [Square.Position] { + return pseudoLegalPositions.filter { isLegal(on: $0) } } - package func move(dst: Square.Position) -> Bool { + internal func move(to dst: Square.Position) -> Bool { return false } - package func isLegal(pos: Square.Position) -> Bool { - return false + internal 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 } + if p.kind == .King { + // TODO: Notify board of check + return false + } + } + } + return true } - package init(color: Color, on position: Square.Position) { + internal init(color: Color, on position: Square.Position) { self.color = color self.position = position } diff --git a/Sources/Engine/Pieces/CommonMoves.swift b/Sources/Engine/Pieces/CommonMoves.swift new file mode 100644 index 0000000..2e96ebe --- /dev/null +++ b/Sources/Engine/Pieces/CommonMoves.swift @@ -0,0 +1,19 @@ +internal protocol DiagonalMoves { + func getDiagonalMoves(from pos: Square.Position) -> [Square.Position] +} + +extension DiagonalMoves { + func getDiagonalMoves(from pos: Square.Position) -> [Square.Position] { + return [] + } +} + +internal protocol LinearMoves { + func getLinearMoves(from pos: Square.Position) -> [Square.Position] +} + +extension LinearMoves { + func getLinearMoves(from pos: Square.Position) -> [Square.Position] { + return [] + } +} \ No newline at end of file diff --git a/Sources/Engine/Pieces/King.swift b/Sources/Engine/Pieces/King.swift index 11ba2b4..9c7d4b7 100644 --- a/Sources/Engine/Pieces/King.swift +++ b/Sources/Engine/Pieces/King.swift @@ -1,31 +1,41 @@ -package class King: Piece { - package weak var board: Board? - package var kind: Kind = .King +internal final class King: Piece { + internal weak var board: Board? + internal var kind: Kind = .King - package var unicodeRepresentation: String { + internal var unicodeRepresentation: String { return color == .Black ? "♛" : "♕" } - package var color: Color + internal var color: Color - package var position: Square.Position + internal var position: Square.Position - package var pseudoLegalPositions: [Square.Position] { + internal var pseudoLegalPositions: [Square.Position] { return [] } - package var legalPositions: [Square.Position] { - return pseudoLegalPositions.filter { isLegal(pos: $0) } + internal var legalPositions: [Square.Position] { + return pseudoLegalPositions.filter { isLegal(on: $0) } } - package func move(dst: Square.Position) -> Bool { + internal func move(to dst: Square.Position) -> Bool { return false } - package func isLegal(pos: Square.Position) -> Bool { - return false + internal 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 } + if p.kind == .King { + // TODO: Notify board of check + return false + } + } + + } + return true } - package init(color: Color, on position: Square.Position) { + internal init(color: Color, on position: Square.Position) { self.color = color self.position = position } diff --git a/Sources/Engine/Pieces/Knight.swift b/Sources/Engine/Pieces/Knight.swift index d9c6566..b015241 100644 --- a/Sources/Engine/Pieces/Knight.swift +++ b/Sources/Engine/Pieces/Knight.swift @@ -1,31 +1,54 @@ -package class Knight: Piece { - package weak var board: Board? - package var kind: Kind = .Knight +internal final class Knight: Piece { + internal weak var board: Board? + internal var kind: Kind = .Knight - package var unicodeRepresentation: String { + internal var unicodeRepresentation: String { return color == .Black ? "♞" : "♘" } - package var color: Color + internal var color: Color - package var position: Square.Position + internal var position: Square.Position - package var pseudoLegalPositions: [Square.Position] { - return [] + internal 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) + ] + return directions.filter { + let index = $0.index + return index < 0 || index > 63 + } } - package var legalPositions: [Square.Position] { - return pseudoLegalPositions.filter { isLegal(pos: $0) } + internal var legalPositions: [Square.Position] { + return pseudoLegalPositions.filter { isLegal(on: $0) } } - package func move(dst: Square.Position) -> Bool { + internal func move(to dst: Square.Position) -> Bool { return false } - package func isLegal(pos: Square.Position) -> Bool { - return false + internal 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 } + if p.kind == .King { + // TODO: Notify board of check + return false + } + } + + } + return true } - package init(color: Color, on position: Square.Position) { + internal init(color: Color, on position: Square.Position) { self.color = color self.position = position } diff --git a/Sources/Engine/Pieces/Pawn.swift b/Sources/Engine/Pieces/Pawn.swift index b1b4b82..cdcc682 100644 --- a/Sources/Engine/Pieces/Pawn.swift +++ b/Sources/Engine/Pieces/Pawn.swift @@ -1,20 +1,32 @@ -package class Pawn: Piece { - package var kind: Kind = .Pawn - package weak var board: Board? - package var color: Color - package var position: Square.Position - package var pseudoLegalPositions: [Square.Position] { - return [] +internal final class Pawn: Piece { + internal var kind: Kind = .Pawn + internal weak var board: Board? + internal var color: Color + internal var position: Square.Position + internal var pseudoLegalPositions: [Square.Position] { + let sign: Int8 = color == .Black ? -1 : 1 + let rank = Int8(position.rank) + let directions: [Square.Position] = [ + .init(file: position.file, rank: rank + 1 * sign), + .init(file: position.file, rank: rank + 2 * sign), + .init(file: position.file + 1, rank: rank + 1 * sign), + .init(file: position.file - 1, rank: rank + 1 * sign) + ] + // TODO: Handle en passant + return directions.filter { + let index = $0.index + return index < 1 || index > 63 + } } - package var legalPositions: [Square.Position] { - return pseudoLegalPositions.filter { isLegal(pos: $0) } + internal var legalPositions: [Square.Position] { + pseudoLegalPositions.filter { isLegal(on: $0) } } - package var unicodeRepresentation: String { - return color == .Black ? "♟" : "♙" + internal var unicodeRepresentation: String { + color == .Black ? "♟" : "♙" } - package func move(dst: Square.Position) -> Bool { + internal func move(to dst: Square.Position) -> Bool { guard board != nil else { return false } @@ -33,18 +45,22 @@ package class Pawn: Piece { return true } - package func isLegal(pos: Square.Position) -> Bool { + internal func isLegal(on pos: Square.Position) -> Bool { // TODO: Handle "En-Passant" if let board = board, let s = board[pos] { if let p = s.piece { if p.color == color { return false } + if p.kind == .King { + // TODO: Notify board of check + return false + } } } return true } - package init( + internal init( color: Color, on position: Square.Position ) { self.color = color diff --git a/Sources/Engine/Pieces/Piece.swift b/Sources/Engine/Pieces/Piece.swift index bf91e4e..5eaef3d 100644 --- a/Sources/Engine/Pieces/Piece.swift +++ b/Sources/Engine/Pieces/Piece.swift @@ -1,7 +1,7 @@ -public enum Kind: String, CaseIterable { +internal enum Kind: String, CaseIterable { case Pawn, Knight, Bishop, Rook, Queen, King - public var value: Int8 { + internal var value: Int8 { switch self { case .Pawn: 1 case .Bishop: 3 @@ -12,7 +12,7 @@ public enum Kind: String, CaseIterable { } } - public static subscript(_ c: Character) -> (Self, Color)? { + internal static subscript(_ c: Character) -> (Self, Color)? { let v = c.uppercased() guard @@ -39,7 +39,7 @@ public enum Kind: String, CaseIterable { } } -public protocol Piece { +internal protocol Piece { var board: Board? { get } var color: Color { get } var unicodeRepresentation: String { get } @@ -47,6 +47,6 @@ public protocol Piece { var position: Square.Position { get } var pseudoLegalPositions: [Square.Position] { get } var legalPositions: [Square.Position] { get } - func move(dst: Square.Position) -> Bool - func isLegal(pos: Square.Position) -> Bool + func move(to dst: Square.Position) -> Bool + func isLegal(on pos: Square.Position) -> Bool } diff --git a/Sources/Engine/Pieces/Queen.swift b/Sources/Engine/Pieces/Queen.swift index 3bcd537..437a7ac 100644 --- a/Sources/Engine/Pieces/Queen.swift +++ b/Sources/Engine/Pieces/Queen.swift @@ -1,31 +1,42 @@ -package class Queen: Piece { - package weak var board: Board? - package var kind: Kind = .Queen +internal final class Queen: Piece, LinearMoves, DiagonalMoves { + internal weak var board: Board? + internal var kind: Kind = .Queen - package var unicodeRepresentation: String { + internal var unicodeRepresentation: String { return color == .Black ? "♛" : "♕" } - package var color: Color + internal var color: Color - package var position: Square.Position + internal var position: Square.Position - package var pseudoLegalPositions: [Square.Position] { - return [] + internal var pseudoLegalPositions: [Square.Position] { + return getDiagonalMoves(from: position) + getLinearMoves(from: position) } - package var legalPositions: [Square.Position] { - return pseudoLegalPositions.filter { isLegal(pos: $0) } + internal var legalPositions: [Square.Position] { + return pseudoLegalPositions.filter { isLegal(on: $0) } } - package func move(dst: Square.Position) -> Bool { + internal func move(to dst: Square.Position) -> Bool { + return false } - package func isLegal(pos: Square.Position) -> Bool { - return false + internal 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 } + if p.kind == .King { + // TODO: Notify board of check + return false + } + } + + } + return true } - package init(color: Color, on position: Square.Position) { + internal init(color: Color, on position: Square.Position) { self.color = color self.position = position } diff --git a/Sources/Engine/Pieces/Rook.swift b/Sources/Engine/Pieces/Rook.swift index 0fa7e57..94e4935 100644 --- a/Sources/Engine/Pieces/Rook.swift +++ b/Sources/Engine/Pieces/Rook.swift @@ -1,31 +1,41 @@ -package class Rook: Piece { - package weak var board: Board? - package var kind: Kind = .Rook +internal final class Rook: Piece, LinearMoves { + internal weak var board: Board? + internal var kind: Kind = .Rook - package var unicodeRepresentation: String { + internal var unicodeRepresentation: String { return color == .Black ? "♜" : "♖" } - package var color: Color + internal var color: Color - package var position: Square.Position + internal var position: Square.Position - package var pseudoLegalPositions: [Square.Position] { - return [] + internal var pseudoLegalPositions: [Square.Position] { + return getLinearMoves(from: position) } - package var legalPositions: [Square.Position] { - return pseudoLegalPositions.filter { isLegal(pos: $0) } + internal var legalPositions: [Square.Position] { + return pseudoLegalPositions.filter { isLegal(on: $0) } } - package func move(dst: Square.Position) -> Bool { + internal func move(to dst: Square.Position) -> Bool { return false } - package func isLegal(pos: Square.Position) -> Bool { - return false + internal 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 } + if p.kind == .King { + // TODO: Notify board of check + return false + } + } + + } + return true } - package init(color: Color, on position: Square.Position) { + internal init(color: Color, on position: Square.Position) { self.color = color self.position = position } diff --git a/Sources/Engine/Square.swift b/Sources/Engine/Square.swift index 2e7761e..6336250 100644 --- a/Sources/Engine/Square.swift +++ b/Sources/Engine/Square.swift @@ -1,12 +1,11 @@ public struct Square: Equatable { public struct Position: Equatable { - public let file: UInt8 - public let rank: UInt8 + public let file: Int8 + public let rank: Int8 public var index: Int { - let r = (8 - rank) % 8 - return Int(8*r+file-1) + return Int(8*(8-rank)+file-1) } public static func == (lhs: Position, rhs: Position) -> Bool { @@ -15,8 +14,8 @@ public struct Square: Equatable { } public let position: Position - public let index: UInt8 - public var piece: Piece? = nil + public let index: Int + var piece: Piece? = nil public let color: Color public static func == (lhs: Square, rhs: Square) -> Bool { diff --git a/Sources/exe/main.swift b/Sources/exe/main.swift index 8950a29..913434a 100644 --- a/Sources/exe/main.swift +++ b/Sources/exe/main.swift @@ -2,5 +2,4 @@ import Engine let board = Board(fen: .init(fen: "rnbqkb1r/pppppppp/2n5/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")) // let board: Board = .init() -print(board) - +print(board) \ No newline at end of file