Advertisement
Guest User

Untitled

a guest
Aug 21st, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.17 KB | None | 0 0
  1. public protocol CornerRulesCell {
  2.     associatedtype Puck: Equatable
  3.     associatedtype Swarm: Collection where Swarm.Element: Equatable
  4.     var chips: Swarm { get }
  5.     var puck: Puck? { get }
  6. }
  7.  
  8. public protocol CornerRulesCellSource {
  9.     associatedtype Cell: CornerRulesCell
  10.     var cell: Cell? { get }
  11. }
  12.  
  13.  
  14. struct State<S: Collection, Element: Equatable>: Equatable {
  15.    
  16.     var figures: S
  17.     var puck: Element?
  18.  
  19.     static func == (lhs: State<S, Element>, rhs: State<S, Element>) -> Bool {
  20.         lhs.figures.count == rhs.figures.count && lhs.puck == rhs.puck
  21.     }    
  22. }
  23.  
  24. public struct CornerRules<Model: CornerRulesCellSource> {
  25.    
  26.     var state: State<Model.Cell.Swarm, Model.Cell.Puck>?
  27.    
  28.     public var rulesBroke: (() -> Void)?
  29.    
  30.     public init() {}
  31.     public mutating func handle(model: Model) {
  32.         handle(cell: model.cell)
  33.     }
  34.     mutating func handle<Cell: CornerRulesCell>(cell: Cell?) {
  35.         guard let cell = cell else { return }
  36.         let s = State(figures: cell.chips, puck: cell.puck)
  37.         self.state = s
  38.         guard cell.puck != nil else { return }
  39.         if cell.chips.count == 1 { rulesBroke?() }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement