Board to String extensions

Boards
Mathieu GROUSSEAU 3 months ago
parent 164a0528f0
commit 9a2495c874

@ -11,11 +11,15 @@ let package = Package(
name: "CustomTypes", name: "CustomTypes",
targets: ["CustomTypes"]), targets: ["CustomTypes"]),
], ],
dependencies: [
.package(path: "../Model")
],
targets: [ targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite. // 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. // Targets can depend on other targets in this package and products from dependencies.
.target( .target(
name: "CustomTypes"), name: "CustomTypes",
dependencies: [ .product(name: "Model", package: "Model") ]),
.testTarget( .testTarget(
name: "CustomTypesTests", name: "CustomTypesTests",
dependencies: ["CustomTypes"]), dependencies: ["CustomTypes"]),

@ -1,2 +0,0 @@
// The Swift Programming Language
// https://docs.swift.org/swift-book

@ -0,0 +1,35 @@
import Model
extension Piece: CustomStringConvertible {
public var description: String {
switch self {
case .PlayerA:
"🔴"
case .PlayerB:
"🟡"
}
}
}
extension Board: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
var str = String()
for row in 0..<self.rows {
for col in 0..<self.columns {
str += self[col, row]?.description ?? " "
}
str += "\n"
}
return str
}
public var debugDescription: String {
let (a, b) = self.countPieces()
return "Board[\(self.columns)x\(self.rows), \(a) A and \(b) B pieces]"
}
}

@ -1,12 +0,0 @@
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
}
}

@ -0,0 +1,21 @@
import XCTest
import Model
@testable import CustomTypes
final class CustomTypesTests: XCTestCase {
func testExample() throws {
guard var board = Board(columns: 4, rows: 4) else {
XCTFail()
return
}
board[1, 1] = .PlayerA
board[2, 1] = .PlayerA
board[2, 2] = .PlayerA
board[1, 2] = .PlayerB
let text: String = board.description
XCTAssertEqual(text, " \n 🔴🔴 \n 🟡🔴 \n \n")
}
}
Loading…
Cancel
Save