Terms unification & copypaste bugfix

Boards
Mathieu GROUSSEAU 1 month ago
parent b2e799496a
commit b8d77cf97a

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

Loading…
Cancel
Save