🚨 ♻️ Clean up Board
continuous-integration/drone/push Build is passing Details

main
Alexis Drai 2 years ago
parent 94bcf350fd
commit 82da080d5b

@ -1,6 +1,5 @@
import Foundation import Foundation
//TODO implement Equatable?
public struct Board : CustomStringConvertible { public struct Board : CustomStringConvertible {
public let nbRows: Int public let nbRows: Int
@ -68,11 +67,14 @@ public struct Board : CustomStringConvertible {
public func displayVictory(fromTiles tiles: [(Int, Int)]) -> String { public func displayVictory(fromTiles tiles: [(Int, Int)]) -> String {
var tmpGrid: [[Int?]] = Array(repeating: Array(repeating: nil, count: nbCols), count: nbRows) var tmpGrid: [[Int?]] = Array(repeating: Array(repeating: nil, count: nbCols), count: nbRows)
for pair in tiles { tmpGrid[pair.0][pair.1] = 42 } for pair in tiles {
tmpGrid[pair.0][pair.1] = 42
}
var string = String() var string = String()
for row in tmpGrid { for row in tmpGrid {
for tile in row { string.append(tile == nil ? "" : "$ ") } for tile in row {
string.append(tile == nil ? "" : "$ ")
}
string.append("\n") string.append("\n")
} }
return string return string
@ -80,7 +82,7 @@ public struct Board : CustomStringConvertible {
private mutating func insertChip(from playerId: Int, atRow row: Int, atCol col: Int) -> Bool { private mutating func insertChip(from playerId: Int, atRow row: Int, atCol col: Int) -> Bool {
guard(isWithinBounds(row, and: col)) else { return false } guard(isWithinBounds(row, and: col)) else { return false }
guard((_grid[row][col] == nil)) else { return false } guard(_grid[row][col] == nil) else { return false }
_grid[row][col] = playerId _grid[row][col] = playerId
_nbFree -= 1 _nbFree -= 1
@ -91,22 +93,23 @@ public struct Board : CustomStringConvertible {
guard(0 <= col && col < nbCols) else { return false } guard(0 <= col && col < nbCols) else { return false }
guard(!isFull()) else { return false } guard(!isFull()) else { return false }
if _grid[0][col] != nil { return false } if _grid[0][col] != nil {
return false
}
for i in stride(from: nbRows - 1, through: 0, by: -1) { for i in stride(from: nbRows - 1, through: 0, by: -1) {
if _grid[i][col] == nil { if _grid[i][col] == nil {
return insertChip(from: playerId, atRow: i, atCol: col) return insertChip(from: playerId, atRow: i, atCol: col)
} }
} }
return false return false
} }
// mutating func removeChip(fromRow row: Int, fromCol col: Int) { private mutating func removeChip(fromRow row: Int, fromCol col: Int) -> Bool {
// assert(isWithinBounds(row, and: col)) guard(isWithinBounds(row, and: col)) else { return false }
// _grid[row][col] = nil _grid[row][col] = nil
// _nbFree += 1 _nbFree += 1
// } return true
}
public func isFull() -> Bool { public func isFull() -> Bool {
return _nbFree <= 0 return _nbFree <= 0

Loading…
Cancel
Save