Skip to content

Commit 4ddb5f3

Browse files
author
Xiangyu Sun
committed
lint files with SwiftLint
1 parent 9ac21a6 commit 4ddb5f3

14 files changed

+124
-133
lines changed

Package.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ let package = Package(
99
// Products define the executables and libraries produced by a package, and make them visible to other packages.
1010
.library(
1111
name: "BowlingKit",
12-
targets: ["BowlingKit"]),
12+
targets: ["BowlingKit"]
13+
),
1314
],
1415
dependencies: [
1516
// Dependencies declare other packages that this package depends on.
@@ -20,9 +21,11 @@ let package = Package(
2021
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2122
.target(
2223
name: "BowlingKit",
23-
dependencies: []),
24+
dependencies: []
25+
),
2426
.testTarget(
2527
name: "BowlingKitTests",
26-
dependencies: ["BowlingKit"]),
28+
dependencies: ["BowlingKit"]
29+
),
2730
]
2831
)

Sources/BowlingKit/BowlingBot.swift

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// File.swift
3-
//
3+
//
44
//
55
// Created by 孙翔宇 on 21/06/2019.
66
//
@@ -9,32 +9,32 @@ import Foundation
99

1010
public class BowlingBot {
1111
public private(set) var game: Game
12-
12+
1313
public init() {
1414
game = Game()
1515
}
16-
16+
1717
public func generateFullGame() throws {
1818
if game.isGameover {
1919
game = Game()
2020
}
21-
21+
2222
while !game.isGameover {
2323
try rollNextBall()
2424
}
2525
}
26-
26+
2727
@discardableResult
2828
public func rollNextBall() throws -> UInt {
2929
let pinsDown: UInt
30-
if let lastFrame = game.frames.last, !lastFrame.isCompleted {
31-
pinsDown = UInt.random(in: 0...lastFrame.pinsLeft)
32-
try game.rolledWith(pinsKnockedDown: UInt.random(in: 0...lastFrame.pinsLeft))
30+
if let lastFrame = game.frames.last, !lastFrame.isCompleted {
31+
pinsDown = UInt.random(in: 0 ... lastFrame.pinsLeft)
32+
try game.rolledWith(pinsKnockedDown: UInt.random(in: 0 ... lastFrame.pinsLeft))
3333
} else {
34-
pinsDown = UInt.random(in: 0...10)
35-
try game.rolledWith(pinsKnockedDown: UInt.random(in: 0...10))
34+
pinsDown = UInt.random(in: 0 ... 10)
35+
try game.rolledWith(pinsKnockedDown: UInt.random(in: 0 ... 10))
3636
}
37-
37+
3838
return pinsDown
3939
}
4040
}

Sources/BowlingKit/Frame.swift

+22-23
Original file line numberDiff line numberDiff line change
@@ -9,71 +9,70 @@
99
import Foundation
1010

1111
public final class Frame {
12-
1312
public static let maxiumPinsCount: UInt = 10
1413
public var state: FrameState
15-
14+
1615
public var calcualtedScore: UInt { state.calcualtedScore(self) }
17-
16+
1817
public var isCompleted: Bool { state.isFrameCompleted(self) }
19-
18+
2019
public var isCompletelyScored: Bool { state.canScore(self) }
21-
20+
2221
weak var scoringFrame: Frame?
23-
22+
2423
let isLastFrame: Bool
25-
24+
2625
var pinsLeft: UInt { Frame.maxiumPinsCount - (pinsKnockedDown.first ?? 0) }
27-
26+
2827
private(set) var pinsKnockedDown = [UInt]()
29-
28+
3029
public init(lastFrame: Bool = false) {
31-
self.isLastFrame = lastFrame
32-
self.state = InitialState()
30+
isLastFrame = lastFrame
31+
state = InitialState()
3332
}
34-
33+
3534
public func addPinsKnockedDown(_ count: UInt) { state.addPinsKnockedDown(count, frame: self) }
36-
35+
3736
func addballsKnockedDown(count: UInt) { pinsKnockedDown.append(count) }
38-
37+
3938
func getScoringFramePinsKnockedDown(ballIndex: Int) -> [UInt] {
4039
guard let scoringFrame = scoringFrame, ballIndex != 0 else { return [] }
41-
40+
4241
let allFrames = scoringFrame.pinsKnockedDown + (scoringFrame.scoringFrame?.pinsKnockedDown ?? [])
43-
42+
4443
let ballsKnockDownNeeded = allFrames.count - ballIndex
45-
44+
4645
guard ballsKnockDownNeeded >= 0 else {
4746
return []
4847
}
4948
return Array(allFrames[..<ballIndex])
5049
}
51-
50+
5251
func getFirstBallRolledState(pinsDown: UInt) -> FrameState {
5352
let state = OneBallState()
5453
state.addPinsKnockedDown(pinsDown, frame: self)
5554
return state
5655
}
57-
56+
5857
func getStrikeState(pinsDown: UInt) -> FrameState {
5958
let state: FrameState = isLastFrame ? FinalFrameStrikeState() : StrikeState()
6059
state.addPinsKnockedDown(pinsDown, frame: self)
6160
return state
6261
}
63-
62+
6463
func getSpareState(pinsDown: UInt) -> FrameState {
65-
let state:FrameState = isLastFrame ? FinalFrameSpareState() : SpareState()
64+
let state: FrameState = isLastFrame ? FinalFrameSpareState() : SpareState()
6665
state.addPinsKnockedDown(pinsDown, frame: self)
6766
return state
6867
}
69-
68+
7069
func getMissedState(pinsDown: UInt) -> FrameState {
7170
let state = MissedState()
7271
state.addPinsKnockedDown(pinsDown, frame: self)
7372
return state
7473
}
7574
}
7675

77-
extension Frame : CustomDebugStringConvertible {
76+
extension Frame: CustomDebugStringConvertible {
7877
public var debugDescription: String { pinsKnockedDown.debugDescription }
7978
}

Sources/BowlingKit/FrameState/FrameState.swift

+10-14
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,37 @@ public protocol FrameState {
1919
}
2020

2121
public extension FrameState {
22-
2322
func isFrameCompleted(_ frame: Frame) -> Bool { frame.pinsKnockedDown.count == maximumBallCount }
24-
23+
2524
var maximumBallCount: UInt { 2 }
26-
25+
2726
var ballsRequiredForScoring: UInt { maximumBallCount }
28-
27+
2928
func calcualtedScore(_ frame: Frame) -> UInt { ballsForScoring(frame)?.sum() ?? 0 }
30-
29+
3130
func canScore(_ frame: Frame) -> Bool { ballsForScoring(frame)?.count == ballsRequiredForScoring }
32-
31+
3332
func ballsForScoring(_ frame: Frame) -> [UInt]? { frame.pinsKnockedDown }
3433
}
3534

3635
public protocol CompleteFrameState: FrameState {}
3736
public extension CompleteFrameState {
38-
3937
var ballsRequiredForScoring: UInt { 3 }
40-
38+
4139
func addPinsKnockedDown(_ count: UInt, frame: Frame) {
4240
guard !isFrameCompleted(frame) else { return }
4341
frame.addballsKnockedDown(count: count)
4442
}
45-
43+
4644
func ballsForScoring(_ frame: Frame) -> [UInt]? {
47-
4845
var frames = frame.pinsKnockedDown
49-
46+
5047
let countOfBallsMissing = ballsRequiredForScoring - frames.count
51-
48+
5249
if countOfBallsMissing > 0 {
5350
frames.append(contentsOf: frame.getScoringFramePinsKnockedDown(ballIndex: countOfBallsMissing))
5451
}
55-
52+
5653
return frames
5754
}
5855
}
@@ -67,7 +64,6 @@ public struct StrikeState: CompleteFrameState {
6764
public var maximumBallCount: UInt { 1 }
6865
}
6966

70-
7167
public protocol FinalFrameState: CompleteFrameState {}
7268
public extension FinalFrameState {
7369
func isFrameCompleted(_ frame: Frame) -> Bool { canScore(frame) }

Sources/BowlingKit/FrameState/OneBallState.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
public struct OneBallState: FrameState {
1212
public func addPinsKnockedDown(_ count: UInt, frame: Frame) {
13-
if count == frame.pinsLeft {
13+
if count == frame.pinsLeft {
1414
frame.state = frame.getSpareState(pinsDown: count)
1515
} else {
1616
frame.state = frame.getMissedState(pinsDown: count)

Sources/BowlingKit/Game.swift

+18-19
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,59 @@
88

99
import Foundation
1010

11-
enum GameError : Error {
11+
enum GameError: Error {
1212
case gameFinished
1313
}
1414

1515
public final class Game {
16-
1716
public static let maximumFrameCount: UInt = 10
18-
17+
1918
public private(set) var frames = [Frame]()
20-
21-
public var completelyScoredFames: [Frame] { frames.filter{ $0.isCompletelyScored } }
22-
19+
20+
public var completelyScoredFames: [Frame] { frames.filter { $0.isCompletelyScored } }
21+
2322
public var frameIndexForNextBall: UInt {
2423
let currentFrameDelta: UInt = isNextFrameNew ? 1 : 0
2524
return min(Game.maximumFrameCount, UInt(frames.count) + currentFrameDelta)
2625
}
27-
26+
2827
public var isGameover: Bool { completelyScoredFames.count >= Game.maximumFrameCount }
29-
28+
3029
var isNextFrameNew: Bool { frames.isEmpty || frames.last?.isCompleted == true }
31-
30+
3231
public init() {}
33-
32+
3433
public func rolledWith(pinsKnockedDown: UInt) throws {
3534
guard !isGameover else { throw GameError.gameFinished }
3635

3736
if let newFrame = makeNextFrame() {
3837
frames.append(newFrame)
3938
}
40-
39+
4140
frames.last?.addPinsKnockedDown(pinsKnockedDown)
4241
}
43-
42+
4443
public func rolledWith(pinsKnockedDownSequence: [UInt]) throws {
45-
try pinsKnockedDownSequence.forEach{ try self.rolledWith(pinsKnockedDown: $0) }
44+
try pinsKnockedDownSequence.forEach { try self.rolledWith(pinsKnockedDown: $0) }
4645
}
47-
46+
4847
func makeNextFrame() -> Frame? {
4948
if isNextFrameNew {
50-
let newFrame = Frame(lastFrame: frames.count == Game.maximumFrameCount - 1 )
51-
49+
let newFrame = Frame(lastFrame: frames.count == Game.maximumFrameCount - 1)
50+
5251
if frames.last?.state is StrikeState || frames.last?.state is SpareState {
5352
frames.last?.scoringFrame = newFrame
5453
}
55-
54+
5655
return newFrame
5756
}
58-
57+
5958
return nil
6059
}
6160
}
6261

6362
extension Game: CustomDebugStringConvertible {
6463
public var debugDescription: String {
65-
return frames.compactMap{ $0.pinsKnockedDown }.debugDescription
64+
frames.compactMap { $0.pinsKnockedDown }.debugDescription
6665
}
6766
}

Sources/BowlingKit/Scorer.swift

+11-12
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,44 @@ import Foundation
1010

1111
public final class Scorer {
1212
private(set) var game: Game
13-
13+
1414
/// returns the number of the frame containing the ball about to be rolled
1515
public var frameNumber: String { String(describing: game.frameIndexForNextBall) }
16-
16+
1717
/// returns true when the tenth frame has been
1818
/// scored and false otherwise, and which causes the next roll to start a new
1919
/// game
2020
public var gameIsOver: Bool { game.isGameover }
21-
21+
2222
/// returns the score in the game so far
23-
public var scoreSoFar: String { String(describing: game.frames.lazy.map{ $0.calcualtedScore }.sum()) }
24-
23+
public var scoreSoFar: String { String(describing: game.frames.lazy.map { $0.calcualtedScore }.sum()) }
24+
2525
public init() {
26-
self.game = Game()
26+
game = Game()
2727
}
28-
28+
2929
/// given the number of pins knocked down by a roll of the
3030
/// ball, returns an array whose length is the number of frames completely scored and whose contents are the cumulative scores for those frames
3131
/// - Parameter pinsKnockedDown: pins knocked down by a roll of the
3232
/// ball
3333
@discardableResult
3434
public func rolledWith(pinsKnockedDown: UInt) throws -> [UInt] {
35-
3635
do {
3736
try game.rolledWith(pinsKnockedDown: pinsKnockedDown)
3837
} catch {
3938
game = Game()
4039
try game.rolledWith(pinsKnockedDown: pinsKnockedDown)
4140
}
42-
41+
4342
return cuculativeScores(frames: game.completelyScoredFames)
4443
}
45-
44+
4645
@discardableResult
4746
public func rolledWith(pinsKnockedDownSequence: [UInt]) throws -> [UInt] {
4847
try pinsKnockedDownSequence.compactMap { try self.rolledWith(pinsKnockedDown: $0) }.last ?? []
4948
}
50-
49+
5150
private func cuculativeScores(frames: [Frame]) -> [UInt] {
52-
frames.enumerated().map{ frames[...$0.0].lazy.map{ $0.calcualtedScore }.sum() }
51+
frames.enumerated().map { frames[...$0.0].lazy.map { $0.calcualtedScore }.sum() }
5352
}
5453
}

Sources/BowlingKit/Utilities/UInt.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import Foundation
1010

1111
extension Int {
1212
static func - (left: UInt, right: Int) -> Int {
13-
return Int(left) - right
13+
Int(left) - right
1414
}
1515
}
1616

1717
extension Optional where Wrapped == Int {
1818
static func == (left: Int?, right: UInt) -> Bool {
19-
return left == Int(right)
19+
left == Int(right)
2020
}
2121
}

0 commit comments

Comments
 (0)