Board (pt.2)

main
Alexis Drai 2 years ago
parent c1eb3a69e2
commit 1ebc7b36b9

@ -3,7 +3,7 @@ import connect4_lib
var status: Bool
if var b = Board() {
if var b = Board(withRows: 3, andWithCols: 3) {
print(b)
status = b.insertChip(from: 1, atCol: 1)
print(b)

@ -9,7 +9,7 @@ public struct Board : CustomStringConvertible {
var _grid: [[Int?]]
public init?(withRows nbRows: Int = 6, andWithCols nbCols: Int = 7) {
guard(nbRows > 0 && nbCols > 0) else { return nil }
guard(nbRows >= 3 && nbCols >= 3) else { return nil }
self.nbRows = nbRows
self.nbCols = nbCols
self._nbFree = nbRows * nbCols
@ -22,10 +22,18 @@ public struct Board : CustomStringConvertible {
&& grid.allSatisfy{ $0.count == grid[0].count }) else { return nil }
self.nbRows = grid.count
self.nbCols = grid[0].count
self._nbFree = nbRows * nbCols
self._grid = grid
var nbFree = self.nbRows * self.nbCols
for row in grid {
for tile in row {
if tile != nil {
nbFree -= 1
}
}
}
self._nbFree = nbFree
}
func isWithinBounds(_ row: Int, and col: Int) -> Bool {
return 0 <= row && row < nbRows
&& 0 <= col && col < nbCols

@ -23,10 +23,12 @@ final class BoardTest: XCTestCase {
expect(initBoardWithNbRows: 6, andNbCols: -9, shouldNotBeNil: false)
expect(initBoardWithNbRows: 0, andNbCols: 7, shouldNotBeNil: false)
expect(initBoardWithNbRows: 6, andNbCols: 0, shouldNotBeNil: false)
expect(initBoardWithNbRows: 6, andNbCols: 2, shouldNotBeNil: false)
expect(initBoardWithNbRows: 2, andNbCols: 4, shouldNotBeNil: false)
}
func testInitLoad() throws {
func expect(withBoard orig: [[Int?]], shouldNotBeNil: Bool) {
func expect(withGrid orig: [[Int?]], shouldNotBeNil: Bool) {
let board = Board(withGrid: orig)
if !shouldNotBeNil {
XCTAssertNil(board)
@ -37,11 +39,29 @@ final class BoardTest: XCTestCase {
XCTAssertEqual(orig.count, board?.nbRows)
}
expect(withBoard: [[0, 1, 2], [0, 0, 0], [0, 0, 0]], shouldNotBeNil: true)
expect(withBoard: [], shouldNotBeNil: false)
expect(withBoard: [[], []], shouldNotBeNil: false)
expect(withBoard: [[0, 1], [0, 0]], shouldNotBeNil: false)
expect(withBoard: [[0, 1, 2], [0, 0, 0, 0]], shouldNotBeNil: false)
expect(withGrid: [[0, 1, 2], [0, 0, 0], [0, 0, 0]], shouldNotBeNil: true)
expect(withGrid: [], shouldNotBeNil: false)
expect(withGrid: [[], []], shouldNotBeNil: false)
expect(withGrid: [[0, 1], [0, 0]], shouldNotBeNil: false)
expect(withGrid: [[0, 1, 2], [0, 0, 0, 0]], shouldNotBeNil: false)
}
func testIsFull() throws {
func expect(withGrid orig: [[Int?]], shouldNotBeFull: Bool) {
let board = Board(withGrid: orig)
if shouldNotBeFull {
XCTAssertFalse(board!.isFull())
} else {
XCTAssertTrue(board!.isFull())
}
}
expect(withGrid: [[nil, 1, 2], [nil, nil, nil], [nil, nil, nil]], shouldNotBeFull: true)
expect(withGrid: [[1, 1, 2], [2, 2, 1], [2, 1, 2]], shouldNotBeFull: false)
var board = Board(withGrid: [[1, nil, 2], [2, 2, 1], [2, 1, 2]])
expect(withGrid: board!.grid, shouldNotBeFull: true)
if(board!.insertChip(from: 1, atCol: 1)) {
expect(withGrid: board!.grid, shouldNotBeFull: false)
}
}
}

Loading…
Cancel
Save