|
| 1 | +public protocol PlayingGrid : CustomStringConvertible, CustomPlaygroundQuickLookable { |
| 2 | + var rows: [[Bool]] { get } |
| 3 | + subscript(row: Int) -> [Bool] { get } |
| 4 | +} |
| 5 | + |
| 6 | +extension Bool { |
| 7 | + var gridCharacter: String { |
| 8 | + return self ? "#" : "_" |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +extension PlayingGrid where Self: protocol<CustomStringConvertible, CustomPlaygroundQuickLookable> { |
| 13 | + public var description: String { |
| 14 | + let descriptions : [String] = rows.map { row in |
| 15 | + row.reduce("") { string, gridValue in |
| 16 | + string + gridValue.gridCharacter |
| 17 | + } |
| 18 | + } |
| 19 | + return descriptions.joinWithSeparator("\n") |
| 20 | + } |
| 21 | + public func customPlaygroundQuickLook() -> PlaygroundQuickLook { |
| 22 | + return PlaygroundQuickLook(reflecting: description) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +public struct Square { |
| 28 | + let row: Int |
| 29 | + let column: Int |
| 30 | + let occupied: Bool? |
| 31 | + |
| 32 | + public init(row: Int, column: Int, occupied: Bool? = nil) { |
| 33 | + self.row = row |
| 34 | + self.column = column |
| 35 | + self.occupied = occupied |
| 36 | + } |
| 37 | + |
| 38 | + func offsetBy(square: Square) -> Square { |
| 39 | + return Square(row: self.row + square.row, column: self.column + square.column) |
| 40 | + |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +public class GridSquareGenerator: GeneratorType { |
| 45 | + var currentRow: Int = 0 |
| 46 | + var currentColumn: Int = -1 |
| 47 | + |
| 48 | + let grid: PlayingGrid |
| 49 | + |
| 50 | + public init(grid: PlayingGrid) { |
| 51 | + self.grid = grid |
| 52 | + } |
| 53 | + |
| 54 | + public func next() -> Square? { |
| 55 | + guard currentRow < grid.rows.count else { return nil } |
| 56 | + |
| 57 | + currentColumn += 1 |
| 58 | + |
| 59 | + if currentColumn == grid[currentRow].count { |
| 60 | + currentColumn = 0 |
| 61 | + currentRow += 1 |
| 62 | + } |
| 63 | + if currentRow < grid.rows.count { |
| 64 | + return Square(row: currentRow, column: currentColumn, occupied: grid[currentRow][currentColumn]) |
| 65 | + } else { |
| 66 | + return nil |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +public class GridSquareSequence: SequenceType { |
| 72 | + let grid: PlayingGrid |
| 73 | + |
| 74 | + public init(grid: PlayingGrid) { |
| 75 | + self.grid = grid |
| 76 | + } |
| 77 | + |
| 78 | + public func generate() -> GridSquareGenerator { |
| 79 | + return GridSquareGenerator(grid: grid) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +extension PlayingGrid { |
| 84 | + public func squares() -> GridSquareSequence { |
| 85 | + return GridSquareSequence(grid: self) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +extension PlayingGrid { |
| 91 | + public subscript(row: Int) -> [Bool] { |
| 92 | + get { |
| 93 | + return rows[row] |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public func squareWithinBoard(square: Square) -> Bool { |
| 98 | + return square.row < rows.count && square.column < rows[square.row].count |
| 99 | + } |
| 100 | + |
| 101 | + public func squareOccupied(square: Square) -> Bool { |
| 102 | + return self[square.row][square.column] |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | + |
0 commit comments