🫠 🐛 🧑‍💻 Fix my basic rules

main
Alexis Drai 2 years ago
parent e1d28f0331
commit 038762f648

@ -19,8 +19,8 @@ public struct BasicDefaultsNoDiag : IRules {
&& maxNbRows >= minNbRows && maxNbRows >= minNbRows
&& maxNbCols >= minNbCols && maxNbCols >= minNbCols
&& nbChipsToAlign >= 2 && nbChipsToAlign >= 2
&& nbChipsToAlign < minNbCols && nbChipsToAlign <= minNbCols
&& nbChipsToAlign < minNbRows && nbChipsToAlign <= minNbRows
) else { return nil } ) else { return nil }
self.minNbRows = minNbRows self.minNbRows = minNbRows
@ -46,28 +46,53 @@ public struct BasicDefaultsNoDiag : IRules {
var victoryTiles : [(Int, Int)] = Array(repeating: (0, 0), count: nbChipsToAlign) var victoryTiles : [(Int, Int)] = Array(repeating: (0, 0), count: nbChipsToAlign)
// assuming that the board is square, we could add a check for that in IRules if we want to leave it open for extension // assuming that the board is square -- we could add a check for that in IRules if we wanted to leave that open for extension
let nbCols = grid[0].count
let nbRows = grid.count let nbRows = grid.count
let nbCols = grid[0].count
for i in 0..<nbRows { for i in 0..<nbRows {
for j in 0..<nbCols { for j in 0..<nbCols {
// check if there is room to the right // check if there is room to the right
if nbCols - j >= nbChipsToAlign && grid[i][j] != nil && grid[i][j] == playerId { if (nbCols - j) >= nbChipsToAlign && grid[i][j] != nil && grid[i][j] == playerId {
// check for victory // check for victory
if checkAligned(byPlayer: playerId, onGrid: grid, fromRow: i, andCol: j, going: directions.right) == nbChipsToAlign { let amountAligned = checkAligned(byPlayer: playerId,
onGrid: grid,
fromRow: i,
upToRow: (nbRows - 1),
andCol: j,
upToCol: (nbCols - 1),
going: directions.right)
//print("player \(String(describing: Board.descriptionMapper[playerId])) aligned \(amountAligned) horizontally:")
if amountAligned >= nbChipsToAlign {
for x in 0..<nbChipsToAlign { for x in 0..<nbChipsToAlign {
// row i, origin is (i, j), goes to the right // row i, origin is (i, j), goes to the right
victoryTiles[x] = (i, j + x) victoryTiles[x] = (i, j + x)
} }
return (isOver: true, hasWinner: true, victoryTiles) return (isOver: true, hasWinner: true, victoryTiles)
} }
} }
// check if there is room lower down // check if there is room lower down
if nbRows - i >= nbChipsToAlign && grid[i][j] != nil && grid[i][j] == playerId { if (nbRows - i) >= nbChipsToAlign && grid[i][j] != nil && grid[i][j] == playerId {
// check for victory // check for victory
if checkAligned(byPlayer: playerId, onGrid: grid, fromRow: i, andCol: j, going: directions.down) == nbChipsToAlign { let amountAligned = checkAligned(byPlayer: playerId,
onGrid: grid,
fromRow: i,
upToRow: (nbRows - 1),
andCol: j,
upToCol: (nbCols - 1),
going: directions.down)
//print("player \(String(describing: Board.descriptionMapper[playerId])) aligned \(amountAligned) vertically:")
if amountAligned >= nbChipsToAlign {
for x in 0..<nbChipsToAlign { for x in 0..<nbChipsToAlign {
// column j, origin is (i, j), goes down // column j, origin is (i, j), goes down
victoryTiles[x] = (i + x, j) victoryTiles[x] = (i + x, j)
} }
@ -77,28 +102,37 @@ public struct BasicDefaultsNoDiag : IRules {
} }
} }
/*
winner exists?
return (true, true, tiles)
else is full ?
return (true, false, nil)
else
return (false, false, nil)
*/
return (isFull, hasWinner: false, nil); return (isFull, hasWinner: false, nil);
} }
private func checkAligned(byPlayer playerId: Int, private func checkAligned(byPlayer playerId: Int,
onGrid grid: [[Int?]], onGrid grid: [[Int?]],
fromRow i: Int, fromRow i: Int,
upToRow iMax: Int,
andCol j: Int, andCol j: Int,
upToCol jMax: Int,
going direction: directions) -> Int { going direction: directions) -> Int {
if let tile = grid[i][j] { if let tile = grid[i][j] {
if tile == playerId { if tile == playerId {
if direction == directions.right { if direction == directions.right {
return 1 + checkAligned(byPlayer: playerId, onGrid: grid, fromRow: i, andCol: j + 1, going: direction) if j == jMax { return 1 }
return 1 + checkAligned(byPlayer: playerId,
onGrid: grid,
fromRow: i,
upToRow: iMax,
andCol: j + 1,
upToCol: jMax,
going: direction)
} else if direction == directions.down { } else if direction == directions.down {
return 1 + checkAligned(byPlayer: playerId, onGrid: grid, fromRow: i + 1, andCol: j, going: direction) if i == iMax { return 1 }
return 1 + checkAligned(byPlayer: playerId,
onGrid: grid,
fromRow: i + 1,
upToRow: iMax,
andCol: j,
upToCol: jMax,
going: direction)
} }
} }
} }

@ -47,20 +47,24 @@ final class BasicDefaultNoDiagTest: XCTestCase {
if let rules = BasicDefaultsNoDiag(withMinNbRows: 3, withMaxNbRows: 5, withMinNbCols: 3, withMaxNbCols: 5, withNbChipsToAlign: 3) { if let rules = BasicDefaultsNoDiag(withMinNbRows: 3, withMaxNbRows: 5, withMinNbCols: 3, withMaxNbCols: 5, withNbChipsToAlign: 3) {
XCTAssertEqual(shouldBeOver, rules.isGameOver(byPlayer: playerId, onGrid: grid).isOver) let result = rules.isGameOver(byPlayer: playerId, onGrid: grid)
XCTAssertEqual(shouldHaveWinner, rules.isGameOver(byPlayer: playerId, onGrid: grid).hasWinner)
XCTAssertFalse(rules.isGameOver(byPlayer: playerId, onGrid: grid).hasWinner && !(rules.isGameOver(byPlayer: playerId, onGrid: grid).isOver)) XCTAssertEqual(shouldBeOver, result.isOver)
XCTAssertEqual(shouldHaveWinner, result.hasWinner)
XCTAssertFalse(result.hasWinner && !(result.isOver))
if shouldHaveWinner { if shouldHaveWinner {
XCTAssertTrue(rules.isGameOver(byPlayer: playerId, onGrid: grid).isOver) XCTAssertTrue(result.isOver)
let actualVictoryTiles = rules.isGameOver(byPlayer: playerId, onGrid: grid).victoryTiles let actualVictoryTiles = result.victoryTiles
XCTAssertNotNil(actualVictoryTiles) XCTAssertNotNil(actualVictoryTiles)
for n in 0..<victoryTilesShouldBe!.count { if let actualVictoryTiles {
XCTAssertEqual(victoryTilesShouldBe![n].0, actualVictoryTiles![n].0) for n in 0..<victoryTilesShouldBe!.count {
XCTAssertEqual(victoryTilesShouldBe![n].1, actualVictoryTiles![n].1) XCTAssertEqual(victoryTilesShouldBe![n].0, actualVictoryTiles[n].0)
XCTAssertEqual(victoryTilesShouldBe![n].1, actualVictoryTiles[n].1)
}
} }
} }
} }

Loading…
Cancel
Save