/
Square.swift
595 lines (433 loc) · 10.1 KB
/
Square.swift
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//
// Square.swift
// Sage
//
// Copyright 2016 Nikolai Vazquez
//
// 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 pair of a chess board `File` and `Rank`.
public typealias Location = (file: File, rank: Rank)
/// A chess board square.
///
/// A `Square` can be one of sixty-four possible values, ranging from `A1` to `H8`.
public enum Square: Int, CustomStringConvertible {
#if swift(>=3)
/// A1 square.
case a1
/// B1 square.
case b1
/// C1 square.
case c1
/// D1 square.
case d1
/// E1 square.
case e1
/// F1 square.
case f1
/// G1 square.
case g1
/// H1 square.
case h1
/// A2 square.
case a2
/// B2 square.
case b2
/// C2 square.
case c2
/// D2 square.
case d2
/// E2 square.
case e2
/// F2 square.
case f2
/// G2 square.
case g2
/// H2 square.
case h2
/// A3 square.
case a3
/// B3 square.
case b3
/// C3 square.
case c3
/// D3 square.
case d3
/// E3 square.
case e3
/// F3 square.
case f3
/// G3 square.
case g3
/// H3 square.
case h3
/// A4 square.
case a4
/// B4 square.
case b4
/// C4 square.
case c4
/// D4 square.
case d4
/// E4 square.
case e4
/// F4 square.
case f4
/// G4 square.
case g4
/// H4 square.
case h4
/// A5 square.
case a5
/// B5 square.
case b5
/// C5 square.
case c5
/// D5 square.
case d5
/// E5 square.
case e5
/// F5 square.
case f5
/// G5 square.
case g5
/// H5 square.
case h5
/// A6 square.
case a6
/// B6 square.
case b6
/// C6 square.
case c6
/// D6 square.
case d6
/// E6 square.
case e6
/// F6 square.
case f6
/// G6 square.
case g6
/// H6 square.
case h6
/// A7 square.
case a7
/// B7 square.
case b7
/// C7 square.
case c7
/// D7 square.
case d7
/// E7 square.
case e7
/// F7 square.
case f7
/// G7 square.
case g7
/// H7 square.
case h7
/// A8 square.
case a8
/// B8 square.
case b8
/// C8 square.
case c8
/// D8 square.
case d8
/// E8 square.
case e8
/// F8 square.
case f8
/// G8 square.
case g8
/// H8 square.
case h8
#else
/// A1 square.
case A1
/// B1 square.
case B1
/// C1 square.
case C1
/// D1 square.
case D1
/// E1 square.
case E1
/// F1 square.
case F1
/// G1 square.
case G1
/// H1 square.
case H1
/// A2 square.
case A2
/// B2 square.
case B2
/// C2 square.
case C2
/// D2 square.
case D2
/// E2 square.
case E2
/// F2 square.
case F2
/// G2 square.
case G2
/// H2 square.
case H2
/// A3 square.
case A3
/// B3 square.
case B3
/// C3 square.
case C3
/// D3 square.
case D3
/// E3 square.
case E3
/// F3 square.
case F3
/// G3 square.
case G3
/// H3 square.
case H3
/// A4 square.
case A4
/// B4 square.
case B4
/// C4 square.
case C4
/// D4 square.
case D4
/// E4 square.
case E4
/// F4 square.
case F4
/// G4 square.
case G4
/// H4 square.
case H4
/// A5 square.
case A5
/// B5 square.
case B5
/// C5 square.
case C5
/// D5 square.
case D5
/// E5 square.
case E5
/// F5 square.
case F5
/// G5 square.
case G5
/// H5 square.
case H5
/// A6 square.
case A6
/// B6 square.
case B6
/// C6 square.
case C6
/// D6 square.
case D6
/// E6 square.
case E6
/// F6 square.
case F6
/// G6 square.
case G6
/// H6 square.
case H6
/// A7 square.
case A7
/// B7 square.
case B7
/// C7 square.
case C7
/// D7 square.
case D7
/// E7 square.
case E7
/// F7 square.
case F7
/// G7 square.
case G7
/// H7 square.
case H7
/// A8 square.
case A8
/// B8 square.
case B8
/// C8 square.
case C8
/// D8 square.
case D8
/// E8 square.
case E8
/// F8 square.
case F8
/// G8 square.
case G8
/// H8 square.
case H8
#endif
}
extension Square {
/// An array of all squares.
public static let all: [Square] = (0 ..< 64).flatMap(Square.init(rawValue:))
/// The file of `self`.
public var file: File {
get {
return File(index: rawValue & 7)!
}
set(newFile) {
self = Square(file: newFile, rank: rank)
}
}
/// The rank of `self`.
public var rank: Rank {
get {
return Rank(index: rawValue >> 3)!
}
set(newRank) {
self = Square(file: file, rank: newRank)
}
}
/// The location of `self`.
public var location: Location {
get {
return (file, rank)
}
set(newLocation) {
self = Square(location: newLocation)
}
}
/// The square's color.
public var color: Color {
return (file.index & 1 != rank.index & 1) ? ._white : ._black
}
/// A textual representation of `self`.
public var description: String {
return "\(file)\(rank)"
}
/// Create a square from `file` and `rank`.
public init(file: File, rank: Rank) {
self.init(rawValue: file.index + (rank.index << 3))!
}
/// Create a square from `location`.
public init(location: Location) {
self.init(file: location.file, rank: location.rank)
}
/// Create a square from `file` and `rank`. Returns `nil` if either is `nil`.
public init?(file: File?, rank: Rank?) {
guard let file = file, let rank = rank else {
return nil
}
self.init(file: file, rank: rank)
}
/// Create a square from `string`.
public init?(_ string: String) {
let chars = string.characters
guard chars.count == 2 else {
return nil
}
guard let file = File(chars.first!) else {
return nil
}
guard let rank = Int(String(chars.last!)).flatMap(Rank.init(_:)) else {
return nil
}
self.init(file: file, rank: rank)
}
/// Returns a bitboard mask of attacks for a king at `self`.
public func kingAttacks() -> Bitboard {
return _kingAttackTable[rawValue]
}
/// Returns a bitboard mask of attacks for a knight at `self`.
public func knightAttacks() -> Bitboard {
return _knightAttackTable[rawValue]
}
/// Returns a bitboard mask of attacks for a piece at `self`.
///
/// - parameter piece: The piece for the attacks.
/// - parameter stoppers: The pieces stopping a sliding move. The returned bitboard includes the stopped space.
///
/// - seealso: `attackMoves(for:stoppers:)`
public func attacks(for piece: Piece, stoppers: Bitboard = 0) -> Bitboard {
#if swift(>=3)
switch piece.kind {
case .king:
return kingAttacks()
case .knight:
return knightAttacks()
case .pawn:
return _pawnAttackTable(for: piece.color)[rawValue]
default:
return Bitboard(square: self)._attacks(for: piece, stoppers: stoppers)
}
#else
switch piece.kind {
case .King:
return kingAttacks()
case .Knight:
return knightAttacks()
case .Pawn:
return _pawnAttackTable(for: piece.color)[rawValue]
default:
return Bitboard(square: self)._attacks(for: piece, stoppers: stoppers)
}
#endif
}
/// Returns an array of attack moves for a piece at `self`.
///
/// - seealso: `attacks(for:stoppers:)`
public func attackMoves(for piece: Piece, stoppers: Bitboard = 0) -> [Move] {
return attacks(for: piece, stoppers: stoppers).moves(from: self)
}
#if swift(>=3)
/// Returns moves from the squares in `squares` to `self`.
public func moves<S: Sequence where S.Iterator.Element == Square>(from squares: S) -> [Move] {
return squares.moves(to: self)
}
/// Returns moves from `self` to the squares in `squares`.
public func moves<S: Sequence where S.Iterator.Element == Square>(to squares: S) -> [Move] {
return squares.moves(from: self)
}
#else
/// Returns moves from the squares in `squares` to `self`.
public func moves<S: SequenceType where S.Generator.Element == Square>(from squares: S) -> [Move] {
return squares.moves(to: self)
}
/// Returns moves from `self` to the squares in `squares`.
public func moves<S: SequenceType where S.Generator.Element == Square>(to squares: S) -> [Move] {
return squares.moves(from: self)
}
#endif
}
extension Square: StringLiteralConvertible {
/// Create an instance initialized to `value`.
public init(stringLiteral value: String) {
guard let square = Square(value) else {
fatalError("Invalid string for square: \"\(value)\"")
}
self = square
}
/// Create an instance initialized to `value`.
public init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
/// Create an instance initialized to `value`.
public init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
}