Can now move pieces

This commit is contained in:
cdricms
2024-06-28 19:35:43 +02:00
parent 501484558a
commit 4f5b51f4ae
11 changed files with 125 additions and 109 deletions

View File

@@ -12,20 +12,8 @@ final class Bishop: Piece, DiagonalMoves {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
override func move(to dst: Square.Position) {
}
override 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 {
delegate?.notify(.kingInCheck(self))
return false
}
}
}
return true
super.isLegal(on: pos)
}
init(with color: Color, on position: Square.Position) {

View File

@@ -21,21 +21,8 @@ final class King: Piece {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
override func move(to dst: Square.Position) {
}
override 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
super.isLegal(on: pos)
}
init(with color: Color, on position: Square.Position) {

View File

@@ -20,22 +20,8 @@ final class Knight: Piece {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
override func move(to dst: Square.Position) {
}
override 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
delegate?.notify(.kingInCheck(self))
return false
}
}
}
return true
super.isLegal(on: pos)
}
init(with color: Color, on position: Square.Position) {

View File

@@ -17,37 +17,8 @@ final class Pawn: Piece {
color == .Black ? "" : ""
}
override func move(to dst: Square.Position) {
if !(legalPositions.contains { $0 == dst }) {
return
}
delegate?.movePiece(self, to: dst)
// if let board = board, var s = board[position], var d = board[dst] {
// s.piece = self
// d.piece = nil
// }
// position = dst
// return true
}
override 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
delegate?.notify(.kingInCheck(self))
return false
}
}
}
return true
super.isLegal(on: pos)
}
init(with color: Color, on position: Square.Position) {

View File

@@ -69,8 +69,28 @@ public class Piece {
return []
}
internal var delegate: EventDelegate?
internal func move(to dst: Square.Position) {}
internal func isLegal(on pos: Square.Position) -> Bool { false }
internal func move(to dst: Square.Position) throws {
if !(legalPositions.contains { $0 == dst }) {
throw Board.MoveFailure.destinationIsIllegal(pos: dst)
}
try delegate?.movePiece(self, to: dst)
}
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
delegate?.notify(.kingInCheck(self))
return false
}
}
}
return true
}
internal init(kind: Kind, on pos: Square.Position, with col: Color) {
self.kind = kind

View File

@@ -11,23 +11,8 @@ final class Queen: Piece, LinearMoves, DiagonalMoves {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
override func move(to dst: Square.Position) {
}
override 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
delegate?.notify(.kingInCheck(self))
return false
}
}
}
return true
super.isLegal(on: pos)
}
init(with color: Color, on position: Square.Position) {

View File

@@ -12,22 +12,8 @@ final class Rook: Piece, LinearMoves {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
override func move(to dst: Square.Position) {
}
override 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
delegate?.notify(.kingInCheck(self))
return false
}
}
}
return true
super.isLegal(on: pos)
}
init(with color: Color, on position: Square.Position) {