44 lines
1.0 KiB
Swift
44 lines
1.0 KiB
Swift
public struct Square: Equatable {
|
|
|
|
public struct Position: Equatable {
|
|
public let rank: Int8
|
|
public let file: Int8
|
|
|
|
public var index: Int? {
|
|
guard (rank > 0 && rank < 9) && (file > 0 && file < 9) else {
|
|
return nil
|
|
}
|
|
return Int(8*(8-rank)+file-1)
|
|
}
|
|
|
|
public static func == (lhs: Position, rhs: Position) -> Bool {
|
|
return lhs.index == rhs.index
|
|
}
|
|
|
|
public static func + (lhs: Position, rhs: Position) -> Position {
|
|
.init(rank: lhs.rank + rhs.rank, file: lhs.file + rhs.file)
|
|
}
|
|
|
|
public static func + (lhs: Position, rhs: (Int8, Int8)) -> Position {
|
|
.init(rank: lhs.rank + rhs.0, file: lhs.file + rhs.1)
|
|
}
|
|
|
|
public static func += (lhs: inout Position, rhs: Position) {
|
|
lhs = lhs + rhs
|
|
}
|
|
|
|
public static func += (lhs: inout Position, rhs: (Int8, Int8)) {
|
|
lhs = lhs + rhs
|
|
}
|
|
}
|
|
|
|
public let position: Position
|
|
public let index: Int
|
|
var piece: Piece? = nil
|
|
public let color: Color
|
|
|
|
public static func == (lhs: Square, rhs: Square) -> Bool {
|
|
return lhs.position == rhs.position
|
|
}
|
|
}
|