Lots of changes

This commit is contained in:
cdricms
2024-06-28 00:02:48 +02:00
parent 8556380dba
commit 1d7f1095d5
11 changed files with 175 additions and 139 deletions

View File

@@ -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]]
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 board: Grid {
var board = Grid()
@@ -37,7 +57,8 @@ public class Board: CustomStringConvertible {
let r = 8 - Int(rank)
if c == "/" {
if file != 9 {
throw Fen.FenError.NotAppropriateLength(n: file, column: index)
throw Fen.FenError.NotAppropriateLength(
n: file, column: index)
}
rank -= 1
file = 0
@@ -51,7 +72,7 @@ public class Board: CustomStringConvertible {
let f = file
for i in f..<(f + n) {
file += 1
b[8*r+Int(i)].piece = nil
b[8 * r + Int(i)].piece = nil
}
file -= 1
} else if c.isASCII {
@@ -60,19 +81,20 @@ public class Board: CustomStringConvertible {
let piece: Piece =
switch k {
case .Pawn:
Pawn(color: c, on: .init(rank: rank, file: file))
Pawn(with: c, on: .init(rank: rank, file: file))
case .Knight:
Knight(color: c, on: .init(rank: rank, file: file))
Knight(with: c, on: .init(rank: rank, file: file))
case .Bishop:
Bishop(color: c, on: .init(rank: rank, file: file))
Bishop(with: c, on: .init(rank: rank, file: file))
case .Rook:
Rook(color: c, on: .init(rank: rank, file: file))
Rook(with: c, on: .init(rank: rank, file: file))
case .Queen:
Queen(color: c, on: .init(rank: rank, file: file))
Queen(with: c, on: .init(rank: rank, file: file))
case .King:
King(color: c, on: .init(rank: rank, file: file))
King(with: c, on: .init(rank: rank, file: file))
}
b[8*r+Int(file)-1].piece = piece
piece.delegate = self
b[8 * r + Int(file) - 1].piece = piece
case .none:
throw Fen.FenError.InvalidCharacter(
c: String(c), column: index)
@@ -82,14 +104,16 @@ public class Board: CustomStringConvertible {
file += 1
index += 1
}
#if DEBUG
print(b)
#endif
#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")
public required init(
fen: Fen =
.init(
fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
) {
var rank: Int8 = 8
self.fen = fen
@@ -134,7 +158,7 @@ public class Board: CustomStringConvertible {
return boardString
}
public subscript(pos: Square.Position) -> Square? {
internal subscript(pos: Square.Position) -> Square? {
guard let i = pos.index else {
return nil
}
@@ -143,4 +167,4 @@ public class Board: CustomStringConvertible {
}
return squares[i]
}
}
}