Change counting signature

Boards
Mathieu GROUSSEAU 3 months ago
parent b8d77cf97a
commit 9d200b7b79

@ -32,32 +32,24 @@ public struct Board {
} }
} }
public func countPieces() -> Int { public func countPieces() -> (a: Int, b: Int) {
var count = 0; var a = 0
var b = 0
for column in grid { for column in grid {
for piece in column { for piece in column {
if piece != nil { switch piece {
count += 1 case .Player1:
a += 1
case .Player2:
b += 1
case nil:
break
} }
} }
} }
return count return (a, b)
}
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
} }
@discardableResult @discardableResult

@ -25,9 +25,9 @@ final class EmptyBoardTests: XCTestCase {
func testEmptyByDefault() throws { func testEmptyByDefault() throws {
XCTAssertNil(board[1, 1]) XCTAssertNil(board[1, 1])
XCTAssertEqual(board.countPieces(), 0) let result = board.countPieces()
XCTAssertEqual(board.countPieces(of_type: .Player1), 0) XCTAssertEqual(result.a, 0)
XCTAssertEqual(board.countPieces(of_type: .Player2), 0) XCTAssertEqual(result.b, 0)
} }
func testSetGet() throws { func testSetGet() throws {
@ -39,9 +39,9 @@ final class EmptyBoardTests: XCTestCase {
func testCounts() throws { func testCounts() throws {
board[1, 2] = .Player2 board[1, 2] = .Player2
XCTAssertEqual(board.countPieces(), 1) let counts = board.countPieces()
XCTAssertEqual(board.countPieces(of_type: .Player1), 0) XCTAssertEqual(counts.a, 0)
XCTAssertEqual(board.countPieces(of_type: .Player2), 1) XCTAssertEqual(counts.b, 1)
} }
func testInsertTopNoPush() { func testInsertTopNoPush() {

Loading…
Cancel
Save