Skip to content

Commit fb3c363

Browse files
committed
Blog post four - updating the board model
1 parent b95d9b6 commit fb3c363

File tree

3 files changed

+83
-18
lines changed

3 files changed

+83
-18
lines changed

Pentominoes.playground/Contents.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ let board = Board(size: .FiveByTwelve)
88

99
let tile = Tile(shape: .O)
1010

11-
for boardSquare in board.squares() {
12-
if board.canPositionTile(tile, atSquare: boardSquare) {
13-
print("Can position at \(boardSquare)")
14-
}
15-
}
11+
//tile.rotate(true)
12+
//print("Rotated")
1613

17-
tile.rotate(true)
18-
print("Rotated")
14+
board.positionTile(tile, atSquare: Square(row: 4, column: 6))
15+
board
1916

20-
for boardSquare in board.squares() {
21-
if board.canPositionTile(tile, atSquare: boardSquare) {
22-
print("Can position at \(boardSquare)")
17+
for square in board.squares() {
18+
if let tile = board.tileAtSquare(square) {
19+
print(square)
2320
}
2421
}
2522

26-
27-
28-
29-
23+
let tile2 = Tile(shape: .P)
24+
board.positionTile(tile2, atSquare: Square(row: 4, column: 8))
25+
board
26+
let tile3 = Tile(shape: .T)
27+
board.removeTile(tile3)
28+
board.removeTile(tile)
29+
board
3030

Pentominoes.playground/Sources/Board.swift

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ public class Board: PlayingGrid {
22

33
private (set) public var rows: [[Bool]]
44

5+
private let emptyBoard: [[Bool]]
6+
7+
private struct PlacedTile {
8+
let square: Square
9+
let tile: Tile
10+
}
11+
12+
private var placedTiles = [PlacedTile]() {
13+
didSet {
14+
updateRows()
15+
}
16+
}
17+
518
public struct Size {
619
let height: Int
720
let width: Int
@@ -23,6 +36,7 @@ public class Board: PlayingGrid {
2336
rows += [paddingHorizontal + emptyRow + paddingHorizontal]
2437
}
2538
rows += fullPaddingVertical
39+
emptyBoard = rows
2640
}
2741
}
2842

@@ -31,7 +45,7 @@ extension Board {
3145

3246
for tileSquare in tile.squares() {
3347
let boardSquare = tileSquare.offsetBy(atSquare)
34-
if !squareWithinBoard(boardSquare) {
48+
if !squareWithinGrid(boardSquare) {
3549
return false
3650
}
3751
if tileSquare.occupied == true && squareOccupied(boardSquare) {
@@ -40,4 +54,48 @@ extension Board {
4054
}
4155
return true
4256
}
57+
58+
public func positionTile(tile: Tile, atSquare square: Square) -> Bool {
59+
if !canPositionTile(tile, atSquare: square) {
60+
return false
61+
}
62+
placedTiles.append(PlacedTile(square: square, tile: tile))
63+
return true
64+
}
65+
66+
public func tileAtSquare(square: Square) -> Tile? {
67+
for placedTile in placedTiles {
68+
let locationInTile = square.offsetBy(-placedTile.square)
69+
if placedTile.tile.squareWithinGrid(locationInTile) {
70+
let occupiedSquares = placedTile.tile.squares().filter { $0.occupied == true }
71+
for tileSquare in occupiedSquares {
72+
if tileSquare == locationInTile {
73+
return placedTile.tile
74+
}
75+
}
76+
}
77+
}
78+
return nil
79+
}
80+
81+
public func removeTile(tile: Tile) -> Tile? {
82+
if let index = placedTiles.indexOf( { $0.tile === tile } ){
83+
placedTiles.removeAtIndex(index)
84+
return tile
85+
}
86+
return nil
87+
}
88+
}
89+
90+
extension Board {
91+
private func updateRows() {
92+
rows = emptyBoard
93+
for placedTile in placedTiles {
94+
let occupiedSquares = placedTile.tile.squares().filter { $0.occupied == true }
95+
for tileSquare in occupiedSquares {
96+
let boardLocation = tileSquare.offsetBy(placedTile.square)
97+
rows[boardLocation.row][boardLocation.column] = true
98+
}
99+
}
100+
}
43101
}

Pentominoes.playground/Sources/PlayingGrid.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,17 @@ public struct Square {
3737

3838
func offsetBy(square: Square) -> Square {
3939
return Square(row: self.row + square.row, column: self.column + square.column)
40-
4140
}
4241
}
4342

43+
public prefix func -(square: Square) -> Square {
44+
return Square(row: -square.row, column: -square.column)
45+
}
46+
47+
public func ==(left: Square, right: Square) -> Bool {
48+
return left.row == right.row && left.column == right.column
49+
}
50+
4451
public class GridSquareGenerator: GeneratorType {
4552
var currentRow: Int = 0
4653
var currentColumn: Int = -1
@@ -94,8 +101,8 @@ extension PlayingGrid {
94101
}
95102
}
96103

97-
public func squareWithinBoard(square: Square) -> Bool {
98-
return square.row < rows.count && square.column < rows[square.row].count
104+
public func squareWithinGrid(square: Square) -> Bool {
105+
return square.row >= 0 && square.column >= 0 && square.row < rows.count && square.column < rows[square.row].count
99106
}
100107

101108
public func squareOccupied(square: Square) -> Bool {

0 commit comments

Comments
 (0)