From 02bdfb8830eafb63b453323f7baf9094dc26cfe6 Mon Sep 17 00:00:00 2001 From: Alexis Drai Date: Thu, 26 Jan 2023 12:28:14 +0100 Subject: [PATCH] :sparkles: Implement basic rules with no diagonal victories --- .../connect4_lib/BasicDefaultsNoDiag.swift | 46 -------- .../rules/BasicDefaultsNoDiag.swift | 108 ++++++++++++++++++ .../connect4_lib/{ => rules}/IRules.swift | 2 +- .../BasicDefaultNoDiagTest.swift | 12 ++ .../Tests/connect4_libTests/BoardTest.swift | 1 - 5 files changed, 121 insertions(+), 48 deletions(-) delete mode 100644 Connect4/connect4_lib/Sources/connect4_lib/BasicDefaultsNoDiag.swift create mode 100644 Connect4/connect4_lib/Sources/connect4_lib/rules/BasicDefaultsNoDiag.swift rename Connect4/connect4_lib/Sources/connect4_lib/{ => rules}/IRules.swift (59%) create mode 100644 Connect4/connect4_lib/Tests/connect4_libTests/BasicDefaultNoDiagTest.swift diff --git a/Connect4/connect4_lib/Sources/connect4_lib/BasicDefaultsNoDiag.swift b/Connect4/connect4_lib/Sources/connect4_lib/BasicDefaultsNoDiag.swift deleted file mode 100644 index 5060920..0000000 --- a/Connect4/connect4_lib/Sources/connect4_lib/BasicDefaultsNoDiag.swift +++ /dev/null @@ -1,46 +0,0 @@ -import Foundation - -public struct BasicDefaultsNoDiag : IRules { - var minNbRows: Int - - var maxNbRows: Int - - var minNbCols: Int - - var maxNbCols: Int - - var nbChipsToAlign: Int - - func isGameOver(byPlayer playerId: Int, ontGrid grid: [[Int?]]) -> (isOver: Bool, hasWinner: Bool, victoryTiles: [(Int?, Int?)]) { - - var tiles : [(Int?, Int?)] = Array(repeating: (nil, nil), count: nbChipsToAlign) - - - - /* - game over? - winner exists? - return (true, true, tiles) - else - return (true, false, tiles) (all tiles at nil) - else - return (false, false, tiles) (all tiles at nil) - */ - - return (isOver: false, hasWinner: false, victoryTiles: tiles); - } - - init?(withMinNbRows minNbRows: Int = 0, - withMaxNbRows maxNbRows: Int = 15, - withMaxNbCols minNbCols: Int = 0, - withMaxNbCols maxNbCols: Int = 15, - withNbChipsToAlign nbChipsToAlign: Int = 4) { - self.minNbRows = minNbRows - self.maxNbRows = maxNbRows - self.minNbCols = minNbCols - self.maxNbCols = maxNbCols - self.nbChipsToAlign = nbChipsToAlign - } - - -} diff --git a/Connect4/connect4_lib/Sources/connect4_lib/rules/BasicDefaultsNoDiag.swift b/Connect4/connect4_lib/Sources/connect4_lib/rules/BasicDefaultsNoDiag.swift new file mode 100644 index 0000000..0cb8b5b --- /dev/null +++ b/Connect4/connect4_lib/Sources/connect4_lib/rules/BasicDefaultsNoDiag.swift @@ -0,0 +1,108 @@ +import Foundation + +public struct BasicDefaultsNoDiag : IRules { + var minNbRows: Int + var maxNbRows: Int + var minNbCols: Int + var maxNbCols: Int + var nbChipsToAlign: Int + + func isGameOver(byPlayer playerId: Int, onGrid grid: [[Int?]]) -> (isOver: Bool, hasWinner: Bool, victoryTiles: [(Int, Int)]?) { + + // first check if board is full + var isFull = true + for row in grid { + for tile in row { + if tile == nil { + isFull = false + break + } + } + } + + 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 + let nbRows = grid.count + + for i in 0..= 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 { + for x in 0..= 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 { + 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) + } else if direction == directions.down { + return 1 + checkAligned(byPlayer: playerId, onGrid: grid, fromRow: i + 1, andCol: j, going: direction) + } + } + } + return 0 + } + + private enum directions { + case right + case down + } + + init?(withMinNbRows minNbRows: Int = 0, + withMaxNbRows maxNbRows: Int = 15, + withMaxNbCols minNbCols: Int = 0, + withMaxNbCols maxNbCols: Int = 15, + withNbChipsToAlign nbChipsToAlign: Int = 4) { + + guard (nbChipsToAlign >= 2 + && ( + nbChipsToAlign < maxNbCols || nbChipsToAlign < maxNbRows + )) else { return nil } + + self.minNbRows = minNbRows + self.maxNbRows = maxNbRows + self.minNbCols = minNbCols + self.maxNbCols = maxNbCols + self.nbChipsToAlign = nbChipsToAlign + } + + +} diff --git a/Connect4/connect4_lib/Sources/connect4_lib/IRules.swift b/Connect4/connect4_lib/Sources/connect4_lib/rules/IRules.swift similarity index 59% rename from Connect4/connect4_lib/Sources/connect4_lib/IRules.swift rename to Connect4/connect4_lib/Sources/connect4_lib/rules/IRules.swift index 4661aa1..4a5f943 100644 --- a/Connect4/connect4_lib/Sources/connect4_lib/IRules.swift +++ b/Connect4/connect4_lib/Sources/connect4_lib/rules/IRules.swift @@ -6,5 +6,5 @@ protocol IRules { var minNbCols: Int { get } var maxNbCols: Int { get } var nbChipsToAlign: Int { get } - func isGameOver(byPlayer playerId: Int, ontGrid grid: [[Int?]]) -> (isOver: Bool, hasWinner: Bool, victoryTiles: [(Int?, Int?)]) + func isGameOver(byPlayer playerId: Int, onGrid grid: [[Int?]]) -> (isOver: Bool, hasWinner: Bool, victoryTiles: [(Int, Int)]?) } diff --git a/Connect4/connect4_lib/Tests/connect4_libTests/BasicDefaultNoDiagTest.swift b/Connect4/connect4_lib/Tests/connect4_libTests/BasicDefaultNoDiagTest.swift new file mode 100644 index 0000000..a14fc8c --- /dev/null +++ b/Connect4/connect4_lib/Tests/connect4_libTests/BasicDefaultNoDiagTest.swift @@ -0,0 +1,12 @@ +import XCTest +import connect4_lib + +final class BasicDefaultNoDiagTest: XCTestCase { + func testInit() { + // TODO + } + + func testIsGameOver() { + // TODO + } +} diff --git a/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift b/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift index c28ab6d..f37227d 100644 --- a/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift +++ b/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift @@ -1,4 +1,3 @@ -import Foundation import XCTest import connect4_lib