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,32 +1,26 @@
final class Bishop: Piece, DiagonalMoves {
weak var board: Board?
var kind: Kind = .Bishop
var unicodeRepresentation: String {
override var unicodeRepresentation: String {
return color == .Black ? "" : ""
}
var color: Color
var position: Square.Position
var pseudoLegalPositions: [Square.Position] {
override var pseudoLegalPositions: [Square.Position] {
return getDiagonalMoves(from: position)
}
var legalPositions: [Square.Position] {
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
func move(to dst: Square.Position) -> Bool {
return false
override func move(to dst: Square.Position) {
}
func isLegal(on pos: Square.Position) -> Bool {
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
}
}
@@ -34,9 +28,8 @@ final class Bishop: Piece, DiagonalMoves {
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: .Bishop, on: position, with: color)
}
}

View File

@@ -1,15 +1,10 @@
final class King: Piece {
weak var board: Board?
var kind: Kind = .King
var unicodeRepresentation: String {
override var unicodeRepresentation: String {
return color == .Black ? "" : ""
}
var color: Color
var position: Square.Position
var pseudoLegalPositions: [Square.Position] {
override var pseudoLegalPositions: [Square.Position] {
[
position + (1, 0),
position + (1, 1),
@@ -18,19 +13,18 @@ final class King: Piece {
position + (-1, 0),
position + (-1, -1),
position + (0, -1),
position + (1, -1)
].filter {$0.index != nil}
position + (1, -1),
].filter { $0.index != nil }
}
var legalPositions: [Square.Position] {
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
func move(to dst: Square.Position) -> Bool {
return false
override func move(to dst: Square.Position) {
}
func isLegal(on pos: Square.Position) -> Bool {
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 }
@@ -44,9 +38,8 @@ final class King: 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: .King, on: position, with: color)
}
}

View File

@@ -1,15 +1,9 @@
final class Knight: Piece {
weak var board: Board?
var kind: Kind = .Knight
var unicodeRepresentation: String {
override var unicodeRepresentation: String {
return color == .Black ? "" : ""
}
var color: Color
var position: Square.Position
var pseudoLegalPositions: [Square.Position] {
override var pseudoLegalPositions: [Square.Position] {
[
position + (2, 1),
position + (2, -1),
@@ -18,24 +12,24 @@ final class Knight: Piece {
position + (1, 2),
position + (1, -2),
position + (-1, 2),
position + (-1, -2)
position + (-1, -2),
].filter { $0.index != nil }
}
var legalPositions: [Square.Position] {
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
func move(to dst: Square.Position) -> Bool {
return false
override func move(to dst: Square.Position) {
}
func isLegal(on pos: Square.Position) -> Bool {
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
}
}
@@ -44,9 +38,8 @@ final class Knight: 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: .Knight, on: position, with: color)
}
}

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)
}
}

View File

@@ -1,4 +1,4 @@
enum Kind: String, CaseIterable {
public enum Kind: String, CaseIterable {
case Pawn, Knight, Bishop, Rook, Queen, King
var value: Int8 {
@@ -39,14 +39,28 @@ enum Kind: String, CaseIterable {
}
}
protocol Piece {
var board: Board? { get }
var color: Color { get }
var unicodeRepresentation: String { get }
var kind: Kind { get }
var position: Square.Position { get }
var pseudoLegalPositions: [Square.Position] { get }
var legalPositions: [Square.Position] { get }
func move(to dst: Square.Position) -> Bool
func isLegal(on pos: Square.Position) -> Bool
public class Piece {
#warning("TODO: TO be removed, handle everything through the delegate")
internal weak var board: Board?
public internal(set) var color: Color
public var unicodeRepresentation: String {
return ""
}
public internal(set) var kind: Kind
public internal(set) var position: Square.Position
internal var pseudoLegalPositions: [Square.Position] {
return []
}
internal var legalPositions: [Square.Position] {
return []
}
internal var delegate: EventDelegate?
internal func move(to dst: Square.Position) {}
internal func isLegal(on pos: Square.Position) -> Bool { false }
internal init(kind: Kind, on pos: Square.Position, with col: Color) {
self.kind = kind
self.position = pos
self.color = col
}
}

View File

@@ -1,33 +1,27 @@
final class Queen: Piece, LinearMoves, DiagonalMoves {
weak var board: Board?
var kind: Kind = .Queen
var unicodeRepresentation: String {
override var unicodeRepresentation: String {
return color == .Black ? "" : ""
}
var color: Color
var position: Square.Position
var pseudoLegalPositions: [Square.Position] {
override var pseudoLegalPositions: [Square.Position] {
return getDiagonalMoves(from: position) + getLinearMoves(from: position)
}
var legalPositions: [Square.Position] {
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
func move(to dst: Square.Position) -> Bool {
return false
override func move(to dst: Square.Position) {
}
func isLegal(on pos: Square.Position) -> Bool {
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
}
}
@@ -36,9 +30,8 @@ final class Queen: Piece, LinearMoves, DiagonalMoves {
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: .Queen, on: position, with: color)
}
}

View File

@@ -1,32 +1,27 @@
final class Rook: Piece, LinearMoves {
weak var board: Board?
var kind: Kind = .Rook
var unicodeRepresentation: String {
override var unicodeRepresentation: String {
return color == .Black ? "" : ""
}
var color: Color
var position: Square.Position
var pseudoLegalPositions: [Square.Position] {
override var pseudoLegalPositions: [Square.Position] {
return getLinearMoves(from: position)
}
var legalPositions: [Square.Position] {
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
func move(to dst: Square.Position) -> Bool {
return false
override func move(to dst: Square.Position) {
}
func isLegal(on pos: Square.Position) -> Bool {
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
}
}
@@ -35,8 +30,10 @@ final class Rook: Piece, LinearMoves {
return true
}
init(color: Color, on position: Square.Position) {
init(with color: Color, on position: Square.Position) {
super.init(kind: .Rook, on: position, with: color)
self.color = color
self.kind = .Rook
self.position = position
}