Permalink
Browse files

Blog post four - updating the board model

  • Loading branch information...
1 parent b95d9b6 commit fb3c3630019118bed6dae73f83815d71eb38839e @jrturton committed Jun 12, 2016
@@ -8,23 +8,23 @@ let board = Board(size: .FiveByTwelve)
let tile = Tile(shape: .O)
-for boardSquare in board.squares() {
- if board.canPositionTile(tile, atSquare: boardSquare) {
- print("Can position at \(boardSquare)")
- }
-}
+//tile.rotate(true)
+//print("Rotated")
-tile.rotate(true)
-print("Rotated")
+board.positionTile(tile, atSquare: Square(row: 4, column: 6))
+board
-for boardSquare in board.squares() {
- if board.canPositionTile(tile, atSquare: boardSquare) {
- print("Can position at \(boardSquare)")
+for square in board.squares() {
+ if let tile = board.tileAtSquare(square) {
+ print(square)
}
}
-
-
-
-
+let tile2 = Tile(shape: .P)
+board.positionTile(tile2, atSquare: Square(row: 4, column: 8))
+board
+let tile3 = Tile(shape: .T)
+board.removeTile(tile3)
+board.removeTile(tile)
+board
@@ -2,6 +2,19 @@ public class Board: PlayingGrid {
private (set) public var rows: [[Bool]]
+ private let emptyBoard: [[Bool]]
+
+ private struct PlacedTile {
+ let square: Square
+ let tile: Tile
+ }
+
+ private var placedTiles = [PlacedTile]() {
+ didSet {
+ updateRows()
+ }
+ }
+
public struct Size {
let height: Int
let width: Int
@@ -23,6 +36,7 @@ public class Board: PlayingGrid {
rows += [paddingHorizontal + emptyRow + paddingHorizontal]
}
rows += fullPaddingVertical
+ emptyBoard = rows
}
}
@@ -31,7 +45,7 @@ extension Board {
for tileSquare in tile.squares() {
let boardSquare = tileSquare.offsetBy(atSquare)
- if !squareWithinBoard(boardSquare) {
+ if !squareWithinGrid(boardSquare) {
return false
}
if tileSquare.occupied == true && squareOccupied(boardSquare) {
@@ -40,4 +54,48 @@ extension Board {
}
return true
}
+
+ public func positionTile(tile: Tile, atSquare square: Square) -> Bool {
+ if !canPositionTile(tile, atSquare: square) {
+ return false
+ }
+ placedTiles.append(PlacedTile(square: square, tile: tile))
+ return true
+ }
+
+ public func tileAtSquare(square: Square) -> Tile? {
+ for placedTile in placedTiles {
+ let locationInTile = square.offsetBy(-placedTile.square)
+ if placedTile.tile.squareWithinGrid(locationInTile) {
+ let occupiedSquares = placedTile.tile.squares().filter { $0.occupied == true }
+ for tileSquare in occupiedSquares {
+ if tileSquare == locationInTile {
+ return placedTile.tile
+ }
+ }
+ }
+ }
+ return nil
+ }
+
+ public func removeTile(tile: Tile) -> Tile? {
+ if let index = placedTiles.indexOf( { $0.tile === tile } ){
+ placedTiles.removeAtIndex(index)
+ return tile
+ }
+ return nil
+ }
+}
+
+extension Board {
+ private func updateRows() {
+ rows = emptyBoard
+ for placedTile in placedTiles {
+ let occupiedSquares = placedTile.tile.squares().filter { $0.occupied == true }
+ for tileSquare in occupiedSquares {
+ let boardLocation = tileSquare.offsetBy(placedTile.square)
+ rows[boardLocation.row][boardLocation.column] = true
+ }
+ }
+ }
}
@@ -37,10 +37,17 @@ public struct Square {
func offsetBy(square: Square) -> Square {
return Square(row: self.row + square.row, column: self.column + square.column)
-
}
}
+public prefix func -(square: Square) -> Square {
+ return Square(row: -square.row, column: -square.column)
+}
+
+public func ==(left: Square, right: Square) -> Bool {
+ return left.row == right.row && left.column == right.column
+}
+
public class GridSquareGenerator: GeneratorType {
var currentRow: Int = 0
var currentColumn: Int = -1
@@ -94,8 +101,8 @@ extension PlayingGrid {
}
}
- public func squareWithinBoard(square: Square) -> Bool {
- return square.row < rows.count && square.column < rows[square.row].count
+ 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 {

0 comments on commit fb3c363

Please sign in to comment.