Can now move pieces
This commit is contained in:
@@ -4,7 +4,7 @@ enum Event {
|
||||
}
|
||||
protocol EventDelegate {
|
||||
func notify(_ event: Event)
|
||||
func movePiece(_ piece: Piece, to dst: Square.Position)
|
||||
func movePiece(_ piece: Piece, to dst: Square.Position) throws
|
||||
}
|
||||
public class Board: CustomStringConvertible, EventDelegate {
|
||||
public typealias Grid = [[Square]]
|
||||
@@ -24,8 +24,87 @@ public class Board: CustomStringConvertible, EventDelegate {
|
||||
|
||||
}
|
||||
|
||||
func movePiece(_ piece: Piece, to dst: Square.Position) {
|
||||
func movePiece(_ piece: Piece, to dst: Square.Position) throws {
|
||||
let from = piece.position
|
||||
piece.position = dst
|
||||
squares[dst.index!].piece = piece
|
||||
squares[from.index!].piece = nil
|
||||
fen.set(from: board, castling: .All, enPassant: fen.enPassant)
|
||||
}
|
||||
|
||||
public enum MoveFailure: Error, CustomStringConvertible {
|
||||
case sourceSquareUnreachable(pos: Square.Position)
|
||||
case destinationSquareUnreachable(pos: Square.Position)
|
||||
case squareHasNoPiece(pos: Square.Position)
|
||||
case destinationIsIllegal(pos: Square.Position)
|
||||
case squareANIsTooLong(an: String)
|
||||
case squareANIsTooShort(an: String)
|
||||
|
||||
public var description: String {
|
||||
return switch self {
|
||||
case .sourceSquareUnreachable(let pos):
|
||||
"The source square given \(pos.rank) \(pos.file) is unreachable."
|
||||
case .destinationSquareUnreachable(let pos):
|
||||
"The destination square given \(pos.rank) \(pos.file) is unreachable."
|
||||
case .squareHasNoPiece(let pos):
|
||||
"The square \(pos.rank) \(pos.file) doesn't have any piece to be moved."
|
||||
case .destinationIsIllegal(let pos):
|
||||
"The destination square given \(pos.rank) \(pos.file) is illegal to be moved to."
|
||||
case .squareANIsTooLong(let an):
|
||||
"The algebraic notation \(an) is too long. (Needs to be 2 characters)"
|
||||
case .squareANIsTooShort(let an):
|
||||
"The algebraic notation \(an) is too short. (Needs to be 2 characters)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func move(src: String, dst: String) throws {
|
||||
guard src.count < 3 else {
|
||||
throw MoveFailure.squareANIsTooLong(an: src)
|
||||
}
|
||||
guard src.count > 1 else {
|
||||
throw MoveFailure.squareANIsTooShort(an: src)
|
||||
}
|
||||
guard dst.count < 3 else {
|
||||
throw MoveFailure.squareANIsTooLong(an: dst)
|
||||
}
|
||||
guard dst.count > 1 else {
|
||||
throw MoveFailure.squareANIsTooShort(an: dst)
|
||||
}
|
||||
guard let f1 = src.first, let r1 = src.last, r1.isWholeNumber else {
|
||||
return
|
||||
}
|
||||
guard let f2 = dst.first, let r2 = src.last, r2.isWholeNumber else {
|
||||
return
|
||||
}
|
||||
guard
|
||||
let srcFile = Square.Position.File.init(rawValue: String(f1))?.value
|
||||
else {
|
||||
return
|
||||
}
|
||||
guard
|
||||
let dstFile = Square.Position.File.init(rawValue: String(f2))?.value
|
||||
else {
|
||||
return
|
||||
}
|
||||
|
||||
try move(
|
||||
src: Square.Position(rank: Int8(String(r1))!, file: srcFile),
|
||||
dst: Square.Position(rank: Int8(String(r2))!, file: dstFile))
|
||||
}
|
||||
|
||||
public func move(src: Square.Position, dst: Square.Position) throws {
|
||||
guard let srcSquare = self[src] else {
|
||||
throw MoveFailure.sourceSquareUnreachable(pos: src)
|
||||
}
|
||||
guard self[dst] != nil else {
|
||||
throw MoveFailure.destinationSquareUnreachable(pos: src)
|
||||
}
|
||||
guard let piece = srcSquare.piece else {
|
||||
throw MoveFailure.squareHasNoPiece(pos: src)
|
||||
}
|
||||
|
||||
try piece.move(to: dst)
|
||||
}
|
||||
|
||||
public func getSquareInfo(on pos: Square.Position) -> Square? {
|
||||
|
||||
Reference in New Issue
Block a user