From 1ebc7b36b978d503edc1040095622f47ebc411f3 Mon Sep 17 00:00:00 2001 From: Alexis Drai Date: Tue, 24 Jan 2023 15:25:09 +0100 Subject: [PATCH] :white_check_mark: Board (pt.2) --- Connect4/connect4_cli/connect4_cli/main.swift | 2 +- .../Sources/connect4_lib/Board.swift | 14 ++++++-- .../Tests/connect4_libTests/BoardTest.swift | 32 +++++++++++++++---- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/Connect4/connect4_cli/connect4_cli/main.swift b/Connect4/connect4_cli/connect4_cli/main.swift index dd75262..6509ed1 100644 --- a/Connect4/connect4_cli/connect4_cli/main.swift +++ b/Connect4/connect4_cli/connect4_cli/main.swift @@ -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) diff --git a/Connect4/connect4_lib/Sources/connect4_lib/Board.swift b/Connect4/connect4_lib/Sources/connect4_lib/Board.swift index a909ea6..76a1023 100644 --- a/Connect4/connect4_lib/Sources/connect4_lib/Board.swift +++ b/Connect4/connect4_lib/Sources/connect4_lib/Board.swift @@ -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 diff --git a/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift b/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift index af2e962..9ba40d6 100644 --- a/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift +++ b/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift @@ -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) + } + } }