Skip to content

Commit 8864d04

Browse files
committed
At blog post five
1 parent 85ca594 commit 8864d04

4 files changed

Lines changed: 54 additions & 55 deletions

File tree

Pentominoes.playground/Contents.swift

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,17 @@
22
import UIKit
33
import XCPlayground
44

5-
public class BoardView: UIView {
6-
private let board: Board
7-
public let gridSize: CGFloat
8-
9-
public init(board: Board, gridSize: CGFloat = 20) {
10-
self.board = board
11-
self.gridSize = gridSize
12-
super.init(frame: board.sizeForGridSize(gridSize))
13-
let gridLayer = CAShapeLayer()
14-
gridLayer.frame = bounds
15-
gridLayer.fillColor = UIColor.whiteColor().CGColor
16-
gridLayer.strokeColor = UIColor.lightGrayColor().CGColor
17-
layer.addSublayer(gridLayer)
18-
gridLayer.path = boardPath()
19-
}
20-
21-
private func boardPath() -> CGPath {
22-
23-
let playableSquares = board.squares().filter { $0.occupied == false }
24-
let rects : [CGRect] = playableSquares.map { square in
25-
let originX = CGFloat(square.column) * gridSize
26-
let originY = CGFloat(square.row) * gridSize
27-
return CGRect(x: originX, y: originY, width: gridSize, height: gridSize)
28-
}
29-
30-
let paths : [UIBezierPath] = rects.map {
31-
return UIBezierPath(rect: $0)
32-
}
33-
let path = UIBezierPath()
34-
paths.forEach { path.appendPath($0) }
35-
return path.CGPath
36-
}
37-
38-
required public init?(coder aDecoder: NSCoder) {
39-
fatalError("init(coder:) has not been implemented")
40-
}
41-
}
42-
43-
let tileView = TileView(tile: Tile(shape: .X))
5+
let tileView = TileView(tile: Tile(shape: .Z), color:.purpleColor(), gridSize: 30)
6+
tileView.lifted = true
447
let board = Board(size: .SixByTen)
45-
let boardView = BoardView(board: board)
8+
let boardView = BoardView(board: board, gridSize: 30)
469
let shapes = (0..<11).map { Shape(rawValue:$0)! }
4710
let tiles = shapes.map { Tile(shape: $0) }
4811
for tile in tiles {
4912
for square in board.squares() {
5013
if board.canPositionTile(tile, atSquare: square) {
5114
board.positionTile(tile, atSquare: square)
52-
let tileView = TileView(tile: tile,color: randomColor())
15+
let tileView = TileView(tile: tile,color: randomColor(), gridSize: boardView.gridSize)
5316
boardView.addSubview(tileView)
5417
tileView.frame.origin = board.pointAtOriginOfSquare(square, gridSize: boardView.gridSize)
5518
break
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import UIKit
2+
3+
public class BoardView: UIView {
4+
private let board: Board
5+
public let gridSize: CGFloat
6+
7+
public init(board: Board, gridSize: CGFloat = 20) {
8+
self.board = board
9+
self.gridSize = gridSize
10+
super.init(frame: board.sizeForGridSize(gridSize))
11+
let gridLayer = CAShapeLayer()
12+
gridLayer.frame = bounds
13+
gridLayer.fillColor = UIColor.whiteColor().CGColor
14+
gridLayer.strokeColor = UIColor.lightGrayColor().CGColor
15+
layer.addSublayer(gridLayer)
16+
gridLayer.path = boardPath()
17+
}
18+
19+
private func boardPath() -> CGPath {
20+
return board.pathForSquares(false, gridSize: gridSize)
21+
}
22+
23+
required public init?(coder aDecoder: NSCoder) {
24+
fatalError("init(coder:) has not been implemented")
25+
}
26+
}

Pentominoes.playground/Sources/PlayingGrid.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,32 @@ extension PlayingGrid {
123123
public func squareWithinGrid(square: Square) -> Bool {
124124
return square.row >= 0 && square.column >= 0 && square.row < rows.count && square.column < rows[square.row].count
125125
}
126-
126+
127127
public func squareOccupied(square: Square) -> Bool {
128128
return self[square.row][square.column]
129129
}
130130
}
131131

132+
extension PlayingGrid {
133+
134+
public func pathForSquares(occupied: Bool, gridSize: CGFloat) -> CGPath {
135+
136+
let squaresForPath = squares().filter { $0.occupied == occupied }
137+
138+
let rects : [CGRect] = squaresForPath.map { square in
139+
let originX = CGFloat(square.column) * gridSize
140+
let originY = CGFloat(square.row) * gridSize
141+
return CGRect(x: originX, y: originY, width: gridSize, height: gridSize)
142+
}
143+
144+
let path : UIBezierPath = rects.reduce(UIBezierPath()) { path, rect in
145+
path.appendPath(UIBezierPath(rect: rect))
146+
return path
147+
}
148+
149+
return path.CGPath
150+
}
151+
}
152+
132153

133154

Pentominoes.playground/Sources/TileView.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,11 @@ public class TileView: UIView {
2626
}
2727

2828
private func tilePath() -> CGPath {
29-
let rects : [CGRect] = tile.occupiedSquares().map { square in
30-
let originX = CGFloat(square.column) * gridSize
31-
let originY = CGFloat(square.row) * gridSize
32-
return CGRect(x: originX, y: originY, width: gridSize, height: gridSize)
33-
}
34-
35-
let paths : [UIBezierPath] = rects.map {
36-
return UIBezierPath(rect: $0)
37-
}
38-
let path = UIBezierPath()
39-
paths.forEach { path.appendPath($0) }
40-
return path.CGPath
29+
return tile.pathForSquares(true, gridSize: gridSize)
4130
}
4231

4332
func rotate(clockwise: Bool) {
44-
UIView.animateWithDuration(0.4, animations: {
33+
UIView.animateWithDuration(0.1, animations: {
4534
self.transform = CGAffineTransformMakeRotation(clockwise ? CGFloat(M_PI_2) : CGFloat(-M_PI_2))
4635
}, completion: { _ in
4736
self.transform = CGAffineTransformIdentity

0 commit comments

Comments
 (0)