Sliding pieces moves are now illegal if after piece of same color

This commit is contained in:
cdricms
2024-06-30 21:06:03 +02:00
parent 02de114d74
commit d3503f1441
8 changed files with 225 additions and 54 deletions

View File

@@ -16,6 +16,25 @@ public struct Square: Equatable {
file = f
}
public init?(with rep: String) throws {
guard rep.count < 3 else {
throw Board.MoveFailure.squareANIsTooLong(an: rep)
}
guard rep.count > 1 else {
throw Board.MoveFailure.squareANIsTooShort(an: rep)
}
guard let f1 = rep.first, let r1 = rep.last, r1.isWholeNumber else {
return nil
}
guard
let srcFile = File.init(rawValue: String(f1))?.value
else {
return nil
}
rank = Int8(String(r1))!
file = srcFile
}
public enum File: String, CustomStringConvertible {
case a, b, c, d, e, f, g, h
@@ -70,6 +89,22 @@ public struct Square: Equatable {
public static func += (lhs: inout Position, rhs: (Int8, Int8)) {
lhs = lhs + rhs
}
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