diff --git a/4forces.xcworkspace/xcuserdata/yorickgeoffre.xcuserdatad/UserInterfaceState.xcuserstate b/4forces.xcworkspace/xcuserdata/yorickgeoffre.xcuserdatad/UserInterfaceState.xcuserstate index 1a764e8..ef502cb 100644 Binary files a/4forces.xcworkspace/xcuserdata/yorickgeoffre.xcuserdatad/UserInterfaceState.xcuserstate and b/4forces.xcworkspace/xcuserdata/yorickgeoffre.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/4forcesPack/Sources/4forcesPack/Board.swift b/4forcesPack/Sources/4forcesPack/Board.swift index 8daf431..edccba3 100644 --- a/4forcesPack/Sources/4forcesPack/Board.swift +++ b/4forcesPack/Sources/4forcesPack/Board.swift @@ -8,15 +8,25 @@ import Foundation public struct Board { - private let nbRows = 4 - private let nbColumn = 4 + private var nbRows = 4 + private var nbColumn = 4 private var grid: [[Int?]] public init(){ grid = Array(repeating: Array( repeating: nil, count: nbRows), count: nbColumn) } - public init(input: [[Int?]]){ + public init?(nbRows: Int, nbCol: Int){ + guard (nbRows < 1 || nbCol < 1) else { + return nil + } + self.nbColumn = nbCol + self.nbRows = nbRows + grid = Array(repeating: Array( repeating: nil, count: nbRows), count: nbColumn) + } + + public init?(input: [[Int?]]){ + guard input[0][0] != nil else {return nil} //inutile grid = input } @@ -33,8 +43,8 @@ public struct Board { public mutating func insertPeice(id: Int, row: Int) -> Bool{ if(isFull()){ - //return false - }; + return false + } var colToInsert: Int = nbColumn-1 @@ -51,7 +61,7 @@ public struct Board { } print("jeton ok: ",colToInsert) - return insertPeice(id: id, row: row, col: colToInsert) && isFull(); + return insertPeice(id: id, row: row, col: colToInsert) || isFull(); } public func isFull()-> Bool{ @@ -72,4 +82,17 @@ public struct Board { return grid[row][col] } } + + public func toString() -> String{ + var str: String = "" + str += String(repeating: "_", count: (nbColumn*2)+1) + "\n" + for collNum in 0...nbColumn-1{ //parcours de la grille du bas vers le haut + for rowNum in 0...nbRows-1{ + str += String("│"+String(grid[rowNum][collNum]!)) + } + str += String("│\n") + } + str += String(repeating: "¯", count: (nbColumn*2)+1) + "\n" + return str + } } diff --git a/4forcesPack/Tests/4forcesPackTests/BoardTests.swift b/4forcesPack/Tests/4forcesPackTests/BoardTests.swift index 61d97ec..a4e2175 100644 --- a/4forcesPack/Tests/4forcesPackTests/BoardTests.swift +++ b/4forcesPack/Tests/4forcesPackTests/BoardTests.swift @@ -9,10 +9,11 @@ final class _forcesPackTests: XCTestCase { for col in 0...3{ for n in 0...3{ print("----------------------------\niteration", col*4+n) - XCTAssertTrue(b.insertPeice(id: 611, row: col)) + XCTAssertTrue(b.insertPeice(id: 9, row: col)) } } XCTAssertTrue(b.isFull()) + print(b.toString()) } }