aboutsummaryrefslogtreecommitdiff
path: root/Sources/SwiftChessNeo/File.swift
blob: 158eee7c282493bea54f04c0b2fdaa9f68ab382a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
//  File.swift
//  Sage
//
//  Copyright 2016-2017 Nikolai Vazquez
//  Modified by SuperGeroy
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

/// A chess board file.
///
/// Files refer to the eight columns of a chess board, beginning with A and ending with H from left to right.
public enum File: Int, Comparable, CustomStringConvertible {

    /// A direction in file.
    public enum Direction {

        /// Left direction.
        case left

        /// Right direction.
        case right

    }

    /// File "A".
    case a = 1

    /// File "B".
    case b = 2

    /// File "C".
    case c = 3

    /// File "D".
    case d = 4

    /// File "E".
    case e = 5

    /// File "F".
    case f = 6

    /// File "G".
    case g = 7

    /// File "H".
    case h = 8

    /// A regardless of Swift version.
    internal static let _a = File.a

    /// B regardless of Swift version.
    internal static let _b = File.b

    /// C regardless of Swift version.
    internal static let _c = File.c

    /// D regardless of Swift version.
    internal static let _d = File.d

    /// E regardless of Swift version.
    internal static let _e = File.e

    /// F regardless of Swift version.
    internal static let _f = File.f

    /// G regardless of Swift version.
    internal static let _g = File.g

    /// H regardless of Swift version.
    internal static let _h = File.h

}

extension File {

    /// An array of all files.
    public static let all: [File] = [.a, .b, .c, .d, .e, .f, .g, .h]

    /// The column index of `self`.
    public var index: Int {
        return rawValue - 1
    }

    /// A textual representation of `self`.
    public var description: String {
        return String(character)
    }

    /// The character value of `self`.
    public var character: Character {
        switch self {
            case .a: return "a"
            case .b: return "b"
            case .c: return "c"
            case .d: return "d"
            case .e: return "e"
            case .f: return "f"
            case .g: return "g"
            case .h: return "h"
        }
    }

    /// Create an instance from a character value.
    public init?(_ character: Character) {
        switch character {
            case "A", "a": self = .a
            case "B", "b": self = .b
            case "C", "c": self = .c
            case "D", "d": self = .d
            case "E", "e": self = .e
            case "F", "f": self = .f
            case "G", "g": self = .g
            case "H", "h": self = .h
            default: return nil
        }
    }

    /// Create a `File` from a zero-based column index.
    public init?(index: Int) {
        self.init(rawValue: index + 1)
    }

    /// Returns a rank from advancing `self` by `value`.
    public func advanced(by value: Int) -> File? {
        return File(rawValue: rawValue + value)
    }

    /// The next file after `self`.
    public func next() -> File? {
        return File(rawValue: (rawValue + 1))
    }

    /// The previous file to `self`.
    public func previous() -> File? {
        return File(rawValue: (rawValue - 1))
    }

    /// The opposite file of `self`.
    public func opposite() -> File {
        return File(rawValue: 9 - rawValue)!
    }

    /// The files from `self` to `other`.
    public func to(_ other: File) -> [File] {
        return _to(other)
    }

    /// The files between `self` and `other`.
    public func between(_ other: File) -> [File] {
        return _between(other)
    }

}

extension File: ExpressibleByExtendedGraphemeClusterLiteral { }

extension File {

    /// Create an instance initialized to `value`.
    public init(unicodeScalarLiteral value: Character) {
        guard let file = File(value) else {
            fatalError("File value not within \"A\" and \"H\" or \"a\" and \"h\", inclusive")
        }
        self = file
    }

    /// Create an instance initialized to `value`.
    public init(extendedGraphemeClusterLiteral value: Character) {
        self.init(unicodeScalarLiteral: value)
    }

}

/// Returns `true` if one file is further left than the other.
public func < (lhs: File, rhs: File) -> Bool {
    return lhs.rawValue < rhs.rawValue
}