Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public protocol CornerRulesCell {
- associatedtype Puck: Equatable
- associatedtype Swarm: Collection where Swarm.Element: Equatable
- var chips: Swarm { get }
- var puck: Puck? { get }
- }
- public protocol CornerRulesCellSource {
- associatedtype Cell: CornerRulesCell
- var cell: Cell? { get }
- }
- struct State<S: Collection, Element: Equatable>: Equatable {
- var figures: S
- var puck: Element?
- static func == (lhs: State<S, Element>, rhs: State<S, Element>) -> Bool {
- lhs.figures.count == rhs.figures.count && lhs.puck == rhs.puck
- }
- }
- public struct CornerRules<Model: CornerRulesCellSource> {
- var state: State<Model.Cell.Swarm, Model.Cell.Puck>?
- public var rulesBroke: (() -> Void)?
- public init() {}
- public mutating func handle(model: Model) {
- handle(cell: model.cell)
- }
- mutating func handle<Cell: CornerRulesCell>(cell: Cell?) {
- guard let cell = cell else { return }
- let s = State(figures: cell.chips, puck: cell.puck)
- self.state = s
- guard cell.puck != nil else { return }
- if cell.chips.count == 1 { rulesBroke?() }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement