1

I am creating a chess application on swift. I'm trying to create a deep copy of my pieces so as to check if the new state would prevent an incoming check from the opposition. The deep copy would consist of the proposed move and it will utilize a function isKingUnderCheck() to validate if the move helps avoid the check. So far I've looked through multiple websites and I've found many that talk about deep copying array, however, I'm still unsure about how to deep copy a Set of objects.

I've stored my pieces as a set let pieces: Set<ChessPiece>.

I've set up NSCopying for my ChessPiece class as shown below.

Lastly, I tried to create a function that takes in Set<ChessPiece> to return a deep copy. However, I am unable to do so and it displays an error Cannot convert return expression of type '[Any]' to return type 'Set<ChessPiece>'.

ChessPiece Class

class ChessPiece: NSObject, NSCopying {
func copy(with zone: NSZone? = nil) -> Any {
    return type(of: self).init(self)
}

required init(_ piece: ChessPiece) {
    col = piece.col
    row = piece.row
    isWhite = piece.isWhite
  }

static func == (lhs: ChessPiece, rhs: ChessPiece) -> Bool {
    lhs.col == rhs.col && lhs.row == rhs.row && lhs.isWhite == rhs.isWhite
}

var col: Int
var row: Int
let isWhite: Bool

init(col: Int, row: Int, isWhite: Bool) {
    self.col = col
    self.row = row
    self.isWhite = isWhite
}

}

deepCopyPieces function

private func deepCopyPieces(pieces: Set<ChessPiece>) -> Set<ChessPiece> {
    let tempPieces = pieces.map{$0.copy()}
    return tempPieces
}

It is worth noting that Set<ChessPiece> includes extensions of ChessPiece such as class KnightPiece: ChessPiece.

I'm not so sure if I am doing this correctly as this is the first time I'm doing a project of this scale, thank you in advance for all your responses. Thank you for your time.

Justin Lim
  • 13
  • 4
  • 1
    Unfortunately, using `NSCopying` you have to cast the result to your type. There're plenty of code examples around, for instance: https://stackoverflow.com/questions/32111868/how-to-conform-to-nscopying-and-implement-copywithzone-in-swift-2 – lazarevzubov Dec 08 '21 at 06:56
  • 1
    So why is ChessPiece a class and not a struct? – Joakim Danielson Dec 08 '21 at 07:49
  • 1
    I agree. Make your pieces structs and then the immutability of structs and sets means you don't need to worry about copying – Paulw11 Dec 08 '21 at 09:31
  • 1
    @Paulw11 : I think he use sclasses to be able to subclass for KingPiece, QueenPiece,… . You can not subclass with struct. To use struct , he may define enum and ChessPieceProtocole. – Ptit Xav Dec 08 '21 at 09:54
  • @Paulw11, Joakim, thank you for your suggestion, as PtitXav mentions, my intention is to subclass the different pieces from ChessPiece Class. Unfortunately, I cannot subclass with struct. I'm not sure if that is the best way of accomplishing it, but it is able to categorize the classes very nicely. – Justin Lim Dec 08 '21 at 15:52
  • I think you should separate the piece from the rules about how pieces move. Perhaps create a `MoveRules` class that can be applied to a piece and that class can have subclasses. Personally, I am not sure that I would have the position in the piece either. A piece is just a type (enum) and color (enum). The pieces should be on a `Board` – Paulw11 Dec 08 '21 at 18:58

0 Answers0