|
|
|
@ -1,11 +1,11 @@
|
|
|
|
|
public struct Board {
|
|
|
|
|
var grid: [[Piece?]]
|
|
|
|
|
private var grid: [[Piece?]]
|
|
|
|
|
|
|
|
|
|
public var width: Int { return grid.count }
|
|
|
|
|
public var height: Int { return grid.first!.count }
|
|
|
|
|
public var columns: Int { return grid.count }
|
|
|
|
|
public var rows: Int { return grid.first!.count }
|
|
|
|
|
|
|
|
|
|
public init?(width: Int, height: Int) {
|
|
|
|
|
self.init(grid: Array(repeating: Array(repeating: nil, count: height), count: width))
|
|
|
|
|
public init?(columns: Int, rows: Int) {
|
|
|
|
|
self.init(grid: Array(repeating: Array(repeating: nil, count: rows), count: columns))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public init?(grid: [[Piece?]]) {
|
|
|
|
@ -17,10 +17,10 @@ public struct Board {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func checkBounds(_ column: Int, _ row: Int) {
|
|
|
|
|
precondition(column >= 0 && column < self.width && row >= 0 && row < self.height, "Coordinates out of bounds")
|
|
|
|
|
precondition(column >= 0 && column < self.columns && row >= 0 && row < self.rows, "Coordinates out of bounds")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subscript(column: Int, row: Int) -> Piece? {
|
|
|
|
|
public subscript(column: Int, row: Int) -> Piece? {
|
|
|
|
|
get {
|
|
|
|
|
checkBounds(column, row)
|
|
|
|
|
return grid[column][row]
|
|
|
|
@ -60,12 +60,13 @@ public struct Board {
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
|
public mutating func insert(piece: Piece, side: Side = .Top, offset: Int, pushing push: Bool = false) -> Bool {
|
|
|
|
|
precondition(offset >= 0, "Offset out of bounds")
|
|
|
|
|
|
|
|
|
|
switch side {
|
|
|
|
|
case .Top:
|
|
|
|
|
precondition(offset < self.width, "Offset (column) out of bounds")
|
|
|
|
|
precondition(offset < self.columns, "Offset (column) out of bounds")
|
|
|
|
|
|
|
|
|
|
if self.grid[offset].first! != nil {
|
|
|
|
|
if (!push) {
|
|
|
|
@ -79,21 +80,21 @@ public struct Board {
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
case .Left:
|
|
|
|
|
precondition(offset < self.height, "Offset (row) out of bounds")
|
|
|
|
|
precondition(offset < self.rows, "Offset (row) out of bounds")
|
|
|
|
|
|
|
|
|
|
if self.grid.first![offset] != nil {
|
|
|
|
|
if (!push) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.shiftLeft(row: offset)
|
|
|
|
|
self.shiftRight(row: offset)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.grid[0][offset] = piece
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
case .Bottom:
|
|
|
|
|
precondition(offset < self.width, "Offset (column) out of bounds")
|
|
|
|
|
precondition(offset < self.columns, "Offset (column) out of bounds")
|
|
|
|
|
|
|
|
|
|
if self.grid[offset].last! != nil {
|
|
|
|
|
if (!push) {
|
|
|
|
@ -108,7 +109,7 @@ public struct Board {
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
case .Right:
|
|
|
|
|
precondition(offset < self.height, "Offset (row) out of bounds")
|
|
|
|
|
precondition(offset < self.rows, "Offset (row) out of bounds")
|
|
|
|
|
|
|
|
|
|
if self.grid.last![offset] != nil {
|
|
|
|
|
if (!push) {
|
|
|
|
@ -118,8 +119,7 @@ public struct Board {
|
|
|
|
|
self.shiftLeft(row: offset)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let lastIndex = self.grid[offset].count - 1
|
|
|
|
|
self.grid[offset][lastIndex] = piece
|
|
|
|
|
self.grid[self.columns - 1][offset] = piece
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|