diff --git a/SwiftChess/.swiftlint.yml b/SwiftChess/.swiftlint.yml new file mode 100644 index 0000000..b89f5c4 --- /dev/null +++ b/SwiftChess/.swiftlint.yml @@ -0,0 +1,7 @@ +disabled_rules: + - trailing_whitespace + - cyclomatic_complexity + - nesting + - type_name + - force_cast + - identifier_name diff --git a/SwiftChess/Source/AIPlayer.swift b/SwiftChess/Source/AIPlayer.swift index dc3f003..a7273cc 100644 --- a/SwiftChess/Source/AIPlayer.swift +++ b/SwiftChess/Source/AIPlayer.swift @@ -6,16 +6,16 @@ // // - +// swiftlint:disable for_where import Foundation -open class AIPlayer : Player { +open class AIPlayer: Player { let boardRaters: [BoardRater]! public var configuration: AIConfiguration! var openingMoves = [OpeningMove]() - public init(color: Color, configuration: AIConfiguration){ + public init(color: Color, configuration: AIConfiguration) { self.configuration = configuration @@ -58,12 +58,12 @@ open class AIPlayer : Player { var move: Move! // Get an opening move - if let openingMove = openingMoveForBoard(board){ + if let openingMove = openingMoveForBoard(board) { //print("Playing opening move") move = openingMove } // Or, get the Highest rated move - else{ + else { move = highestRatedMoveOnBoard(board) } @@ -73,10 +73,8 @@ open class AIPlayer : Player { switch move.type { case .singlePiece(let sourceLocation, let targetLocation): operations = game.board.movePiece(fromLocation: sourceLocation, toLocation: targetLocation) - //print("Chose move (\(sourceLocation.x),\(sourceLocation.y)) -> (\(targetLocation.x),\(targetLocation.y))"); case .castle(let color, let side): operations = game.board.performCastle(color: color, side: side) - //print("Chose Castling move"); } // Promote pawns @@ -86,11 +84,12 @@ open class AIPlayer : Player { game.board = promotePawnsOnBoard(game.board) let location = pawnsToPromoteLocations.first! - let transformOperation = BoardOperation(type: .transformPiece, piece: game.board.getPiece(at: location)!, location: location) + let transformOperation = BoardOperation(type: .transformPiece, + piece: game.board.getPiece(at: location)!, + location: location) operations.append(transformOperation) } - let strongGame = self.game! DispatchQueue.main.async { strongGame.playerDidMakeMove(player: self, boardOperations: operations) @@ -99,13 +98,11 @@ open class AIPlayer : Player { func openingMoveForBoard(_ board: Board) -> Move? { - let possibleMoves = openingMoves.filter{ + let possibleMoves = openingMoves.filter { $0.board == board } - //print("Num opening moves`; \(possibleMoves.count)") - - guard possibleMoves.count > 0 else{ + guard possibleMoves.count > 0 else { return nil } @@ -153,7 +150,7 @@ open class AIPlayer : Player { // reduce rating if suicide if resultBoard.canColorMoveAnyPieceToLocation(color: color.opposite(), location: targetLocation) { - rating -= (abs(rating) * configuration.suicideMultipler.value); + rating -= (abs(rating) * configuration.suicideMultipler.value) } let move = Move(type: .singlePiece(sourceLocation: sourceLocation, targetLocation: targetLocation), @@ -185,7 +182,7 @@ open class AIPlayer : Player { // If there are no possible moves, we must be in stale mate if possibleMoves.count == 0 { - print("There are no possible moves!!!!"); + print("There are no possible moves!!!!") } // Choose move with highest rating @@ -196,13 +193,13 @@ open class AIPlayer : Player { if move.rating > highestRating { highestRating = move.rating - highestRatedMove = move; + highestRatedMove = move } //print("rating: \(move.rating)") } - return highestRatedMove; + return highestRatedMove } func canAIMovePiece(fromLocation: BoardLocation, toLocation: BoardLocation) -> Bool { @@ -217,10 +214,9 @@ open class AIPlayer : Player { return canMove.result } - func ratingForBoard(_ board: Board) -> Double { - var rating: Double = 0; + var rating: Double = 0 for boardRater in boardRaters { @@ -239,7 +235,6 @@ open class AIPlayer : Player { return rating } - func promotePawnsOnBoard(_ board: Board) -> Board { let pawnsToPromoteLocations = board.getLocationsOfPromotablePawns(color: color) @@ -295,7 +290,7 @@ struct Move { let rating: Double } -// MARK - BoardRater +// MARK: - BoardRater internal class BoardRater { @@ -305,10 +300,7 @@ internal class BoardRater { self.configuration = configuration } - func ratingfor(board: Board, color: Color) -> Double{ + func ratingfor(board: Board, color: Color) -> Double { fatalError("Override ratingFor method in subclasses") } } - - - diff --git a/SwiftChess/Source/ASCIIBoard.swift b/SwiftChess/Source/ASCIIBoard.swift index e4cd187..b4b020e 100644 --- a/SwiftChess/Source/ASCIIBoard.swift +++ b/SwiftChess/Source/ASCIIBoard.swift @@ -8,13 +8,13 @@ import Foundation -func transformASCIIBoardInput(_ input: String) -> String{ +func transformASCIIBoardInput(_ input: String) -> String { let boardArt = input.replacingOccurrences(of: " ", with: "") var transformedArt = String() - for y in (0...7).reversed(){ + for y in (0...7).reversed() { for x in 0...7 { let index = y*8 + x @@ -65,12 +65,12 @@ public struct ASCIIBoard { var board = Board(state: .empty) // Clear all pieces on the board - BoardLocation.all.forEach{ + BoardLocation.all.forEach { board.removePiece(atLocation: $0) } // Setup pieces from ascii art - (0..<64).forEach{ + (0..<64).forEach { let character = boardArt[boardArt.characters.index(boardArt.startIndex, offsetBy: $0)] if let piece = pieceFromCharacter(character) { @@ -81,7 +81,6 @@ public struct ASCIIBoard { return board } - func pieceFromCharacter(_ character: Character) -> Piece? { var piece: Piece? @@ -118,7 +117,7 @@ public struct ASCIIBoard { return piece } - public func indexOfCharacter(_ character: Character) -> Int{ + public func indexOfCharacter(_ character: Character) -> Int { var index: Int? @@ -136,11 +135,11 @@ public struct ASCIIBoard { return BoardLocation(index: index) } - public func indexesWithCharacter(_ character: Character) -> [Int]{ + public func indexesWithCharacter(_ character: Character) -> [Int] { var indexes = [Int]() - (0..<64).forEach{ + (0..<64).forEach { let aCharacter = artString[artString.characters.index(artString.startIndex, offsetBy: $0)] if character == aCharacter { indexes.append($0) @@ -156,7 +155,7 @@ public struct ASCIIBoard { var locations = [BoardLocation]() - indexes.forEach{ + indexes.forEach { let location = BoardLocation(index: $0) locations.append(location) } diff --git a/SwiftChess/Source/Board.swift b/SwiftChess/Source/Board.swift index 58f6578..1d3fb60 100644 --- a/SwiftChess/Source/Board.swift +++ b/SwiftChess/Source/Board.swift @@ -6,6 +6,9 @@ // // +// swiftlint:disable file_length +// swiftlint:disable type_body_length + import Foundation public enum CastleSide { @@ -15,13 +18,13 @@ public enum CastleSide { // MARK: - ****** Square ****** -public struct Square : Equatable { +public struct Square: Equatable { public var piece: Piece? } -public func ==(lhs: Square, rhs: Square) -> Bool { +public func == (lhs: Square, rhs: Square) -> Bool { switch (lhs.piece, rhs.piece) { case (.none, .none): @@ -37,7 +40,7 @@ public func ==(lhs: Square, rhs: Square) -> Bool { // MARK: - ****** Board ****** -public struct Board : Equatable { +public struct Board: Equatable { public enum InitialState { case empty @@ -63,7 +66,7 @@ public struct Board : Equatable { mutating func setupForNewGame() { - func setPieceAtIndex(_ piece: Piece, _ index: Int){ + func setPieceAtIndex(_ piece: Piece, _ index: Int) { setPiece(piece, at: BoardLocation(index: index)) } @@ -113,7 +116,8 @@ public struct Board : Equatable { squares[location.index].piece = nil } - @discardableResult internal mutating func movePiece(fromLocation: BoardLocation, toLocation: BoardLocation) -> [BoardOperation] { + @discardableResult internal mutating func movePiece(fromLocation: BoardLocation, + toLocation: BoardLocation) -> [BoardOperation] { if toLocation == fromLocation { return [] @@ -134,7 +138,7 @@ public struct Board : Equatable { } squares[toLocation.index].piece = self.squares[fromLocation.index].piece - squares[toLocation.index].piece?.location = toLocation; + squares[toLocation.index].piece?.location = toLocation squares[toLocation.index].piece?.hasMoved = true squares[fromLocation.index].piece = nil @@ -150,7 +154,7 @@ public struct Board : Equatable { } if enPassentPiece.canBeTakenByEnPassant && enPassentPiece.color == movingPiece.color.opposite() { - squares[enPassentLocation.index].piece = nil; + squares[enPassentLocation.index].piece = nil let operation = BoardOperation(type: .removePiece, piece: enPassentPiece, location: enPassentLocation) operations.append(operation) } @@ -188,7 +192,7 @@ public struct Board : Equatable { for square in squares { - guard let piece = square.piece else{ + guard let piece = square.piece else { continue } @@ -198,7 +202,8 @@ public struct Board : Equatable { } } - // We'll implicitly unwrap this, because there should always be a king for each color on the board. If there isn't, it's an error + // We'll implicitly unwrap this, because there should always be a king for each color on the board. + // If there isn't, it's an error return king! } @@ -206,7 +211,7 @@ public struct Board : Equatable { for (index, square) in squares.enumerated() { - guard let piece = square.piece else{ + guard let piece = square.piece else { continue } @@ -242,7 +247,7 @@ public struct Board : Equatable { for square in squares { - guard let piece = square.piece else{ + guard let piece = square.piece else { continue } @@ -356,8 +361,7 @@ public struct Board : Equatable { if !isColorAbleToMove(color: color) && !isColorInCheckMate(color: color) { return true - } - else{ + } else { return false } } @@ -405,7 +409,8 @@ public struct Board : Equatable { continue } - if piece.movement.canPieceMove(fromLocation: BoardLocation(index: index), toLocation: location, board: self) { + if piece.movement.canPieceMove(fromLocation: BoardLocation(index: index), + toLocation: location, board: self) { return true } } @@ -424,14 +429,14 @@ public struct Board : Equatable { public func possibleMoveLocationsForPiece(atLocation location: BoardLocation) -> [BoardLocation] { - guard let piece = squares[location.index].piece else{ + guard let piece = squares[location.index].piece else { return [] } var locations = [BoardLocation]() - BoardLocation.all.forEach{ - if piece.movement.canPieceMove(fromLocation: location, toLocation: $0, board: self){ + BoardLocation.all.forEach { + if piece.movement.canPieceMove(fromLocation: location, toLocation: $0, board: self) { locations.append($0) } } @@ -441,7 +446,7 @@ public struct Board : Equatable { // MARK: - Castling - struct CastleMove{ + struct CastleMove { let yPos: Int let kingStartXPos: Int let rookStartXPos: Int @@ -524,7 +529,9 @@ public struct Board : Equatable { } // Check that there are no pieces between the king and the rook - for xPos in min(castleMove.kingStartXPos, castleMove.rookStartXPos).. [BoardOperation] { - assert(canColorCastle(color: color, side: side) == true, "\(color) is unable to castle on side \(side). Call canColorCastle(color: side:) first") + assert(canColorCastle(color: color, side: side) == true, + "\(color) is unable to castle on side \(side). Call canColorCastle(color: side:) first") let castleMove = CastleMove(color: color, side: side) - let moveKingOperations = self.movePiece(fromLocation: castleMove.kingStartLocation, toLocation: castleMove.kingEndLocation) - let moveRookOperations = self.movePiece(fromLocation: castleMove.rookStartLocation, toLocation: castleMove.rookEndLocation) + let moveKingOperations = self.movePiece(fromLocation: castleMove.kingStartLocation, + toLocation: castleMove.kingEndLocation) + let moveRookOperations = self.movePiece(fromLocation: castleMove.rookStartLocation, + toLocation: castleMove.rookEndLocation) return moveKingOperations + moveRookOperations } @@ -578,10 +590,10 @@ public struct Board : Equatable { public func printBoardColors() { printBoard { (square: Square) -> Character? in - if let piece = square.piece{ + if let piece = square.piece { return piece.color == .white ? "W" : "B" } - return nil; + return nil } } @@ -590,9 +602,9 @@ public struct Board : Equatable { var character: Character? - if let piece = square.piece{ + if let piece = square.piece { - switch (piece.type){ + switch piece.type { case .rook: character = "R" case .knight: @@ -608,7 +620,7 @@ public struct Board : Equatable { } } - return character; + return character } } @@ -617,9 +629,9 @@ public struct Board : Equatable { var character: Character? - if let piece = square.piece{ + if let piece = square.piece { - switch (piece.type){ + switch piece.type { case .rook: character = piece.color == .white ? "R" : "r" case .knight: @@ -635,17 +647,15 @@ public struct Board : Equatable { } } - return character; + return character } } - - - func printBoard( _ squarePrinter: (Square) -> Character? ){ + func printBoard( _ squarePrinter: (Square) -> Character? ) { var printString = String() - for y in (0...7).reversed(){ + for y in (0...7).reversed() { for x in 0...7 { let index = y*8 + x @@ -660,6 +670,6 @@ public struct Board : Equatable { } } -public func ==(lhs: Board, rhs: Board) -> Bool { +public func == (lhs: Board, rhs: Board) -> Bool { return lhs.squares == rhs.squares } diff --git a/SwiftChess/Source/BoardLocation.swift b/SwiftChess/Source/BoardLocation.swift index 5bf5e11..4b8faf4 100644 --- a/SwiftChess/Source/BoardLocation.swift +++ b/SwiftChess/Source/BoardLocation.swift @@ -8,17 +8,17 @@ import Foundation -public struct BoardLocation : Equatable { +public struct BoardLocation: Equatable { - public enum GridPosition : Int{ - case a1; case b1; case c1; case d1; case e1; case f1; case g1; case h1; - case a2; case b2; case c2; case d2; case e2; case f2; case g2; case h2; - case a3; case b3; case c3; case d3; case e3; case f3; case g3; case h3; - case a4; case b4; case c4; case d4; case e4; case f4; case g4; case h4; - case a5; case b5; case c5; case d5; case e5; case f5; case g5; case h5; - case a6; case b6; case c6; case d6; case e6; case f6; case g6; case h6; - case a7; case b7; case c7; case d7; case e7; case f7; case g7; case h7; - case a8; case b8; case c8; case d8; case e8; case f8; case g8; case h8; + public enum GridPosition: Int { + case a1; case b1; case c1; case d1; case e1; case f1; case g1; case h1 + case a2; case b2; case c2; case d2; case e2; case f2; case g2; case h2 + case a3; case b3; case c3; case d3; case e3; case f3; case g3; case h3 + case a4; case b4; case c4; case d4; case e4; case f4; case g4; case h4 + case a5; case b5; case c5; case d5; case e5; case f5; case g5; case h5 + case a6; case b6; case c6; case d6; case e6; case f6; case g6; case h6 + case a7; case b7; case c7; case d7; case e7; case f7; case g7; case h7 + case a8; case b8; case c8; case d8; case e8; case f8; case g8; case h8 } public var index: Int @@ -28,11 +28,10 @@ public struct BoardLocation : Equatable { if let all = allLocationsBacking { return all - } - else{ + } else { var locations = [BoardLocation]() - (0..<64).forEach{ + (0..<64).forEach { locations.append(BoardLocation(index: $0)) } @@ -65,7 +64,7 @@ public struct BoardLocation : Equatable { self.index = x + (y*8) } - public init(gridPosition: GridPosition){ + public init(gridPosition: GridPosition) { self.index = gridPosition.rawValue } @@ -83,9 +82,11 @@ public struct BoardLocation : Equatable { func incrementedBy(stride: BoardStride) -> BoardLocation { - // TODO: for performance, we should probably only check this if we're in debug mode + // TODO: Only call this in debug mode to increace performance! if !canIncrementBy(stride: stride) { + // swiftlint:disable line_length print("WARNING! BoardLocation is being incremented by a stride that will result in wrapping! call canIncrementBy(stride: BoardStride) first") + // swiftlint:enable line_length } return BoardLocation(x: x + stride.x, @@ -95,22 +96,22 @@ public struct BoardLocation : Equatable { func canIncrementBy(stride: BoardStride) -> Bool { // Check will not wrap to right - if x + stride.x > 7 { + if x + stride.x > 7 { return false } // Check will not wrap to left - if x + stride.x < 0 { + if x + stride.x < 0 { return false } // Check will not wrap top - if y + stride.y > 7 { + if y + stride.y > 7 { return false } // Check will not wrap bottom - if y + stride.y < 0 { + if y + stride.y < 0 { return false } @@ -130,10 +131,10 @@ public struct BoardLocation : Equatable { } } -public func ==(lhs: BoardLocation, rhs: BoardLocation) -> Bool { +public func == (lhs: BoardLocation, rhs: BoardLocation) -> Bool { return lhs.index == rhs.index } -public func +(left: BoardLocation, right: BoardLocation) -> BoardLocation { +public func + (left: BoardLocation, right: BoardLocation) -> BoardLocation { return BoardLocation(index: left.index + right.index) } diff --git a/SwiftChess/Source/BoardRaterBoardDominance.swift b/SwiftChess/Source/BoardRaterBoardDominance.swift index 71f3626..25875c3 100644 --- a/SwiftChess/Source/BoardRaterBoardDominance.swift +++ b/SwiftChess/Source/BoardRaterBoardDominance.swift @@ -8,30 +8,30 @@ import Foundation -class BoardRaterBoardDominance : BoardRater { +class BoardRaterBoardDominance: BoardRater { override func ratingfor(board: Board, color: Color) -> Double { - let squareValue = Double(1); + let squareValue = Double(1) var rating = Double(0) // Check this color pieces for sourcelocation in BoardLocation.all { - guard let piece = board.getPiece(at: sourcelocation) else{ - continue; + guard let piece = board.getPiece(at: sourcelocation) else { + continue } for targetLocation in BoardLocation.all { if piece.movement.canPieceMove(fromLocation: sourcelocation, toLocation: targetLocation, board: board) { - rating += (piece.color == color) ? squareValue : -squareValue; + rating += (piece.color == color) ? squareValue : -squareValue } } } - return rating * configuration.boardRaterBoardDominanceWeighting.value; + return rating * configuration.boardRaterBoardDominanceWeighting.value } } diff --git a/SwiftChess/Source/BoardRaterCenterDominance.swift b/SwiftChess/Source/BoardRaterCenterDominance.swift index d025457..2f6cc79 100644 --- a/SwiftChess/Source/BoardRaterCenterDominance.swift +++ b/SwiftChess/Source/BoardRaterCenterDominance.swift @@ -12,7 +12,7 @@ import Foundation Rates the board according to which player's pieces are able to move to the squares at the center of the board */ -class BoardRaterCenterDominance : BoardRater { +class BoardRaterCenterDominance: BoardRater { override func ratingfor(board: Board, color: Color) -> Double { @@ -26,7 +26,11 @@ class BoardRaterCenterDominance : BoardRater { for targetLocation in BoardLocation.all { - if sourceLocation == targetLocation || piece.movement.canPieceMove(fromLocation: sourceLocation, toLocation: targetLocation, board: board) { + if sourceLocation == targetLocation || + piece.movement.canPieceMove(fromLocation: sourceLocation, + toLocation: targetLocation, + board: board) { + let value = dominanceValueFor(location: targetLocation) rating += (piece.color == color) ? value : -value } @@ -39,7 +43,7 @@ class BoardRaterCenterDominance : BoardRater { func dominanceValueFor(location: BoardLocation) -> Double { - let axisMiddle = 3.5; + let axisMiddle = 3.5 let x = Double(location.x) let y = Double(location.y) @@ -51,5 +55,4 @@ class BoardRaterCenterDominance : BoardRater { return axisMiddle - distance } - } diff --git a/SwiftChess/Source/BoardRaterCenterFourOccupation.swift b/SwiftChess/Source/BoardRaterCenterFourOccupation.swift index dfa23d4..256de02 100644 --- a/SwiftChess/Source/BoardRaterCenterFourOccupation.swift +++ b/SwiftChess/Source/BoardRaterCenterFourOccupation.swift @@ -12,14 +12,14 @@ class BoardRaterCenterFourOccupation: BoardRater { override func ratingfor(board: Board, color: Color) -> Double { - let value = Double(1); + let value = Double(1) var rating = Double(0) let locations = [ BoardLocation(x: 4, y: 4), // NE BoardLocation(x: 4, y: 3), // SE BoardLocation(x: 3, y: 3), // SW - BoardLocation(x: 3, y: 4), // NW + BoardLocation(x: 3, y: 4) // NW ] for location in locations { diff --git a/SwiftChess/Source/BoardRaterCenterOwnership.swift b/SwiftChess/Source/BoardRaterCenterOwnership.swift index 344452a..d7aa4dd 100644 --- a/SwiftChess/Source/BoardRaterCenterOwnership.swift +++ b/SwiftChess/Source/BoardRaterCenterOwnership.swift @@ -12,7 +12,7 @@ import Foundation Rates the board according to which player's pieces are occupying the center of the board */ -class BoardRaterCenterOwnership : BoardRater { +class BoardRaterCenterOwnership: BoardRater { override func ratingfor(board: Board, color: Color) -> Double { @@ -35,7 +35,7 @@ class BoardRaterCenterOwnership : BoardRater { func dominanceValueFor(location: BoardLocation) -> Double { - let axisMiddle = 3.5; + let axisMiddle = 3.5 let x = Double(location.x) let y = Double(location.y) diff --git a/SwiftChess/Source/BoardRaterCheckMateOpportunity.swift b/SwiftChess/Source/BoardRaterCheckMateOpportunity.swift index 2d776ee..d712e33 100644 --- a/SwiftChess/Source/BoardRaterCheckMateOpportunity.swift +++ b/SwiftChess/Source/BoardRaterCheckMateOpportunity.swift @@ -14,7 +14,7 @@ import Foundation Iterates over all possible moves, makes them, and checks if the resulting state is check mate */ -class BoardRaterCheckMateOpportunity : BoardRater { +class BoardRaterCheckMateOpportunity: BoardRater { override func ratingfor(board: Board, color: Color) -> Double { @@ -23,7 +23,7 @@ class BoardRaterCheckMateOpportunity : BoardRater { for (index, square) in board.squares.enumerated() { - guard let piece = square.piece else{ + guard let piece = square.piece else { continue } @@ -36,10 +36,9 @@ class BoardRaterCheckMateOpportunity : BoardRater { var movedBoard = board movedBoard.movePiece(fromLocation: sourceLocation, toLocation: location) - if piece.color == color && movedBoard.isColorInCheckMate(color: color.opposite()){ + if piece.color == color && movedBoard.isColorInCheckMate(color: color.opposite()) { rating += value - } - else if piece.color == color.opposite() && movedBoard.isColorInCheckMate(color: color){ + } else if piece.color == color.opposite() && movedBoard.isColorInCheckMate(color: color) { rating -= value } } diff --git a/SwiftChess/Source/BoardRaterCountPieces.swift b/SwiftChess/Source/BoardRaterCountPieces.swift index 46b35c3..087d3d7 100644 --- a/SwiftChess/Source/BoardRaterCountPieces.swift +++ b/SwiftChess/Source/BoardRaterCountPieces.swift @@ -8,11 +8,11 @@ import Foundation -class BoardRaterCountPieces : BoardRater { +class BoardRaterCountPieces: BoardRater { override func ratingfor(board: Board, color: Color) -> Double { - var rating : Double = 0 + var rating: Double = 0 for square in board.squares { diff --git a/SwiftChess/Source/BoardRaterKingSurroundingPossession.swift b/SwiftChess/Source/BoardRaterKingSurroundingPossession.swift index 8842431..8004cab 100644 --- a/SwiftChess/Source/BoardRaterKingSurroundingPossession.swift +++ b/SwiftChess/Source/BoardRaterKingSurroundingPossession.swift @@ -8,7 +8,7 @@ import Foundation -class BoardRaterKingSurroundingPossession : BoardRater { +class BoardRaterKingSurroundingPossession: BoardRater { override func ratingfor(board: Board, color: Color) -> Double { @@ -30,7 +30,7 @@ class BoardRaterKingSurroundingPossession : BoardRater { if noKingsBoard.doesColorOccupyLocation(color: color, location: location) { rating += squareValue - continue; + continue } if noKingsBoard.doesColorOccupyLocation(color: color.opposite(), location: location) { @@ -40,9 +40,7 @@ class BoardRaterKingSurroundingPossession : BoardRater { if noKingsBoard.canColorMoveAnyPieceToLocation(color: color, location: location) { rating += squareValue - } - - else if noKingsBoard.canColorMoveAnyPieceToLocation(color: color.opposite(), location: location) { + } else if noKingsBoard.canColorMoveAnyPieceToLocation(color: color.opposite(), location: location) { rating -= squareValue } @@ -52,7 +50,7 @@ class BoardRaterKingSurroundingPossession : BoardRater { if noKingsBoard.doesColorOccupyLocation(color: color, location: location) { rating += squareValue - continue; + continue } if noKingsBoard.doesColorOccupyLocation(color: color.opposite(), location: location) { @@ -62,9 +60,7 @@ class BoardRaterKingSurroundingPossession : BoardRater { if noKingsBoard.canColorMoveAnyPieceToLocation(color: color, location: location) { rating += squareValue - } - - else if noKingsBoard.canColorMoveAnyPieceToLocation(color: color.opposite(), location: location) { + } else if noKingsBoard.canColorMoveAnyPieceToLocation(color: color.opposite(), location: location) { rating -= squareValue } } @@ -77,14 +73,14 @@ class BoardRaterKingSurroundingPossession : BoardRater { let kingLocation = board.getKingLocation(color: color) let strides = [ - BoardStride(x: 0, y: 1), // N - BoardStride(x: 1, y: 1), // NE - BoardStride(x: 1, y: 0), // E - BoardStride(x: 1, y: -1), // SE - BoardStride(x: 0, y: -1), // S + BoardStride(x: 0, y: 1), // N + BoardStride(x: 1, y: 1), // NE + BoardStride(x: 1, y: 0), // E + BoardStride(x: 1, y: -1), // SE + BoardStride(x: 0, y: -1), // S BoardStride(x: -1, y: -1), // SW - BoardStride(x: -1, y: 0), // W - BoardStride(x: -1, y: 1), // NW + BoardStride(x: -1, y: 0), // W + BoardStride(x: -1, y: 1) // NW ] var surroundingLocations = [BoardLocation]() diff --git a/SwiftChess/Source/BoardRaterPawnProgression.swift b/SwiftChess/Source/BoardRaterPawnProgression.swift index 2f9b615..510a0db 100644 --- a/SwiftChess/Source/BoardRaterPawnProgression.swift +++ b/SwiftChess/Source/BoardRaterPawnProgression.swift @@ -8,7 +8,7 @@ import Foundation -class BoardRaterPawnProgression : BoardRater { +class BoardRaterPawnProgression: BoardRater { override func ratingfor(board: Board, color: Color) -> Double { @@ -38,12 +38,11 @@ class BoardRaterPawnProgression : BoardRater { if color == .white { if location.y < 2 { - return 0; + return 0 } squaresAdvanced = location.y - 2 - } - else{ + } else { if location.y > 5 { return 0 @@ -52,7 +51,7 @@ class BoardRaterPawnProgression : BoardRater { squaresAdvanced = 7 - (location.y + 2) } - return Double(squaresAdvanced) * configuration.boardRaterPawnProgressionWeighting.value // <- should probably add some sort of curve + return Double(squaresAdvanced) * configuration.boardRaterPawnProgressionWeighting.value } } diff --git a/SwiftChess/Source/BoardRaterThreatenedPieces.swift b/SwiftChess/Source/BoardRaterThreatenedPieces.swift index 38a63a2..3889f48 100644 --- a/SwiftChess/Source/BoardRaterThreatenedPieces.swift +++ b/SwiftChess/Source/BoardRaterThreatenedPieces.swift @@ -10,59 +10,13 @@ import Foundation // Tendancy to protect own pieces -class BoardRaterThreatenedPieces : BoardRater { - - /* - override func ratingfor(board: Board, color: Color) -> Double { - - var rating = Double(0) - - let allPieces = board.squares.flatMap{ - $0.piece - } - - for piece in allPieces { - - var value = Double(0) - - let threatenedByPieces = getPieces(threatening: piece, onBoard: board) - let protectedByPieces = getPieces(protecting: piece, onBoard: board) - let isThreatened = threatenedByPieces.count > 0 - let isProtected = protectedByPieces.count > 0 - - switch (isThreatened, isProtected) { - case (false, true): - value = piece.value() - case (true, false): - value = -piece.value() - case (true, true): - let lowestValueThreat = threatenedByPieces.dropFirst().reduce(threatenedByPieces[0].value(), { (value: Double, piece) -> Double in - return min(value, piece.value()) - }) - - if lowestValueThreat < piece.value() { - value = -piece.value() - } - case (false, false): - break - } - - if piece.color == color.opposite() { - value = -value - } - - rating += value - } - - return rating * configuration.boardRaterThreatenedPiecesWeighting - } - */ +class BoardRaterThreatenedPieces: BoardRater { override func ratingfor(board: Board, color: Color) -> Double { let rating = board.getPieces(color: color) - .map{ threatValue(forPiece: $0, onBoard: board) } - .reduce(0,+) + .map { threatValue(forPiece: $0, onBoard: board) } + .reduce(0, +) * configuration.boardRaterThreatenedPiecesWeighting.value return rating @@ -100,8 +54,7 @@ class BoardRaterThreatenedPieces : BoardRater { // If it's protected, is it a good trade if isTargetProtected && targetPiece.value() < piece.value() { return 0 - } - else{ + } else { return targetPiece.value() } } @@ -117,15 +70,21 @@ class BoardRaterThreatenedPieces : BoardRater { var alteredBoard = board alteredBoard.setPiece(piece.withOppositeColor(), at: piece.location) - return alteredBoard.getPieces(color: piece.color).filter{ - $0.movement.canPieceMove(fromLocation: $0.location, toLocation: piece.location, board: alteredBoard, accountForCheckState: true) + return alteredBoard.getPieces(color: piece.color).filter { + $0.movement.canPieceMove(fromLocation: $0.location, + toLocation: piece.location, + board: alteredBoard, + accountForCheckState: true) } } func getPieces(protectedBy piece: Piece, onBoard board: Board) -> [Piece] { - return board.getPieces(color: piece.color).filter{ - piece.movement.canPieceMove(fromLocation: piece.location, toLocation: $0.location, board: board, accountForCheckState: true) + return board.getPieces(color: piece.color).filter { + piece.movement.canPieceMove(fromLocation: piece.location, + toLocation: $0.location, + board: board, + accountForCheckState: true) } } @@ -167,7 +126,10 @@ class BoardRaterThreatenedPieces : BoardRater { continue } - if squarePiece.movement.canPieceMove(fromLocation: squarePiece.location, toLocation: piece.location, board: board, accountForCheckState: true) { + if squarePiece.movement.canPieceMove(fromLocation: squarePiece.location, + toLocation: piece.location, + board: board, + accountForCheckState: true) { return true } } @@ -175,18 +137,23 @@ class BoardRaterThreatenedPieces : BoardRater { return false } - func getPieces(threatening piece: Piece, onBoard board: Board) -> [Piece] { - return board.getPieces(color: piece.color.opposite()).filter{ - $0.movement.canPieceMove(fromLocation: $0.location, toLocation: piece.location, board: board, accountForCheckState: true) + return board.getPieces(color: piece.color.opposite()).filter { + $0.movement.canPieceMove(fromLocation: $0.location, + toLocation: piece.location, + board: board, + accountForCheckState: true) } } func getPieces(threatenedBy piece: Piece, onBoard board: Board) -> [Piece] { - return board.getPieces(color: piece.color.opposite()).filter{ - piece.movement.canPieceMove(fromLocation: piece.location, toLocation: $0.location, board: board, accountForCheckState: true) + return board.getPieces(color: piece.color.opposite()).filter { + piece.movement.canPieceMove(fromLocation: piece.location, + toLocation: $0.location, + board: board, + accountForCheckState: true) } } @@ -194,7 +161,10 @@ class BoardRaterThreatenedPieces : BoardRater { for location in BoardLocation.all { - if piece.movement.canPieceMove(fromLocation: piece.location, toLocation: location, board: board, accountForCheckState: true) { + if piece.movement.canPieceMove(fromLocation: piece.location, + toLocation: location, + board: board, + accountForCheckState: true) { var boardCopy = board boardCopy.movePiece(fromLocation: piece.location, toLocation: location) @@ -209,7 +179,7 @@ class BoardRaterThreatenedPieces : BoardRater { } } -extension Collection where Iterator.Element == Piece{ +extension Collection where Iterator.Element == Piece { func lowestPieceValue() -> Double { diff --git a/SwiftChess/Source/BoardStride.swift b/SwiftChess/Source/BoardStride.swift index 5926e28..b91f719 100644 --- a/SwiftChess/Source/BoardStride.swift +++ b/SwiftChess/Source/BoardStride.swift @@ -10,12 +10,11 @@ import Foundation public struct BoardStride { - public var x: Int; - public var y: Int; + public var x: Int + public var y: Int - public init(x: Int, y: Int){ - self.x = x; - self.y = y; + public init(x: Int, y: Int) { + self.x = x + self.y = y } } - diff --git a/SwiftChess/Source/Game.swift b/SwiftChess/Source/Game.swift index 41557f6..50ad1b3 100644 --- a/SwiftChess/Source/Game.swift +++ b/SwiftChess/Source/Game.swift @@ -16,7 +16,7 @@ open class Game { case staleMate(color: Color) case won(color: Color) - public static func ==(lhs: State, rhs: State) -> Bool { + public static func == (lhs: State, rhs: State) -> Bool { switch (lhs, rhs) { case (.inProgress, .inProgress): return true @@ -57,7 +57,10 @@ open class Game { } // MARK: Init - public init(firstPlayer: Player, secondPlayer: Player, board: Board = Board(state: .newGame), colorToMove: Color = .white){ + public init(firstPlayer: Player, + secondPlayer: Player, + board: Board = Board(state: .newGame), + colorToMove: Color = .white) { self.board = board @@ -77,11 +80,10 @@ open class Game { self.currentPlayer = (colorToMove == .white ? self.whitePlayer : self.blackPlayer) } - } // MARK: - Game : PlayerDelegate -extension Game : PlayerDelegate { +extension Game: PlayerDelegate { func playerDidMakeMove(player: Player, boardOperations: [BoardOperation]) { @@ -116,8 +118,7 @@ extension Game : PlayerDelegate { // Switch to the other player if player === whitePlayer { currentPlayer = blackPlayer - } - else{ + } else { currentPlayer = whitePlayer } @@ -131,11 +132,17 @@ extension Game : PlayerDelegate { switch boardOperation.type! { case .movePiece: - self.delegate?.gameDidMovePiece(game: self, piece: boardOperation.piece, toLocation: boardOperation.location) + self.delegate?.gameDidMovePiece(game: self, + piece: boardOperation.piece, + toLocation: boardOperation.location) case .removePiece: - self.delegate?.gameDidRemovePiece(game: self, piece: boardOperation.piece, location: boardOperation.location) + self.delegate?.gameDidRemovePiece(game: self, + piece: boardOperation.piece, + location: boardOperation.location) case .transformPiece: - self.delegate?.gameDidTransformPiece(game: self, piece: boardOperation.piece, location: boardOperation.location) + self.delegate?.gameDidTransformPiece(game: self, + piece: boardOperation.piece, + location: boardOperation.location) } } @@ -153,13 +160,23 @@ public protocol GameDelegate: class { func gameEndedInStaleMate(game: Game) // Piece adding / removing / modifying - func gameWillBeginUpdates(game: Game) // Updates will begin - func gameDidAddPiece(game: Game) // A new piece was added to the board (do we actually need to include this functionality?) - func gameDidRemovePiece(game: Game, piece: Piece, location: BoardLocation) // A piece was removed from the board - func gameDidMovePiece(game: Game, piece: Piece, toLocation: BoardLocation) // A piece was moved on the board - func gameDidTransformPiece(game: Game, piece: Piece, location: BoardLocation) // A piece was transformed (eg. pawn was promoted to another piece) - func gameDidEndUpdates(game: Game) // Updates will end + + // Updates will begin + func gameWillBeginUpdates(game: Game) + // A new piece was added to the board + func gameDidAddPiece(game: Game) + // A piece was removed from the board + func gameDidRemovePiece(game: Game, piece: Piece, location: BoardLocation) + // A piece was moved on the board + func gameDidMovePiece(game: Game, piece: Piece, toLocation: BoardLocation) + // A piece was transformed (eg. pawn was promoted to another piece) + func gameDidTransformPiece(game: Game, piece: Piece, location: BoardLocation) + // Updates will end) + func gameDidEndUpdates(game: Game) // Callbacks from player - func promotedTypeForPawn(location: BoardLocation, player: Human, possiblePromotions: [Piece.PieceType], callback: @escaping (Piece.PieceType) -> Void ) + func promotedTypeForPawn(location: BoardLocation, + player: Human, + possiblePromotions: [Piece.PieceType], + callback: @escaping (Piece.PieceType) -> Void ) } diff --git a/SwiftChess/Source/Human.swift b/SwiftChess/Source/Human.swift index 654fea8..fce89e1 100644 --- a/SwiftChess/Source/Human.swift +++ b/SwiftChess/Source/Human.swift @@ -6,11 +6,13 @@ // // +//swiftlint:disable line_length + import Foundation -open class Human : Player { +open class Human: Player { - public init(color: Color){ + public init(color: Color) { super.init() self.color = color } @@ -49,7 +51,7 @@ open class Human : Player { player: self, possiblePromotions: Piece.PieceType.possiblePawnPromotionResultingTypes(), callback: { - + // Change the piece let newPiece = self.game.board.squares[pawnLocation.index].piece?.byChangingType(newType: $0) self.game.board.setPiece(newPiece!, at: pawnLocation) @@ -63,7 +65,7 @@ open class Human : Player { }) } // ... Or if no pawn promotions, end move - else{ + else { // Inform the delegate that we made a move delegate?.playerDidMakeMove(player: self, boardOperations: operations) @@ -90,7 +92,4 @@ open class Human : Player { delegate?.playerDidMakeMove(player: self, boardOperations: operations) } - } - - diff --git a/SwiftChess/Source/Opening.swift b/SwiftChess/Source/Opening.swift index c9e6740..069fe6a 100644 --- a/SwiftChess/Source/Opening.swift +++ b/SwiftChess/Source/Opening.swift @@ -22,7 +22,7 @@ class Opening { ItalianGame(), SicilianDefense(), QueensGambit(), - KingsGambit(), + KingsGambit() ] } @@ -30,7 +30,7 @@ class Opening { var openingMoves = [OpeningMove]() - allOpenings().forEach{ + allOpenings().forEach { openingMoves += $0.moves(forColor: color) } @@ -42,7 +42,7 @@ class Opening { var moves = [OpeningMove]() var board = Board(state: .newGame) - for locations in moveLocations(){ + for locations in moveLocations() { let move = OpeningMove(board: board, fromLocation: locations.fromLocation, @@ -57,7 +57,7 @@ class Opening { } // Filter for color - return moves.enumerated().flatMap{ (index, value) in + return moves.enumerated().flatMap { (index, value) in index % 2 == (color == .white ? 0 : 1) ? value : nil } } @@ -71,7 +71,7 @@ class Opening { */ func moveLocations() -> [(fromLocation: BoardLocation, toLocation: BoardLocation)] { - return moveGridPositions().map{ + return moveGridPositions().map { (BoardLocation(gridPosition: $0), BoardLocation(gridPosition: $1)) } } @@ -86,14 +86,15 @@ class Opening { class RuyLopez: Opening { - override func moveGridPositions() -> [(fromPosition: BoardLocation.GridPosition, toPosition: BoardLocation.GridPosition)] { + override func moveGridPositions() -> + [(fromPosition: BoardLocation.GridPosition, toPosition: BoardLocation.GridPosition)] { let moves: [(BoardLocation.GridPosition, BoardLocation.GridPosition)] = [ (.e2, .e4), // white moves pawn to e4 (.e7, .e5), // black moves pawn to e5 (.g1, .f3), // white moves knight to f3 (.b8, .c6), // black moves knight to c6 - (.f1, .b5), // white moves bishop to b5 + (.f1, .b5) // white moves bishop to b5 ] return moves @@ -104,14 +105,15 @@ class RuyLopez: Opening { class ItalianGame: Opening { - override func moveGridPositions() -> [(fromPosition: BoardLocation.GridPosition, toPosition: BoardLocation.GridPosition)] { + override func moveGridPositions() -> + [(fromPosition: BoardLocation.GridPosition, toPosition: BoardLocation.GridPosition)] { let moves: [(BoardLocation.GridPosition, BoardLocation.GridPosition)] = [ (.e2, .e4), // white moves pawn to e4 (.e7, .e5), // black moves pawn to e5 (.g1, .f3), // white moves knight to f3 (.b8, .c6), // black moves knight to c6 - (.f1, .c4), // white moves bishop to c4 + (.f1, .c4) // white moves bishop to c4 ] return moves @@ -122,11 +124,12 @@ class ItalianGame: Opening { class SicilianDefense: Opening { - override func moveGridPositions() -> [(fromPosition: BoardLocation.GridPosition, toPosition: BoardLocation.GridPosition)] { + override func moveGridPositions() -> + [(fromPosition: BoardLocation.GridPosition, toPosition: BoardLocation.GridPosition)] { let moves: [(BoardLocation.GridPosition, BoardLocation.GridPosition)] = [ (.e2, .e4), // white moves pawn to e4 - (.c7, .c5), // black moves pawn to c5 + (.c7, .c5) // black moves pawn to c5 ] return moves @@ -137,12 +140,13 @@ class SicilianDefense: Opening { class QueensGambit: Opening { - override func moveGridPositions() -> [(fromPosition: BoardLocation.GridPosition, toPosition: BoardLocation.GridPosition)] { + override func moveGridPositions() -> + [(fromPosition: BoardLocation.GridPosition, toPosition: BoardLocation.GridPosition)] { let moves: [(BoardLocation.GridPosition, BoardLocation.GridPosition)] = [ (.d2, .d4), // white moves pawn to d4 (.d7, .d5), // black moves pawn to d5 - (.c2, .c4), // white moves pawn to c4 + (.c2, .c4) // white moves pawn to c4 ] return moves @@ -153,18 +157,15 @@ class QueensGambit: Opening { class KingsGambit: Opening { - override func moveGridPositions() -> [(fromPosition: BoardLocation.GridPosition, toPosition: BoardLocation.GridPosition)] { + override func moveGridPositions() -> + [(fromPosition: BoardLocation.GridPosition, toPosition: BoardLocation.GridPosition)] { let moves: [(BoardLocation.GridPosition, BoardLocation.GridPosition)] = [ (.e2, .e4), // white moves pawn to e4 (.e7, .e5), // black moves pawn to e5 - (.f2, .f4), // white moves pawn to f4 + (.f2, .f4) // white moves pawn to f4 ] return moves } } - - - - diff --git a/SwiftChess/Source/Piece.swift b/SwiftChess/Source/Piece.swift index 710933d..7a2be0e 100644 --- a/SwiftChess/Source/Piece.swift +++ b/SwiftChess/Source/Piece.swift @@ -25,7 +25,7 @@ public enum Color: Int { } } -public struct Piece : Equatable { +public struct Piece: Equatable { static private var lastAssignedTag = 0 @@ -49,11 +49,11 @@ public struct Piece : Equatable { public var canBeTakenByEnPassant = false public internal(set) var location = BoardLocation(index: 0) - var movement : PieceMovement! { + var movement: PieceMovement! { return PieceMovement.pieceMovementForPieceType(pieceType: self.type) } - public init(type: PieceType, color: Color){ + public init(type: PieceType, color: Color) { self.type = type self.color = color @@ -62,7 +62,7 @@ public struct Piece : Equatable { self.tag = Piece.lastAssignedTag } - public init(type: PieceType, color: Color, tag: Int){ + public init(type: PieceType, color: Color, tag: Int) { self.type = type self.color = color self.tag = tag @@ -95,8 +95,7 @@ public func == (left: Piece, right: Piece) -> Bool { if left.type == right.type && left.color == right.color { return true - } - else{ + } else { return false } } diff --git a/SwiftChess/Source/PieceMovement.swift b/SwiftChess/Source/PieceMovement.swift index 5574dae..13abd0e 100644 --- a/SwiftChess/Source/PieceMovement.swift +++ b/SwiftChess/Source/PieceMovement.swift @@ -6,6 +6,8 @@ // // +//swiftlint:disable file_length + import Foundation // MARK: - PieceMovement (Base Class) @@ -37,11 +39,13 @@ open class PieceMovement { } } - public init(){ - + public init() { } - func canPieceMove(fromLocation: BoardLocation, toLocation: BoardLocation, board: Board, accountForCheckState: Bool = false) -> Bool { + func canPieceMove(fromLocation: BoardLocation, + toLocation: BoardLocation, + board: Board, + accountForCheckState: Bool = false) -> Bool { if fromLocation == toLocation { return false @@ -56,8 +60,7 @@ open class PieceMovement { var boardCopy = board boardCopy.movePiece(fromLocation: fromLocation, toLocation: toLocation) return boardCopy.isColorInCheck(color: color) ? false : true - } - else{ + } else { return canMove } } @@ -66,9 +69,13 @@ open class PieceMovement { return false } - func canPieceMove(fromLocation: BoardLocation, toLocation: BoardLocation, board: Board, stride: BoardStride) -> Bool { + // swiftlint:disable function_body_length + func canPieceMove(fromLocation: BoardLocation, + toLocation: BoardLocation, + board: Board, + stride: BoardStride) -> Bool { - enum Direction: Int{ + enum Direction: Int { case increasing case decresing case none @@ -150,12 +157,12 @@ open class PieceMovement { return false } - func canPieceOccupySquare(pieceLocation: BoardLocation, xOffset: Int, yOffset: Int, board: Board) -> Bool{ + func canPieceOccupySquare(pieceLocation: BoardLocation, xOffset: Int, yOffset: Int, board: Board) -> Bool { let targetLocation = pieceLocation.incrementedBy(x: xOffset, y: yOffset) // Check if in bounds - guard targetLocation.isInBounds() else{ + guard targetLocation.isInBounds() else { return false } @@ -246,7 +253,7 @@ open class PieceMovementDiagonal: PieceMovement { open class PieceMovementQueen: PieceMovement { - let movements : [PieceMovement] = [PieceMovementStraightLine(), PieceMovementDiagonal()] + let movements: [PieceMovement] = [PieceMovementStraightLine(), PieceMovementDiagonal()] override func isMovementPossible(fromLocation: BoardLocation, toLocation: BoardLocation, board: Board) -> Bool { @@ -291,14 +298,14 @@ open class PieceMovementBishop: PieceMovement { open class PieceMovementKnight: PieceMovement { let offsets: [(x: Int, y: Int)] = [ - (1,2), - (2,1), - (2,-1), - (-2,1), - (-1,-2), - (-2,-1), - (1,-2), - (-1,2) + (1, 2), + (2, 1), + (2, -1), + (-2, 1), + (-1, -2), + (-2, -1), + (1, -2), + (-1, 2) ] override func isMovementPossible(fromLocation: BoardLocation, toLocation: BoardLocation, board: Board) -> Bool { @@ -315,7 +322,10 @@ open class PieceMovementKnight: PieceMovement { let offsetLocation = fromLocation.incrementedBy(x: offset.x, y: offset.y) if toLocation == offsetLocation - && canPieceOccupySquare(pieceLocation: fromLocation, xOffset: offset.x, yOffset: offset.y, board: board) { + && canPieceOccupySquare(pieceLocation: fromLocation, + xOffset: offset.x, + yOffset: offset.y, + board: board) { return true } } @@ -327,12 +337,13 @@ open class PieceMovementKnight: PieceMovement { // MARK: - PieceMovementPawn +// swiftlint:disable function_body_length open class PieceMovementPawn: PieceMovement { override func isMovementPossible(fromLocation: BoardLocation, toLocation: BoardLocation, board: Board) -> Bool { // Get the moving piece - guard let movingPiece = board.getPiece(at: fromLocation) else{ + guard let movingPiece = board.getPiece(at: fromLocation) else { return false } @@ -363,7 +374,7 @@ open class PieceMovementPawn: PieceMovement { let location = fromLocation.incrementedBy(stride: oneAheadStride) - if let _ = board.getPiece(at: location) { + if board.getPiece(at: location) != nil { canMoveOneAhead = false break ONE_AHEAD } @@ -373,7 +384,6 @@ open class PieceMovementPawn: PieceMovement { } } - // Test two ahead offset if canMoveOneAhead { @@ -381,8 +391,7 @@ open class PieceMovementPawn: PieceMovement { if color == .white && fromLocation.y == 1 { twoAheadStride = BoardStride(x: 0, y: 2) - } - else if color == .black && fromLocation.y == 6 { + } else if color == .black && fromLocation.y == 6 { twoAheadStride = BoardStride(x: 0, y: -2) } @@ -406,8 +415,7 @@ open class PieceMovementPawn: PieceMovement { if color == .white { diagonalStrides.append( BoardStride(x: -1, y: 1) ) diagonalStrides.append( BoardStride(x: 1, y: 1) ) - } - else{ + } else { diagonalStrides.append( BoardStride(x: -1, y: -1) ) diagonalStrides.append( BoardStride(x: 1, y: -1) ) } @@ -455,20 +463,19 @@ open class PieceMovementPawn: PieceMovement { } } - // MARK: - PieceMovementKing open class PieceMovementKing: PieceMovement { let offsets: [(x: Int, y: Int)] = [ - (0,1), // North - (1,1), // North-East - (1,0), // East - (1,-1), // South-East - (0,-1), // South - (-1,-1), // South-West - (-1,0), // West - (-1,1) // North- West + (0, 1), // North + (1, 1), // North-East + (1, 0), // East + (1, -1), // South-East + (0, -1), // South + (-1, -1), // South-West + (-1, 0), // West + (-1, 1) // North- West ] override func isMovementPossible(fromLocation: BoardLocation, toLocation: BoardLocation, board: Board) -> Bool { @@ -486,7 +493,10 @@ open class PieceMovementKing: PieceMovement { if toLocation == offsetLocation && offsetLocation.isInBounds() - && canPieceOccupySquare(pieceLocation: fromLocation, xOffset: offset.x, yOffset: offset.y, board: board) { + && canPieceOccupySquare(pieceLocation: fromLocation, + xOffset: offset.x, + yOffset: offset.y, + board: board) { return true } } diff --git a/SwiftChess/Source/Player.swift b/SwiftChess/Source/Player.swift index 77e2c6c..4ab8535 100644 --- a/SwiftChess/Source/Player.swift +++ b/SwiftChess/Source/Player.swift @@ -18,9 +18,9 @@ open class Player { weak var game: Game! weak var delegate: PlayerDelegate? - public func occupiesSquareAt(location: BoardLocation) -> Bool{ + public func occupiesSquareAt(location: BoardLocation) -> Bool { - if let piece = self.game.board.getPiece(at: location){ + if let piece = self.game.board.getPiece(at: location) { if piece.color == self.color { return true } @@ -33,7 +33,7 @@ open class Player { return canMovePieceWithError(fromLocation: fromLocation, toLocation: toLocation).result } - public enum MoveError : Error { + public enum MoveError: Error { case notThisPlayersTurn case movingToSameLocation case noPieceToMove @@ -44,7 +44,8 @@ open class Player { case gameIsNotInProgress } - public func canMovePieceWithError(fromLocation: BoardLocation, toLocation: BoardLocation) -> (result: Bool, error: MoveError?) { + public func canMovePieceWithError(fromLocation: BoardLocation, toLocation: BoardLocation) -> + (result: Bool, error: MoveError?) { // We can't move to our current location if fromLocation == toLocation { @@ -83,7 +84,4 @@ open class Player { return (true, nil) } - - - } diff --git a/SwiftChess/SwiftChess.xcodeproj/project.pbxproj b/SwiftChess/SwiftChess.xcodeproj/project.pbxproj index 9adf856..643ca89 100644 --- a/SwiftChess/SwiftChess.xcodeproj/project.pbxproj +++ b/SwiftChess/SwiftChess.xcodeproj/project.pbxproj @@ -211,6 +211,7 @@ 67A9C9DA1DE64CD200510FB8 /* Frameworks */, 67A9C9DB1DE64CD200510FB8 /* Headers */, 67A9C9DC1DE64CD200510FB8 /* Resources */, + 67B663191F9939A4004621B2 /* Swift Lint */, ); buildRules = ( ); @@ -296,6 +297,23 @@ }; /* End PBXResourcesBuildPhase section */ +/* Begin PBXShellScriptBuildPhase section */ + 67B663191F9939A4004621B2 /* Swift Lint */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + name = "Swift Lint"; + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "if which swiftlint >/dev/null; then\nswiftlint\nelse\necho \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi"; + }; +/* End PBXShellScriptBuildPhase section */ + /* Begin PBXSourcesBuildPhase section */ 67A9C9D91DE64CD200510FB8 /* Sources */ = { isa = PBXSourcesBuildPhase;