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,51 +1,47 @@
final class Pawn: Piece {
var kind: Kind = .Pawn
weak var board: Board?
var color: Color
var position: Square.Position
var pseudoLegalPositions: [Square.Position] {
override var pseudoLegalPositions: [Square.Position] {
let sign: Int8 = color == .Black ? -1 : 1
return [
position + (1 * sign, 0),
position + (2 * sign, 0),
position + (1 * sign, 1),
position + (1 * sign, -1)
position + (1 * sign, -1),
].filter { $0.index != nil }
}
var legalPositions: [Square.Position] {
override var legalPositions: [Square.Position] {
pseudoLegalPositions.filter { isLegal(on: $0) }
}
var unicodeRepresentation: String {
override var unicodeRepresentation: String {
color == .Black ? "" : ""
}
func move(to dst: Square.Position) -> Bool {
guard board != nil else {
return false
}
override func move(to dst: Square.Position) {
if !(legalPositions.contains { $0 == dst }) {
return false
return
}
if let board = board, var s = board[position], var d = board[dst] {
s.piece = self
d.piece = nil
}
delegate?.movePiece(self, to: dst)
position = dst
// if let board = board, var s = board[position], var d = board[dst] {
// s.piece = self
// d.piece = nil
// }
return true
// position = dst
// return true
}
func isLegal(on pos: Square.Position) -> Bool {
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
}
}
@@ -54,10 +50,7 @@ final class Pawn: Piece {
return true
}
init(
color: Color, on position: Square.Position
) {
self.color = color
self.position = position
init(with color: Color, on position: Square.Position) {
super.init(kind: .Pawn, on: position, with: color)
}
}