parent
0b27a3ff67
commit
55cedeb7b0
@ -0,0 +1,8 @@
|
||||
.DS_Store
|
||||
/.build
|
||||
/Packages
|
||||
xcuserdata/
|
||||
DerivedData/
|
||||
.swiftpm/configuration/registries.json
|
||||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
||||
.netrc
|
@ -0,0 +1,23 @@
|
||||
// swift-tools-version: 5.10
|
||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "CustomTypes",
|
||||
products: [
|
||||
// Products define the executables and libraries a package produces, making them visible to other packages.
|
||||
.library(
|
||||
name: "CustomTypes",
|
||||
targets: ["CustomTypes"]),
|
||||
],
|
||||
targets: [
|
||||
// Targets are the basic building blocks of a package, defining a module or a test suite.
|
||||
// Targets can depend on other targets in this package and products from dependencies.
|
||||
.target(
|
||||
name: "CustomTypes"),
|
||||
.testTarget(
|
||||
name: "CustomTypesTests",
|
||||
dependencies: ["CustomTypes"]),
|
||||
]
|
||||
)
|
@ -0,0 +1,2 @@
|
||||
// The Swift Programming Language
|
||||
// https://docs.swift.org/swift-book
|
@ -0,0 +1,12 @@
|
||||
import XCTest
|
||||
@testable import CustomTypes
|
||||
|
||||
final class CustomTypesTests: XCTestCase {
|
||||
func testExample() throws {
|
||||
// XCTest Documentation
|
||||
// https://developer.apple.com/documentation/xctest
|
||||
|
||||
// Defining Test Cases and Test Methods
|
||||
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
|
||||
}
|
||||
}
|
@ -1,14 +1,151 @@
|
||||
public struct Board {
|
||||
var grid: [[Piece?]]
|
||||
|
||||
public init(width: Int, height: Int) throws {
|
||||
// guard width > 0, height > 0 else {
|
||||
// // TODO: Swift std error?
|
||||
// }
|
||||
public var width: Int { return grid.count }
|
||||
public var height: 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(grid: [[Piece?]]) {
|
||||
public init?(grid: [[Piece?]]) {
|
||||
guard !grid.isEmpty, !grid.first!.isEmpty else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.grid = grid
|
||||
}
|
||||
|
||||
private func checkBounds(_ column: Int, _ row: Int) {
|
||||
precondition(column >= 0 && column < self.width && row >= 0 && row < self.height, "Coordinates out of bounds")
|
||||
}
|
||||
|
||||
subscript(column: Int, row: Int) -> Piece? {
|
||||
get {
|
||||
checkBounds(column, row)
|
||||
return grid[column][row]
|
||||
}
|
||||
|
||||
set {
|
||||
checkBounds(column, row)
|
||||
grid[column][row] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
public func countPieces() -> Int {
|
||||
var count = 0;
|
||||
|
||||
for column in grid {
|
||||
for piece in column {
|
||||
if piece != nil {
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
public func countPieces(of_type type: Piece) -> Int {
|
||||
var count = 0
|
||||
|
||||
for column in grid {
|
||||
for piece in column {
|
||||
if piece == type {
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
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")
|
||||
|
||||
if self.grid[offset].first! != nil {
|
||||
if (!push) {
|
||||
return false
|
||||
}
|
||||
|
||||
Board.shiftDown(column: &self.grid[offset])
|
||||
}
|
||||
|
||||
self.grid[offset][0] = piece
|
||||
|
||||
return true
|
||||
case .Left:
|
||||
precondition(offset < self.height, "Offset (row) out of bounds")
|
||||
|
||||
if self.grid.first![offset] != nil {
|
||||
if (!push) {
|
||||
return false
|
||||
}
|
||||
|
||||
self.shiftLeft(row: offset)
|
||||
}
|
||||
|
||||
self.grid[0][offset] = piece
|
||||
|
||||
return true
|
||||
case .Bottom:
|
||||
precondition(offset < self.width, "Offset (column) out of bounds")
|
||||
|
||||
if self.grid[offset].last! != nil {
|
||||
if (!push) {
|
||||
return false
|
||||
}
|
||||
|
||||
Board.shiftUp(column: &self.grid[offset])
|
||||
}
|
||||
|
||||
let lastIndex = self.grid[offset].count - 1
|
||||
self.grid[offset][lastIndex] = piece
|
||||
|
||||
return true
|
||||
case .Right:
|
||||
precondition(offset < self.height, "Offset (row) out of bounds")
|
||||
|
||||
if self.grid.last![offset] != nil {
|
||||
if (!push) {
|
||||
return false
|
||||
}
|
||||
|
||||
self.shiftLeft(row: offset)
|
||||
}
|
||||
|
||||
let lastIndex = self.grid[offset].count - 1
|
||||
self.grid[offset][lastIndex] = piece
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
private static func shiftDown(column: inout [Piece?]) {
|
||||
for i in (1..<column.count).reversed() {
|
||||
column[i] = column[i - 1]
|
||||
}
|
||||
}
|
||||
|
||||
private static func shiftUp(column: inout [Piece?]) {
|
||||
for i in 0..<(column.count - 1) {
|
||||
column[i] = column[i + 1]
|
||||
}
|
||||
}
|
||||
|
||||
private mutating func shiftLeft(row: Int) {
|
||||
for i in 0..<(grid.count - 1) {
|
||||
self.grid[i][row] = self.grid[i + 1][row]
|
||||
}
|
||||
}
|
||||
|
||||
private mutating func shiftRight(row: Int) {
|
||||
for i in 1..<grid.count {
|
||||
self.grid[i - 1][row] = self.grid[i][row]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,3 @@
|
||||
public enum Side {
|
||||
case Top, Left, Bottom, Right
|
||||
}
|
Loading…
Reference in new issue