diff --git a/Connect4/connect4_lib/Sources/connect4_lib/Board.swift b/Connect4/connect4_lib/Sources/connect4_lib/Board.swift index a81870a..e26395a 100644 --- a/Connect4/connect4_lib/Sources/connect4_lib/Board.swift +++ b/Connect4/connect4_lib/Sources/connect4_lib/Board.swift @@ -59,7 +59,6 @@ public struct Board : CustomStringConvertible { mutating func insertChip(from playerId: Int, atRow row: Int, atCol col: Int) -> Bool { guard(isWithinBounds(row, and: col)) else { return false } guard((playerId == 1 || playerId == 2)) else { return false } - guard(!isFull()) else { return false } guard((_grid[row][col] == nil)) else { return false } _grid[row][col] = playerId @@ -68,7 +67,8 @@ public struct Board : CustomStringConvertible { } public mutating func insertChip(from playerId: Int, atCol col: Int) -> Bool { - guard(0 < col && col <= nbCols) else { return false } + guard(0 <= col && col < nbCols) else { return false } + guard(!isFull()) else { return false } if _grid[0][col] != nil { return false } diff --git a/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift b/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift index 0420a5c..c28ab6d 100644 --- a/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift +++ b/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift @@ -57,7 +57,7 @@ final class BoardTest: XCTestCase { } } - expect(withGrid: [[nil, 1, 2], [nil, nil, nil], [nil, nil, nil]], shouldNotBeFull: true) + expect(withGrid: [[nil, nil, nil], [nil, nil, nil], [nil, 1, 2]], 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) @@ -65,4 +65,39 @@ final class BoardTest: XCTestCase { expect(withGrid: board!.grid, shouldNotBeFull: false) } } + + func testInsertChip() throws { + func expect(withGrid orig: [[Int?]], playerId: Int, secretTargetRow: Int, targetCol: Int, shouldWork: Bool) { + if shouldWork { + XCTAssertNil(orig[secretTargetRow][targetCol]) + if var board = Board(withGrid: orig) { + XCTAssertTrue(board.insertChip(from: playerId, atCol: targetCol)) + XCTAssertEqual(playerId, board.grid[secretTargetRow][targetCol]) + } + } else { + if var board = Board(withGrid: orig) { + XCTAssertFalse(board.insertChip(from: playerId, atCol: targetCol)) + if 0 <= targetCol && targetCol < orig[0].count { + XCTAssertEqual(orig[secretTargetRow][targetCol], board.grid[secretTargetRow][targetCol]) + } + } + } + } + + // p1, ok + expect(withGrid: [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]], playerId: 1, secretTargetRow: 2, targetCol: 0, shouldWork: true) + // p2, ok + expect(withGrid: [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]], playerId: 2, secretTargetRow: 2, targetCol: 0, shouldWork: true) + // p3, nok + expect(withGrid: [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]], playerId: 3, secretTargetRow: 2, targetCol: 0, shouldWork: false) + // out of bounds left, nok + expect(withGrid: [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]], playerId: 1, secretTargetRow: 2, targetCol: -1, shouldWork: false) + // out of bounds right, nok + expect(withGrid: [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]], playerId: 1, secretTargetRow: 2, targetCol: 3, shouldWork: false) + // grid full, nok + expect(withGrid: [[1, 2, 1], [1, 2, 1], [2, 1, 2]], playerId: 1, secretTargetRow: 0, targetCol: 1, shouldWork: false) + // column full, nok + expect(withGrid: [[nil, nil, 2], [nil, nil, 1], [nil, nil, 2]], playerId: 1, secretTargetRow: 0, targetCol: 2, shouldWork: false) + + } }