|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// A structure to handle Chess movement and relative positions. |
| 4 | +public struct Chess: Hashable, Equatable, Codable { |
| 5 | + |
| 6 | + /// An enumeration of standard chess colors. |
| 7 | + public enum Color: String, Hashable, Equatable, Codable { |
| 8 | + case black |
| 9 | + case white |
| 10 | + } |
| 11 | + |
| 12 | + /// An enumeration of standard piece types, plus special types `.empty`, and `.invalid`. |
| 13 | + public enum Piece: String, Hashable, Equatable, Codable { |
| 14 | + case bishop |
| 15 | + case pawn |
| 16 | + case queen |
| 17 | + case king |
| 18 | + case knight |
| 19 | + case rook |
| 20 | + |
| 21 | + /// This represents an empty space on a chessboard. |
| 22 | + case empty |
| 23 | + |
| 24 | + /// This represents an invalid `Piece` type, in case that is needed for some reason. |
| 25 | + case invalid |
| 26 | + |
| 27 | + /// An array of all the valid piece types. |
| 28 | + public static var allValidTypes: [Piece] { |
| 29 | + return [ |
| 30 | + .bishop, |
| 31 | + .pawn, |
| 32 | + .queen, |
| 33 | + .king, |
| 34 | + .knight, |
| 35 | + .rook, |
| 36 | + ] |
| 37 | + } |
| 38 | + |
| 39 | + /// An array of possible capture directions as `Coordinate` offsets. |
| 40 | + /// |
| 41 | + /// Notes: |
| 42 | + /// - For `.pawn`, positive `y` movement is assumed. (Meaning this doesn't consider color.) |
| 43 | + /// - For pieces that can capture more than one space away in a given direction, a single `Coordinate` |
| 44 | + /// value for each direction is returned. See also `possibleMovementRecurses(forPiece:)`. |
| 45 | + /// - See also `possibleMovementCoordinates(forPiece:)`. |
| 46 | + public static func possibleCaptureCoordinates( |
| 47 | + forPiece piece: Piece |
| 48 | + ) -> [Coordinate] { |
| 49 | + switch piece { |
| 50 | + case .bishop: |
| 51 | + return Direction.diagonalOffsets |
| 52 | + case .pawn: |
| 53 | + return [ |
| 54 | + Coordinate(inDirection: .upLeft), |
| 55 | + Coordinate(inDirection: .upRight), |
| 56 | + ] |
| 57 | + case .queen: |
| 58 | + return Direction.allOffsets |
| 59 | + case .king: |
| 60 | + return Direction.allOffsets |
| 61 | + case .knight: |
| 62 | + return [ |
| 63 | + Coordinate(x: -1, y: 2), |
| 64 | + Coordinate(x: -2, y: 1), |
| 65 | + Coordinate(x: -2, y: -1), |
| 66 | + Coordinate(x: -1, y: -2), |
| 67 | + Coordinate(x: 1, y: 2), |
| 68 | + Coordinate(x: 2, y: 1), |
| 69 | + Coordinate(x: 2, y: -1), |
| 70 | + Coordinate(x: 1, y: -2), |
| 71 | + ] |
| 72 | + case .rook: |
| 73 | + return Direction.orthogonalOffsets |
| 74 | + case .empty, .invalid: |
| 75 | + return [] |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// An array of possible movement directions as `Coordinate` offsets. |
| 80 | + /// |
| 81 | + /// Notes: |
| 82 | + /// - For `.pawn`, positive y movement is assumed. (Meaning this doesn't consider color.) |
| 83 | + /// - For pieces that can move more than one space in a given direction, a single `Coordinate` |
| 84 | + /// value for each direction is returned. See also `possibleMovementRecurses(forPiece:)`. |
| 85 | + /// - See also `possibleCaptureCoordinates(forPiece:)`. |
| 86 | + public static func possibleMovementCoordinates( |
| 87 | + forPiece piece: Piece |
| 88 | + ) -> [Coordinate] { |
| 89 | + switch piece { |
| 90 | + case .bishop: |
| 91 | + return Direction.diagonalOffsets |
| 92 | + case .pawn: |
| 93 | + return [ |
| 94 | + Coordinate(x: 0, y: 1), |
| 95 | + ] |
| 96 | + case .queen: |
| 97 | + return Direction.allOffsets |
| 98 | + case .king: |
| 99 | + return Direction.allOffsets |
| 100 | + case .knight: |
| 101 | + return [ |
| 102 | + Coordinate(x: -1, y: 2), |
| 103 | + Coordinate(x: -2, y: 1), |
| 104 | + Coordinate(x: -2, y: -1), |
| 105 | + Coordinate(x: -1, y: -2), |
| 106 | + Coordinate(x: 1, y: 2), |
| 107 | + Coordinate(x: 2, y: 1), |
| 108 | + Coordinate(x: 2, y: -1), |
| 109 | + Coordinate(x: 1, y: -2), |
| 110 | + ] |
| 111 | + case .rook: |
| 112 | + return Direction.orthogonalOffsets |
| 113 | + case .empty, .invalid: |
| 114 | + return [] |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /// A boolean value indicating whether a given `Piece` type can move (or capture) more than |
| 119 | + /// one space in the `Coordinate` direction indicated by `possibleMovementCoordinates` or |
| 120 | + /// `possibleCaptureCoordinates` respectively. |
| 121 | + public static func possibleMovementRecurses( |
| 122 | + forPiece piece: Piece |
| 123 | + ) -> Bool { |
| 124 | + switch piece { |
| 125 | + case .bishop, .queen, .rook: |
| 126 | + return true |
| 127 | + case .pawn, .king, .knight: |
| 128 | + return false |
| 129 | + case .empty, .invalid: |
| 130 | + return false |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /// A randomly selected valid piece type. |
| 135 | + public static func random() -> Piece { |
| 136 | + let range = 0..<allValidTypes.count |
| 137 | + let index = Int.random(in: range) |
| 138 | + return Chess.Piece.allValidTypes[index] |
| 139 | + } |
| 140 | + |
| 141 | + /// An array of the whole set, in the proper distributions. |
| 142 | + public static var theWholeSet: [Piece] { |
| 143 | + return [ |
| 144 | + .king, .queen, .rook, .rook, .bishop, .bishop, .knight, .knight, |
| 145 | + .pawn, .pawn, .pawn, .pawn, .pawn, .pawn, .pawn, .pawn, |
| 146 | + ] |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments