|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|