Skip to content

Commit d5a3c2c

Browse files
committed
Refactored code according to recommendations from the Swift lint tool. Added a typealias for the board position.
1 parent 23cc921 commit d5a3c2c

File tree

4 files changed

+195
-173
lines changed

4 files changed

+195
-173
lines changed

PGNParser.xcodeproj/project.pbxproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
0C07C2BB1E525E0F00A921A8 /* Frameworks */,
249249
0C07C2BC1E525E0F00A921A8 /* Headers */,
250250
0C07C2BD1E525E0F00A921A8 /* Resources */,
251+
0C654BEB1EA8A35200E00A6D /* ShellScript */,
251252
);
252253
buildRules = (
253254
);
@@ -445,6 +446,22 @@
445446
};
446447
/* End PBXResourcesBuildPhase section */
447448

449+
/* Begin PBXShellScriptBuildPhase section */
450+
0C654BEB1EA8A35200E00A6D /* ShellScript */ = {
451+
isa = PBXShellScriptBuildPhase;
452+
buildActionMask = 2147483647;
453+
files = (
454+
);
455+
inputPaths = (
456+
);
457+
outputPaths = (
458+
);
459+
runOnlyForDeploymentPostprocessing = 0;
460+
shellPath = /bin/sh;
461+
shellScript = "if which swiftlint >/dev/null; then\nswiftlint\nelse\necho \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi";
462+
};
463+
/* End PBXShellScriptBuildPhase section */
464+
448465
/* Begin PBXSourcesBuildPhase section */
449466
0C07C2BA1E525E0F00A921A8 /* Sources */ = {
450467
isa = PBXSourcesBuildPhase;

PGNParser/DraughtsMove.swift

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,25 @@
99
import Foundation
1010
import ParserCombinator
1111

12+
/// The positions on the board conform to an interger number.
13+
typealias BoardPosition = Int
14+
1215
/**
1316
Describes a single move performed by one player.
1417
*/
1518
public struct DraughtsPieceMove {
16-
17-
let from: Int
18-
let to: Int
19-
let hasCapture: Bool
20-
19+
20+
/// The starting position of the piece which should be moved.
21+
let origin: BoardPosition
22+
/// The resting position of the piece after the move is made.
23+
let destination: BoardPosition
24+
/// A flag to indicate if any opponenets pieces were captured during the move.
25+
let isCapture: Bool
26+
2127
}
2228

2329
extension DraughtsPieceMove {
24-
30+
2531
/**
2632
A constructor with the parameters in the order of draughts portable game notation.
2733

@@ -34,12 +40,13 @@ extension DraughtsPieceMove {
3440

3541
- parameter to: The location at which the piece resides after the move.
3642
*/
37-
init(from: Int, hasCapture: Bool, to: Int) {
38-
self.from = from
39-
self.to = to
40-
self.hasCapture = hasCapture
43+
init(origin: BoardPosition, isCapture: Bool, destination: BoardPosition) {
44+
45+
self.origin = origin
46+
self.destination = destination
47+
self.isCapture = isCapture
4148
}
42-
49+
4350
}
4451

4552
/**
@@ -49,14 +56,14 @@ extension DraughtsPieceMove {
4956
A move in technical draughts terms refers to a round of moves by both players.
5057
*/
5158
public struct DraughtsMove {
52-
59+
5360
let white: DraughtsPieceMove
5461
let black: DraughtsPieceMove?
55-
62+
5663
}
5764

5865
extension DraughtsMove {
59-
66+
6067
/**
6168
Parse the input draughts portable game notation string into an array of draughts moves.
6269

@@ -67,8 +74,8 @@ extension DraughtsMove {
6774
- returns: A result of the parse. If successful it will contain the move objects.
6875
*/
6976
public static func parse(fromPortableGameNotation notation: String) -> ParseResult<[DraughtsMove]> {
70-
77+
7178
return DraughtsNotationParser.portableGameNotation().run(withInput: notation)
7279
}
73-
80+
7481
}

PGNParser/DraughtsNotationParser.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Currier
1313
Creates a parser to parse a description a draughts game in portable game notation to DraughtsMove objects.
1414
*/
1515
struct DraughtsNotationParser {
16-
16+
1717
/**
1818
Create a parser which performs the parseing of portable game notation for draughts.
1919

@@ -22,28 +22,28 @@ struct DraughtsNotationParser {
2222
- returns: A parser which creates a list of draughts moves from a string in the correct type.
2323
*/
2424
static func portableGameNotation() -> Parser<[DraughtsMove]> {
25-
25+
2626
let singlePieceMove = curry(DraughtsPieceMove.init) <^> integerNumber
2727
<*> lowercaseXIsTrue <|> hyphenIsFalse
2828
<*> integerNumber
29-
29+
3030
let moveRound = numberWithPoint *> twoPlayerTurn(singlePieceMove)
31-
31+
3232
return moveRound.oneOrMany
3333
}
34-
34+
3535
/// A parser which resolves lowercase x to a boolean true and fails on all other input strings.
36-
private static let lowercaseXIsTrue = character(isEqualTo: "x").map() { _ in return true }
37-
36+
private static let lowercaseXIsTrue = character(isEqualTo: "x").map { _ in return true }
37+
3838
/// A parser which resolves a hyphen to a boolean false and fails on all other input strings.
39-
private static let hyphenIsFalse = character(isEqualTo: "-").map() { _ in return false }
40-
39+
private static let hyphenIsFalse = character(isEqualTo: "-").map { _ in return false }
40+
4141
/// A parser which resolves a full stop to a success and fails on all other input strings.
4242
private static let fullStop = character(isEqualTo: ".")
43-
43+
4444
/// A parser which resolves numbers followed by a fullStop and fails on all other input strings.
4545
private static let numberWithPoint = whitespace.zeroOneOrMany *> digit.oneOrMany *> fullStop
46-
46+
4747
/**
4848
Combine a description of a single player turn into a full move with up to two player turns.
4949

@@ -52,10 +52,10 @@ struct DraughtsNotationParser {
5252
- returns: A parser for a draughts move object describing both playes turn for a move.
5353
*/
5454
private static func twoPlayerTurn(_ turn: Parser<DraughtsPieceMove>) -> Parser<DraughtsMove> {
55-
55+
5656
let singleMove = whitespace.zeroOneOrMany *> turn
57-
57+
5858
return curry(DraughtsMove.init) <^> singleMove <*> singleMove.optional
5959
}
60-
60+
6161
}

0 commit comments

Comments
 (0)