@@ -34,7 +34,7 @@ public struct Board {
34
34
/// Initializes a board with the given `position`.
35
35
///
36
36
/// - parameter position: The starting position of the board.
37
- /// Defaults to `Position.standard` which is the starting position
37
+ /// The default is `Position.standard` which is the starting position
38
38
/// for a standard chess game.
39
39
///
40
40
public init ( position: Position = . standard) {
@@ -48,6 +48,7 @@ public struct Board {
48
48
///
49
49
/// - parameter start: The starting square of the piece.
50
50
/// - parameter end: The ending square of the piece.
51
+ ///
51
52
/// - returns: The `Move` object representing the move.
52
53
///
53
54
/// If `start` doesn't contain a piece or `end` is not a valid legal move
@@ -155,7 +156,9 @@ public struct Board {
155
156
///
156
157
/// - parameter square: The square currently containing the piece.
157
158
/// - parameter newSquare: The new square for the piece.
159
+ ///
158
160
/// - returns: Whether or not the move is valid.
161
+ ///
159
162
public func canMove( pieceAt square: Square , to newSquare: Square ) -> Bool {
160
163
guard let piece = set. get ( square) else { return false }
161
164
return legalMoves ( for: piece, in: set) & newSquare. bb != 0
@@ -164,8 +167,10 @@ public struct Board {
164
167
/// Returns the possible legal moves for a piece at a given square.
165
168
///
166
169
/// - parameter square: The square containing the piece to check.
170
+ ///
167
171
/// - returns: An array of squares containing legal moves or an empty
168
172
/// array if there are no legal moves or if there is no piece at `square`.
173
+ ///
169
174
public func legalMoves( forPieceAt square: Square ) -> [ Square ] {
170
175
guard let piece = set. get ( square) else { return [ ] }
171
176
return legalMoves ( for: piece, in: set) . squares
@@ -175,6 +180,7 @@ public struct Board {
175
180
///
176
181
/// - parameter move: The move that triggered the promotion.
177
182
/// - parameter kind: The piece kind to promote a pawn to.
183
+ ///
178
184
/// - returns: The final move containing the promoted piece.
179
185
///
180
186
/// Call this when a pawn reaches the opposite side of the board
@@ -196,7 +202,6 @@ public struct Board {
196
202
197
203
/// Determines check state and handles pawn promotion for
198
204
/// provided `move`.
199
- ///
200
205
private func process( move: Move ) -> Move {
201
206
var processedMove = move
202
207
@@ -223,9 +228,7 @@ public struct Board {
223
228
return processedMove
224
229
}
225
230
226
- /// Determines the current check state for the
227
- /// provided `color`.
228
- ///
231
+ /// Determines the current check state for the provided `color`.
229
232
private func checkState( for color: Piece . Color ) -> Move . CheckState {
230
233
var checkState : Move . CheckState = . none
231
234
@@ -281,6 +284,7 @@ public struct Board {
281
284
282
285
// MARK: - Move Validation
283
286
287
+ /// Determines the legal moves for the given `piece` in `set`.
284
288
private func legalMoves( for piece: Piece , in set: PieceSet ) -> Bitboard {
285
289
let attacks = switch piece. kind {
286
290
case . king:
@@ -312,6 +316,7 @@ public struct Board {
312
316
///
313
317
/// - parameter piece: The piece to move.
314
318
/// - parameter square: The square to move the piece to.
319
+ ///
315
320
/// - returns: Whether the move is valid.
316
321
///
317
322
private func validate( moveFor piece: Piece , to square: Square ) -> Bool {
@@ -333,6 +338,7 @@ public struct Board {
333
338
///
334
339
/// - parameter sq: A bitboard corresponding to the square of interest.
335
340
/// - parameter set: The piece set for which to calculate attackers.
341
+ ///
336
342
/// - returns: A bitboard with the locations of the pieces in `set`
337
343
/// that attack `sq`.
338
344
///
@@ -354,6 +360,7 @@ public struct Board {
354
360
///
355
361
/// - parameter color: The color of the king.
356
362
/// - parameter set: The set of pieces on the board.
363
+ ///
357
364
/// - returns: Whether or not the king with `color` is in check.
358
365
///
359
366
private func isKingInCheck( _ color: Piece . Color , set: PieceSet ) -> Bool {
@@ -395,10 +402,7 @@ public struct Board {
395
402
let singleMove = movement ( 1 )
396
403
397
404
// double pawn push for starting move
398
- var extraMove = Bitboard ( 0 )
399
- if isOnStartingRank {
400
- extraMove = movement ( 2 )
401
- }
405
+ let extraMove = isOnStartingRank ? movement ( 2 ) : 0
402
406
403
407
// en passant move
404
408
var enPassantMove = Bitboard ( 0 )
@@ -418,6 +422,7 @@ public struct Board {
418
422
/// - parameter color: The color of the pawn.
419
423
/// - parameter sq: A bitboard representing the square the pawn is currently on.
420
424
/// - parameter set: The set of pieces active on the board.
425
+ ///
421
426
/// - returns: A bitboard of the possible capturing pawn moves.
422
427
///
423
428
/// For the purposes of `Board`, en-passant is not considered a capturing move.
@@ -437,6 +442,7 @@ public struct Board {
437
442
/// - parameter color: The color of the pawn.
438
443
/// - parameter sq: A bitboard representing the square the pawn is currently on.
439
444
/// - parameter set: The set of pieces active on the board.
445
+ ///
440
446
/// - returns: A bitboard of the possible pawn moves.
441
447
///
442
448
private func pawnAttacks(
@@ -501,7 +507,6 @@ public struct Board {
501
507
502
508
/// Determines whether the king of the provided `color` can
503
509
/// castle according to `castling` given `set`.
504
- ///
505
510
private func canCastle( _ color: Piece . Color , castling: Castling , set: PieceSet ) -> Bool {
506
511
let us = set. get ( color)
507
512
@@ -512,10 +517,13 @@ public struct Board {
512
517
attackers ( to: $0. bb, set: set) & ~ us == 0
513
518
}
514
519
520
+ let notInCheck = !isKingInCheck( color, set: set)
521
+
515
522
return position. legalCastlings. contains ( castling)
516
523
&& validKing != 0
517
524
&& validRook != 0
518
525
&& notCastlingThroughCheck
526
+ && notInCheck
519
527
}
520
528
521
529
}
0 commit comments