aboutsummaryrefslogtreecommitdiff
path: root/Sources/SwiftChessNeo/Rank.swift
blob: 9998e4192044f1a1086ca80fd13b6e1df1d99002 (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
//
//  Rank.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 rank.
///
/// `Rank`s refer to the eight rows of a chess board, beginning with 1 at the bottom and ending with 8 at the top.
public enum Rank: Int, Comparable, CustomStringConvertible {

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

        /// Up direction.
        case up

        /// Down direction.
        case down

    }

    /// Rank 1.
    case one = 1

    /// Rank 2.
    case two = 2

    /// Rank 3.
    case three = 3

    /// Rank 4.
    case four = 4

    /// Rank 5.
    case five = 5

    /// Rank 6.
    case six = 6

    /// Rank 7.
    case seven = 7

    /// Rank 8.
    case eight = 8

}

extension Rank {

    /// An array of all ranks.
    public static let all: [Rank] = [1, 2, 3, 4, 5, 6, 7, 8]

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

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

    /// Create an instance from an integer value.
    public init?(_ value: Int) {
        self.init(rawValue: value)
    }

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

    /// Creates the starting `Rank` for the color.
    public init(startFor color: Color) {
        self = color.isWhite ? 1 : 8
    }

    /// Creates the ending `Rank` for the color.
    public init(endFor color: Color) {
        self = color.isWhite ? 8 : 1
    }

    /// Returns a rank from advancing `self` by `value` with respect to `color`.
    public func advanced(by value: Int, for color: Color = .white) -> Rank? {
        return Rank(rawValue: rawValue + (color.isWhite ? value : -value))
    }

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

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

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

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

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

}

extension Rank: ExpressibleByIntegerLiteral { }

extension Rank {

    /// Create an instance initialized to `value`.
    public init(integerLiteral value: Int) {
        guard let rank = Rank(rawValue: value) else {
            fatalError("Rank value not within 1 and 8, inclusive")
        }
        self = rank
    }

}

/// Returns `true` if one rank is higher than the other.
public func < (lhs: Rank, rhs: Rank) -> Bool {
    return lhs.rawValue < rhs.rawValue
}