Skip to content

Commit

Permalink
Integrate swift lint
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveBarnegren committed Oct 23, 2017
1 parent fc5a5ed commit 46d9b05
Show file tree
Hide file tree
Showing 22 changed files with 310 additions and 293 deletions.
7 changes: 7 additions & 0 deletions SwiftChess/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
disabled_rules:
- trailing_whitespace
- cyclomatic_complexity
- nesting
- type_name
- force_cast
- identifier_name
42 changes: 17 additions & 25 deletions SwiftChess/Source/AIPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {

Expand All @@ -239,7 +235,6 @@ open class AIPlayer : Player {
return rating
}


func promotePawnsOnBoard(_ board: Board) -> Board {

let pawnsToPromoteLocations = board.getLocationsOfPromotablePawns(color: color)
Expand Down Expand Up @@ -295,7 +290,7 @@ struct Move {
let rating: Double
}

// MARK - BoardRater
// MARK: - BoardRater

internal class BoardRater {

Expand All @@ -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")
}
}



17 changes: 8 additions & 9 deletions SwiftChess/Source/ASCIIBoard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -81,7 +81,6 @@ public struct ASCIIBoard {
return board
}


func pieceFromCharacter(_ character: Character) -> Piece? {

var piece: Piece?
Expand Down Expand Up @@ -118,7 +117,7 @@ public struct ASCIIBoard {
return piece
}

public func indexOfCharacter(_ character: Character) -> Int{
public func indexOfCharacter(_ character: Character) -> Int {

var index: Int?

Expand All @@ -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)
Expand All @@ -156,7 +155,7 @@ public struct ASCIIBoard {

var locations = [BoardLocation]()

indexes.forEach{
indexes.forEach {
let location = BoardLocation(index: $0)
locations.append(location)
}
Expand Down
Loading

0 comments on commit 46d9b05

Please sign in to comment.