Permalink
Browse files

At blog post five

  • Loading branch information...
jrturton committed Jun 19, 2016
1 parent 85ca594 commit 8864d0464d301ad98830634f9480c21b8bbabde7
@@ -2,54 +2,17 @@
import UIKit
import XCPlayground
public class BoardView: UIView {
private let board: Board
public let gridSize: CGFloat
public init(board: Board, gridSize: CGFloat = 20) {
self.board = board
self.gridSize = gridSize
super.init(frame: board.sizeForGridSize(gridSize))
let gridLayer = CAShapeLayer()
gridLayer.frame = bounds
gridLayer.fillColor = UIColor.whiteColor().CGColor
gridLayer.strokeColor = UIColor.lightGrayColor().CGColor
layer.addSublayer(gridLayer)
gridLayer.path = boardPath()
}
private func boardPath() -> CGPath {
let playableSquares = board.squares().filter { $0.occupied == false }
let rects : [CGRect] = playableSquares.map { square in
let originX = CGFloat(square.column) * gridSize
let originY = CGFloat(square.row) * gridSize
return CGRect(x: originX, y: originY, width: gridSize, height: gridSize)
}
let paths : [UIBezierPath] = rects.map {
return UIBezierPath(rect: $0)
}
let path = UIBezierPath()
paths.forEach { path.appendPath($0) }
return path.CGPath
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let tileView = TileView(tile: Tile(shape: .X))
let tileView = TileView(tile: Tile(shape: .Z), color:.purpleColor(), gridSize: 30)
tileView.lifted = true
let board = Board(size: .SixByTen)
let boardView = BoardView(board: board)
let boardView = BoardView(board: board, gridSize: 30)
let shapes = (0..<11).map { Shape(rawValue:$0)! }
let tiles = shapes.map { Tile(shape: $0) }
for tile in tiles {
for square in board.squares() {
if board.canPositionTile(tile, atSquare: square) {
board.positionTile(tile, atSquare: square)
let tileView = TileView(tile: tile,color: randomColor())
let tileView = TileView(tile: tile,color: randomColor(), gridSize: boardView.gridSize)
boardView.addSubview(tileView)
tileView.frame.origin = board.pointAtOriginOfSquare(square, gridSize: boardView.gridSize)
break
@@ -0,0 +1,26 @@
import UIKit
public class BoardView: UIView {
private let board: Board
public let gridSize: CGFloat
public init(board: Board, gridSize: CGFloat = 20) {
self.board = board
self.gridSize = gridSize
super.init(frame: board.sizeForGridSize(gridSize))
let gridLayer = CAShapeLayer()
gridLayer.frame = bounds
gridLayer.fillColor = UIColor.whiteColor().CGColor
gridLayer.strokeColor = UIColor.lightGrayColor().CGColor
layer.addSublayer(gridLayer)
gridLayer.path = boardPath()
}
private func boardPath() -> CGPath {
return board.pathForSquares(false, gridSize: gridSize)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
@@ -123,11 +123,32 @@ extension PlayingGrid {
public func squareWithinGrid(square: Square) -> Bool {
return square.row >= 0 && square.column >= 0 && square.row < rows.count && square.column < rows[square.row].count
}
public func squareOccupied(square: Square) -> Bool {
return self[square.row][square.column]
}
}
extension PlayingGrid {
public func pathForSquares(occupied: Bool, gridSize: CGFloat) -> CGPath {
let squaresForPath = squares().filter { $0.occupied == occupied }
let rects : [CGRect] = squaresForPath.map { square in
let originX = CGFloat(square.column) * gridSize
let originY = CGFloat(square.row) * gridSize
return CGRect(x: originX, y: originY, width: gridSize, height: gridSize)
}
let path : UIBezierPath = rects.reduce(UIBezierPath()) { path, rect in
path.appendPath(UIBezierPath(rect: rect))
return path
}
return path.CGPath
}
}
@@ -26,22 +26,11 @@ public class TileView: UIView {
}
private func tilePath() -> CGPath {
let rects : [CGRect] = tile.occupiedSquares().map { square in
let originX = CGFloat(square.column) * gridSize
let originY = CGFloat(square.row) * gridSize
return CGRect(x: originX, y: originY, width: gridSize, height: gridSize)
}
let paths : [UIBezierPath] = rects.map {
return UIBezierPath(rect: $0)
}
let path = UIBezierPath()
paths.forEach { path.appendPath($0) }
return path.CGPath
return tile.pathForSquares(true, gridSize: gridSize)
}
func rotate(clockwise: Bool) {
UIView.animateWithDuration(0.4, animations: {
UIView.animateWithDuration(0.1, animations: {
self.transform = CGAffineTransformMakeRotation(clockwise ? CGFloat(M_PI_2) : CGFloat(-M_PI_2))
}, completion: { _ in
self.transform = CGAffineTransformIdentity

0 comments on commit 8864d04

Please sign in to comment.