diff --git a/Connect4/connect4_lib/Sources/connect4_lib/rules/BasicDefaultsNoDiag.swift b/Connect4/connect4_lib/Sources/connect4_lib/rules/BasicDefaultsNoDiag.swift index 1988e74..1917129 100644 --- a/Connect4/connect4_lib/Sources/connect4_lib/rules/BasicDefaultsNoDiag.swift +++ b/Connect4/connect4_lib/Sources/connect4_lib/rules/BasicDefaultsNoDiag.swift @@ -19,8 +19,8 @@ public struct BasicDefaultsNoDiag : IRules { && maxNbRows >= minNbRows && maxNbCols >= minNbCols && nbChipsToAlign >= 2 - && nbChipsToAlign < minNbCols - && nbChipsToAlign < minNbRows + && nbChipsToAlign <= minNbCols + && nbChipsToAlign <= minNbRows ) else { return nil } self.minNbRows = minNbRows @@ -46,28 +46,53 @@ public struct BasicDefaultsNoDiag : IRules { 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 - let nbCols = grid[0].count + // 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 nbRows = grid.count + let nbCols = grid[0].count for i in 0..= nbChipsToAlign && grid[i][j] != nil && grid[i][j] == playerId { + if (nbCols - j) >= nbChipsToAlign && grid[i][j] != nil && grid[i][j] == playerId { + // 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 && grid[i][j] != nil && grid[i][j] == playerId { + if (nbRows - i) >= nbChipsToAlign && grid[i][j] != nil && grid[i][j] == playerId { + // 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.. Int { if let tile = grid[i][j] { if tile == playerId { 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 { - 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) } } } diff --git a/Connect4/connect4_lib/Tests/connect4_libTests/BasicDefaultNoDiagTest.swift b/Connect4/connect4_lib/Tests/connect4_libTests/BasicDefaultNoDiagTest.swift index 2126dde..c0c22d9 100644 --- a/Connect4/connect4_lib/Tests/connect4_libTests/BasicDefaultNoDiagTest.swift +++ b/Connect4/connect4_lib/Tests/connect4_libTests/BasicDefaultNoDiagTest.swift @@ -44,28 +44,32 @@ final class BasicDefaultNoDiagTest: XCTestCase { shouldBeOver: Bool, shouldHaveWinner: Bool, victoryTilesShouldBe: [(Int, Int)]?) { - + if let rules = BasicDefaultsNoDiag(withMinNbRows: 3, withMaxNbRows: 5, withMinNbCols: 3, withMaxNbCols: 5, withNbChipsToAlign: 3) { - - XCTAssertEqual(shouldBeOver, rules.isGameOver(byPlayer: playerId, onGrid: grid).isOver) - XCTAssertEqual(shouldHaveWinner, rules.isGameOver(byPlayer: playerId, onGrid: grid).hasWinner) - XCTAssertFalse(rules.isGameOver(byPlayer: playerId, onGrid: grid).hasWinner && !(rules.isGameOver(byPlayer: playerId, onGrid: grid).isOver)) + + let result = rules.isGameOver(byPlayer: playerId, onGrid: grid) + + XCTAssertEqual(shouldBeOver, result.isOver) + XCTAssertEqual(shouldHaveWinner, result.hasWinner) + XCTAssertFalse(result.hasWinner && !(result.isOver)) 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) - for n in 0..