This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
forked from refactoring-challenge/reversi-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggestion-By: @koher https://discordapp.com/channels/291054398077927425/499393715140558881/705257491973275750
- Loading branch information
Showing
5 changed files
with
80 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
struct DirectedDistance: Hashable { | ||
let direction: Direction | ||
let distance: Distance | ||
|
||
|
||
var next: DirectedDistance? { | ||
guard let nextDistance = self.distance.next else { | ||
return nil | ||
} | ||
return DirectedDistance(direction: self.direction, distance: nextDistance) | ||
} | ||
|
||
|
||
var prev: DirectedDistance? { | ||
guard let prevDistance = self.distance.prev else { | ||
return nil | ||
} | ||
return DirectedDistance(direction: self.direction, distance: prevDistance) | ||
} | ||
} | ||
|
||
|
||
extension DirectedDistance: CustomDebugStringConvertible { | ||
public var debugDescription: String { | ||
"direction=\(self.direction), distance=\(self.distance)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,46 @@ | ||
struct Line: Hashable { | ||
let start: Coordinate | ||
let end: Coordinate | ||
let direction: Direction | ||
let distance: Distance | ||
let directedDistance: DirectedDistance | ||
|
||
|
||
init(start: Coordinate, unsafeEnd: Coordinate, direction: Direction, distance: Distance) { | ||
init(start: Coordinate, unsafeEnd: Coordinate, directedDistance: DirectedDistance) { | ||
self.start = start | ||
self.end = unsafeEnd | ||
self.direction = direction | ||
self.distance = distance | ||
self.directedDistance = directedDistance | ||
} | ||
|
||
|
||
init?(start: Coordinate, direction: Direction, distance: Distance) { | ||
// NOTE: Be nil if the X is out of boards. | ||
let unsafeX: CoordinateX? | ||
switch direction { | ||
case .top, .bottom: | ||
unsafeX = start.x | ||
case .left, .topLeft, .bottomLeft: | ||
unsafeX = CoordinateX(rawValue: start.x.rawValue - distance.rawValue) | ||
case .right, .topRight, .bottomRight: | ||
unsafeX = CoordinateX(rawValue: start.x.rawValue + distance.rawValue) | ||
} | ||
|
||
// NOTE: Be nil if the Y is out of boards. | ||
let unsafeY: CoordinateY? | ||
switch direction { | ||
case .left, .right: | ||
unsafeY = start.y | ||
case .top, .topLeft, .topRight: | ||
unsafeY = CoordinateY(rawValue: start.y.rawValue - distance.rawValue) | ||
case .bottom, .bottomLeft, .bottomRight: | ||
unsafeY = CoordinateY(rawValue: start.y.rawValue + distance.rawValue) | ||
} | ||
|
||
switch (unsafeX, unsafeY) { | ||
case (.none, _), (_, .none): | ||
init?(start: Coordinate, directedDistance: DirectedDistance) { | ||
guard let end = start.moved(to: directedDistance) else { | ||
return nil | ||
case (.some(let x), .some(let y)): | ||
self.start = start | ||
self.end = Coordinate(x: x, y: y) | ||
self.direction = direction | ||
self.distance = distance | ||
} | ||
self.start = start | ||
self.end = end | ||
self.directedDistance = directedDistance | ||
} | ||
|
||
|
||
var shortened: Line? { | ||
guard let prevDistance = self.distance.prev else { | ||
guard let prevDirectedDistance = self.directedDistance.prev else { | ||
return nil | ||
} | ||
return Line(start: start, direction: self.direction, distance: prevDistance) | ||
return Line(start: start, directedDistance: prevDirectedDistance) | ||
} | ||
|
||
|
||
var grown: Line? { | ||
guard let nextDistance = self.distance.next else { | ||
guard let nextDirectedDistance = self.directedDistance.next else { | ||
return nil | ||
} | ||
return Line(start: start, direction: self.direction, distance: nextDistance) | ||
return Line(start: start, directedDistance: nextDirectedDistance) | ||
} | ||
} | ||
|
||
|
||
|
||
extension Line: CustomDebugStringConvertible { | ||
public var debugDescription: String { | ||
"{Line: start=\(self.start.debugDescription), end=\(self.end.debugDescription), direction=\(self.direction), distance=\(self.distance)}" | ||
"{Line: start=\(self.start.debugDescription), end=\(self.end.debugDescription), \(self.directedDistance)}" | ||
} | ||
} |