Skip to content

Commit

Permalink
Swifty, swifter, the swiftest
Browse files Browse the repository at this point in the history
  • Loading branch information
sochalewski committed Dec 9, 2017
1 parent fdf1f73 commit ac147b5
Show file tree
Hide file tree
Showing 40 changed files with 542 additions and 501 deletions.
12 changes: 6 additions & 6 deletions Example/SwiftChess/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,16 @@ extension GameViewController: BoardViewDelegate {
}

// Select new piece if possible
if player.occupiesSquareAt(location: location) {
if player.occupiesSquare(at: location) {
selectedIndex = index
}

// If there is a selected piece, see if it can move to the new location
if let selectedIndex = selectedIndex {

do {
try player.movePiece(fromLocation: BoardLocation(index: selectedIndex),
toLocation: location)
try player.movePiece(from: BoardLocation(index: selectedIndex),
to: location)

} catch Player.MoveError.pieceUnableToMoveToLocation {
print("Piece is unable to move to this location")
Expand Down Expand Up @@ -309,7 +309,7 @@ extension GameViewController: GameDelegate {
}

func gameDidAddPiece(game: Game) {
// do nothing
// Do nothing
}

func gameDidMovePiece(game: Game, piece: Piece, toLocation: BoardLocation) {
Expand All @@ -320,7 +320,7 @@ extension GameViewController: GameDelegate {

pieceView.location = toLocation

// Animage
// Animate
view.setNeedsLayout()

UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
Expand Down Expand Up @@ -359,7 +359,7 @@ extension GameViewController: GameDelegate {

func gameWonByPlayer(game: Game, player: Player) {

let colorName = player.color.toString()
let colorName = player.color.string

let title = "Checkmate!"
let message = "\(colorName.capitalized) wins!"
Expand Down
10 changes: 0 additions & 10 deletions Example/SwiftChess/MenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ import SwiftChess

class MenuViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Actions

@IBAction func playerVsAIButtonPressed(_ sender: UIButton) {
Expand Down
40 changes: 20 additions & 20 deletions Example/Tests/AIConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class AIConfigurationTests: XCTestCase {
var lowValueConfig = AIConfiguration()
lowValueConfig.boardRaterCountPiecesWeighting = 1
let lowValueRater = BoardRaterCountPieces(configuration: lowValueConfig)
let lowValueRating = lowValueRater.ratingfor(board: board.board, color: .white)
let lowValueRating = lowValueRater.ratingFor(board: board.board, color: .white)

var highValueConfig = AIConfiguration()
highValueConfig.boardRaterCountPiecesWeighting = 2
let highValueRater = BoardRaterCountPieces(configuration: highValueConfig)
let highValueRating = highValueRater.ratingfor(board: board.board, color: .white)
let highValueRating = highValueRater.ratingFor(board: board.board, color: .white)

XCTAssertGreaterThan(highValueRating, lowValueRating)
}
Expand All @@ -63,12 +63,12 @@ class AIConfigurationTests: XCTestCase {
var lowValueConfig = AIConfiguration()
lowValueConfig.boardRaterBoardDominanceWeighting = 1
let lowValueRater = BoardRaterBoardDominance(configuration: lowValueConfig)
let lowValueRating = lowValueRater.ratingfor(board: board.board, color: .white)
let lowValueRating = lowValueRater.ratingFor(board: board.board, color: .white)

var highValueConfig = AIConfiguration()
highValueConfig.boardRaterBoardDominanceWeighting = 2
let highValueRater = BoardRaterBoardDominance(configuration: highValueConfig)
let highValueRating = highValueRater.ratingfor(board: board.board, color: .white)
let highValueRating = highValueRater.ratingFor(board: board.board, color: .white)

XCTAssertGreaterThan(highValueRating, lowValueRating)
}
Expand All @@ -89,12 +89,12 @@ class AIConfigurationTests: XCTestCase {
var lowValueConfig = AIConfiguration()
lowValueConfig.boardRaterCenterOwnershipWeighting = 1
let lowValueRater = BoardRaterCenterOwnership(configuration: lowValueConfig)
let lowValueRating = lowValueRater.ratingfor(board: board.board, color: .white)
let lowValueRating = lowValueRater.ratingFor(board: board.board, color: .white)

var highValueConfig = AIConfiguration()
highValueConfig.boardRaterCenterOwnershipWeighting = 2
let highValueRater = BoardRaterCenterOwnership(configuration: highValueConfig)
let highValueRating = highValueRater.ratingfor(board: board.board, color: .white)
let highValueRating = highValueRater.ratingFor(board: board.board, color: .white)

XCTAssertGreaterThan(highValueRating, lowValueRating)
}
Expand All @@ -115,12 +115,12 @@ class AIConfigurationTests: XCTestCase {
var lowValueConfig = AIConfiguration()
lowValueConfig.boardRaterCenterDominanceWeighting = 1
let lowValueRater = BoardRaterCenterDominance(configuration: lowValueConfig)
let lowValueRating = lowValueRater.ratingfor(board: board.board, color: .white)
let lowValueRating = lowValueRater.ratingFor(board: board.board, color: .white)

var highValueConfig = AIConfiguration()
highValueConfig.boardRaterCenterDominanceWeighting = 2
let highValueRater = BoardRaterCenterDominance(configuration: highValueConfig)
let highValueRating = highValueRater.ratingfor(board: board.board, color: .white)
let highValueRating = highValueRater.ratingFor(board: board.board, color: .white)

XCTAssertGreaterThan(highValueRating, lowValueRating)
}
Expand All @@ -142,12 +142,12 @@ class AIConfigurationTests: XCTestCase {
var lowValueConfig = AIConfiguration()
lowValueConfig.boardRaterThreatenedPiecesWeighting = 1
let lowValueRater = BoardRaterThreatenedPieces(configuration: lowValueConfig)
let lowValueRating = lowValueRater.ratingfor(board: board.board, color: .white)
let lowValueRating = lowValueRater.ratingFor(board: board.board, color: .white)

var highValueConfig = AIConfiguration()
highValueConfig.boardRaterThreatenedPiecesWeighting = 2
let highValueRater = BoardRaterThreatenedPieces(configuration: highValueConfig)
let highValueRating = highValueRater.ratingfor(board: board.board, color: .white)
let highValueRating = highValueRater.ratingFor(board: board.board, color: .white)

XCTAssertGreaterThan(highValueRating, lowValueRating)
}
Expand All @@ -167,12 +167,12 @@ class AIConfigurationTests: XCTestCase {
var lowValueConfig = AIConfiguration()
lowValueConfig.boardRaterThreatenedPiecesOwnPiecesMultiplier = 1
let lowValueRater = BoardRaterThreatenedPieces(configuration: lowValueConfig)
let lowValueRating = lowValueRater.ratingfor(board: board.board, color: .white)
let lowValueRating = lowValueRater.ratingFor(board: board.board, color: .white)

var highValueConfig = AIConfiguration()
highValueConfig.boardRaterThreatenedPiecesOwnPiecesMultiplier = 2
let highValueRater = BoardRaterThreatenedPieces(configuration: highValueConfig)
let highValueRating = highValueRater.ratingfor(board: board.board, color: .white)
let highValueRating = highValueRater.ratingFor(board: board.board, color: .white)

// Result should be more negative for a higher value, as white is under threat
XCTAssertLessThan(highValueRating, lowValueRating)
Expand All @@ -194,12 +194,12 @@ class AIConfigurationTests: XCTestCase {
var lowValueConfig = AIConfiguration()
lowValueConfig.boardRaterPawnProgressionWeighting = 1
let lowValueRater = BoardRaterPawnProgression(configuration: lowValueConfig)
let lowValueRating = lowValueRater.ratingfor(board: board.board, color: .white)
let lowValueRating = lowValueRater.ratingFor(board: board.board, color: .white)

var highValueConfig = AIConfiguration()
highValueConfig.boardRaterPawnProgressionWeighting = 2
let highValueRater = BoardRaterPawnProgression(configuration: highValueConfig)
let highValueRating = highValueRater.ratingfor(board: board.board, color: .white)
let highValueRating = highValueRater.ratingFor(board: board.board, color: .white)

XCTAssertGreaterThan(highValueRating, lowValueRating)
}
Expand All @@ -220,12 +220,12 @@ class AIConfigurationTests: XCTestCase {
var lowValueConfig = AIConfiguration()
lowValueConfig.boardRaterKingSurroundingPossessionWeighting = 1
let lowValueRater = BoardRaterKingSurroundingPossession(configuration: lowValueConfig)
let lowValueRating = lowValueRater.ratingfor(board: board.board, color: .white)
let lowValueRating = lowValueRater.ratingFor(board: board.board, color: .white)

var highValueConfig = AIConfiguration()
highValueConfig.boardRaterKingSurroundingPossessionWeighting = 2
let highValueRater = BoardRaterKingSurroundingPossession(configuration: highValueConfig)
let highValueRating = highValueRater.ratingfor(board: board.board, color: .white)
let highValueRating = highValueRater.ratingFor(board: board.board, color: .white)

XCTAssertGreaterThan(highValueRating, lowValueRating)
}
Expand All @@ -247,12 +247,12 @@ class AIConfigurationTests: XCTestCase {
var lowValueConfig = AIConfiguration()
lowValueConfig.boardRaterCheckMateOpportunityWeighting = 1
let lowValueRater = BoardRaterCheckMateOpportunity(configuration: lowValueConfig)
let lowValueRating = lowValueRater.ratingfor(board: board.board, color: .white)
let lowValueRating = lowValueRater.ratingFor(board: board.board, color: .white)

var highValueConfig = AIConfiguration()
highValueConfig.boardRaterCheckMateOpportunityWeighting = 2
let highValueRater = BoardRaterCheckMateOpportunity(configuration: highValueConfig)
let highValueRating = highValueRater.ratingfor(board: board.board, color: .white)
let highValueRating = highValueRater.ratingFor(board: board.board, color: .white)

XCTAssertGreaterThan(highValueRating, lowValueRating)

Expand All @@ -274,12 +274,12 @@ class AIConfigurationTests: XCTestCase {
var lowValueConfig = AIConfiguration()
lowValueConfig.boardRaterCenterFourOccupationWeighting = 1
let lowValueRater = BoardRaterCenterFourOccupation(configuration: lowValueConfig)
let lowValueRating = lowValueRater.ratingfor(board: board.board, color: .white)
let lowValueRating = lowValueRater.ratingFor(board: board.board, color: .white)

var highValueConfig = AIConfiguration()
highValueConfig.boardRaterCenterFourOccupationWeighting = 2
let highValueRater = BoardRaterCenterFourOccupation(configuration: highValueConfig)
let highValueRating = highValueRater.ratingfor(board: board.board, color: .white)
let highValueRating = highValueRater.ratingFor(board: board.board, color: .white)

XCTAssertGreaterThan(highValueRating, lowValueRating)
}
Expand Down
12 changes: 6 additions & 6 deletions Example/Tests/AIPlayerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class AIPlayerTests: XCTestCase {
fatalError()
}

XCTAssertFalse(player.canAIMovePiece(fromLocation: knightLocation, toLocation: testLocation))
XCTAssertFalse(player.canAIMovePiece(from: knightLocation, to: testLocation))
}

func testKingCannotMoveInToCheck() {
Expand All @@ -88,7 +88,7 @@ class AIPlayerTests: XCTestCase {
fatalError()
}

XCTAssertFalse(player.canAIMovePiece(fromLocation: kingLocation, toLocation: testLocation))
XCTAssertFalse(player.canAIMovePiece(from: kingLocation, to: testLocation))
}

func testPawnCannotPutOwnKingInToCheck() {
Expand All @@ -111,7 +111,7 @@ class AIPlayerTests: XCTestCase {
fatalError()
}

XCTAssertFalse(player.canAIMovePiece(fromLocation: pawnLocation, toLocation: testLocation))
XCTAssertFalse(player.canAIMovePiece(from: pawnLocation, to: testLocation))

}

Expand All @@ -135,7 +135,7 @@ class AIPlayerTests: XCTestCase {
fatalError()
}

XCTAssertFalse(player.canAIMovePiece(fromLocation: queenLocation, toLocation: testLocation))
XCTAssertFalse(player.canAIMovePiece(from: queenLocation, to: testLocation))
}

func testRookCannotPutOwnKingInToCheck() {
Expand All @@ -158,7 +158,7 @@ class AIPlayerTests: XCTestCase {
fatalError()
}

XCTAssertFalse(player.canAIMovePiece(fromLocation: rookLocation, toLocation: testLocation))
XCTAssertFalse(player.canAIMovePiece(from: rookLocation, to: testLocation))
}

func testBishopCannotPutOwnKingInToCheck() {
Expand All @@ -181,6 +181,6 @@ class AIPlayerTests: XCTestCase {
fatalError()
}

XCTAssertFalse(player.canAIMovePiece(fromLocation: bishopLocation, toLocation: testLocation))
XCTAssertFalse(player.canAIMovePiece(from: bishopLocation, to: testLocation))
}
}
4 changes: 2 additions & 2 deletions Example/Tests/BoardLocationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ class BoardLocationTests: XCTestCase {

let opening = FakeOpening()

let whiteMoves = opening.moves(forColor: .white)
let whiteMoves = opening.moves(for: .white)
XCTAssertEqual(whiteMoves.count, expectedWhiteLocations.count)

let blackMoves = opening.moves(forColor: .black)
let blackMoves = opening.moves(for: .black)
XCTAssertEqual(blackMoves.count, expectedBlackLocations.count)

for i in 0..<whiteMoves.count {
Expand Down
16 changes: 8 additions & 8 deletions Example/Tests/BoardRaterBoardDominanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BoardRaterBoardDominanceTests: XCTestCase {
"- - - - - - - -" +
"- - - - - - - -" +
"- - - - - - - -" )
let queenRating = boardRater.ratingfor(board: queenBoard.board, color: .white)
let queenRating = boardRater.ratingFor(board: queenBoard.board, color: .white)

let pawnBoard = ASCIIBoard(pieces: "- - - - - - - -" +
"- - - - - - - -" +
Expand All @@ -45,7 +45,7 @@ class BoardRaterBoardDominanceTests: XCTestCase {
"- - - - - - - -" +
"- - - - - - - -" +
"- - - - - - - -" )
let pawnRating = boardRater.ratingfor(board: pawnBoard.board, color: .white)
let pawnRating = boardRater.ratingFor(board: pawnBoard.board, color: .white)

XCTAssertGreaterThan(queenRating, pawnRating)
}
Expand All @@ -60,7 +60,7 @@ class BoardRaterBoardDominanceTests: XCTestCase {
"- - - - - - - -" +
"P - - - - - - -" +
"Q P - - - - - -" )
let blockedRating = boardRater.ratingfor(board: blockedBoard.board, color: .white)
let blockedRating = boardRater.ratingFor(board: blockedBoard.board, color: .white)

let nonBockedBoard = ASCIIBoard(pieces: "- - - - - - - -" +
"- - - - - - - -" +
Expand All @@ -70,7 +70,7 @@ class BoardRaterBoardDominanceTests: XCTestCase {
"- - - - - - - -" +
"- - - - - - - -" +
"Q - - - - - - -" )
let nonBockedRating = boardRater.ratingfor(board: nonBockedBoard.board, color: .white)
let nonBockedRating = boardRater.ratingFor(board: nonBockedBoard.board, color: .white)

XCTAssertGreaterThan(nonBockedRating, blockedRating)

Expand All @@ -86,7 +86,7 @@ class BoardRaterBoardDominanceTests: XCTestCase {
"- - - - - - - k" +
"- - - - - - - p" +
"Q - - - - - - -" )
let whiteRating = boardRater.ratingfor(board: whiteDominantBoard.board, color: .white)
let whiteRating = boardRater.ratingFor(board: whiteDominantBoard.board, color: .white)

let blackDominantBoard = ASCIIBoard(pieces: "r - - - - - - -" +
"- - - - - - - -" +
Expand All @@ -96,7 +96,7 @@ class BoardRaterBoardDominanceTests: XCTestCase {
"- - - - - - - K" +
"- - - - - - - P" +
"q - - - - - - -" )
let blackRating = boardRater.ratingfor(board: blackDominantBoard.board, color: .white)
let blackRating = boardRater.ratingFor(board: blackDominantBoard.board, color: .white)

XCTAssertGreaterThan(whiteRating, blackRating)
}
Expand All @@ -111,7 +111,7 @@ class BoardRaterBoardDominanceTests: XCTestCase {
"- - - - - - - -" +
"- - - - - - - -" +
"Q - - - - - - -" )
let queenInCornerRating = boardRater.ratingfor(board: queenInCornerBoard.board, color: .white)
let queenInCornerRating = boardRater.ratingFor(board: queenInCornerBoard.board, color: .white)

let queenInCenterBoard = ASCIIBoard(pieces: "- - - - - - - -" +
"- - - - - - - -" +
Expand All @@ -121,7 +121,7 @@ class BoardRaterBoardDominanceTests: XCTestCase {
"- - - - - - - -" +
"- - - - - - - -" +
"- - - - - - - -" )
let queenInCenterRating = boardRater.ratingfor(board: queenInCenterBoard.board, color: .white)
let queenInCenterRating = boardRater.ratingFor(board: queenInCenterBoard.board, color: .white)

XCTAssertGreaterThan(queenInCenterRating, queenInCornerRating)
}
Expand Down
4 changes: 2 additions & 2 deletions Example/Tests/BoardRaterCenterDominanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BoardRaterCenterDominanceTests: XCTestCase {
"- - - - - - - -" +
"- - - - - - - -" +
"- - - - - - - -" )
let centerVisibleRating = boardRater.ratingfor(board: centerVisibleBoard.board, color: .white)
let centerVisibleRating = boardRater.ratingFor(board: centerVisibleBoard.board, color: .white)

let centerObstructedBoard = ASCIIBoard(pieces: "- - - - - - - -" +
"- - - - - - - -" +
Expand All @@ -44,7 +44,7 @@ class BoardRaterCenterDominanceTests: XCTestCase {
"- - - - - - - -" +
"- - - - - - P -" +
"- - - - - - - Q" )
let centerObstructedRating = boardRater.ratingfor(board: centerObstructedBoard.board, color: .white)
let centerObstructedRating = boardRater.ratingFor(board: centerObstructedBoard.board, color: .white)

XCTAssertGreaterThan(centerVisibleRating, centerObstructedRating)
}
Expand Down
Loading

0 comments on commit ac147b5

Please sign in to comment.