🚧 need to solve my test bug

master
Adam BONAFOS 3 months ago
parent 082ee3e222
commit 552c8b2c87

@ -5,6 +5,6 @@
location = "group:Model"> location = "group:Model">
</FileRef> </FileRef>
<FileRef <FileRef
location = "group:Connect4_CLI/Connect4_CLI.xcodeproj"> location = "group:../Connect4_CLI/Connect4_CLI.xcodeproj">
</FileRef> </FileRef>
</Workspace> </Workspace>

@ -7,12 +7,11 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
90BAEE022D34FAF100C6B480 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90BAEE012D34FAF100C6B480 /* main.swift */; }; 90BAEDC82D34F75500C6B480 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90BAEDC72D34F75500C6B480 /* main.swift */; };
90BAEE0A2D34FFA900C6B480 /* Model in Frameworks */ = {isa = PBXBuildFile; productRef = 90BAEE092D34FFA900C6B480 /* Model */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */ /* Begin PBXCopyFilesBuildPhase section */
90BAEDFC2D34FAF100C6B480 /* CopyFiles */ = { 90BAEDC22D34F75500C6B480 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase; isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/; dstPath = /usr/share/man/man1/;
@ -24,93 +23,83 @@
/* End PBXCopyFilesBuildPhase section */ /* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
90BAEDFE2D34FAF100C6B480 /* Connect4_CLI */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Connect4_CLI; sourceTree = BUILT_PRODUCTS_DIR; }; 90BAEDC42D34F75500C6B480 /* Connect4_CLI */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Connect4_CLI; sourceTree = BUILT_PRODUCTS_DIR; };
90BAEE012D34FAF100C6B480 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; }; 90BAEDC72D34F75500C6B480 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
90BAEDCE2D34F78600C6B480 /* Model */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Model; path = ../Model; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
90BAEDFB2D34FAF100C6B480 /* Frameworks */ = { 90BAEDC12D34F75500C6B480 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase; isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
90BAEE0A2D34FFA900C6B480 /* Model in Frameworks */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
/* End PBXFrameworksBuildPhase section */ /* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */ /* Begin PBXGroup section */
90BAEDF52D34FAF100C6B480 = { 90BAEDBB2D34F75500C6B480 = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
90BAEE002D34FAF100C6B480 /* Connect4_CLI */, 90BAEDCE2D34F78600C6B480 /* Model */,
90BAEDFF2D34FAF100C6B480 /* Products */, 90BAEDC62D34F75500C6B480 /* Connect4_CLI */,
90BAEE082D34FFA400C6B480 /* Frameworks */, 90BAEDC52D34F75500C6B480 /* Products */,
); );
sourceTree = "<group>"; sourceTree = "<group>";
}; };
90BAEDFF2D34FAF100C6B480 /* Products */ = { 90BAEDC52D34F75500C6B480 /* Products */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
90BAEDFE2D34FAF100C6B480 /* Connect4_CLI */, 90BAEDC42D34F75500C6B480 /* Connect4_CLI */,
); );
name = Products; name = Products;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
90BAEE002D34FAF100C6B480 /* Connect4_CLI */ = { 90BAEDC62D34F75500C6B480 /* Connect4_CLI */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
90BAEE012D34FAF100C6B480 /* main.swift */, 90BAEDC72D34F75500C6B480 /* main.swift */,
); );
path = Connect4_CLI; path = Connect4_CLI;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
90BAEE082D34FFA400C6B480 /* Frameworks */ = {
isa = PBXGroup;
children = (
);
name = Frameworks;
sourceTree = "<group>";
};
/* End PBXGroup section */ /* End PBXGroup section */
/* Begin PBXNativeTarget section */ /* Begin PBXNativeTarget section */
90BAEDFD2D34FAF100C6B480 /* Connect4_CLI */ = { 90BAEDC32D34F75500C6B480 /* Connect4_CLI */ = {
isa = PBXNativeTarget; isa = PBXNativeTarget;
buildConfigurationList = 90BAEE052D34FAF100C6B480 /* Build configuration list for PBXNativeTarget "Connect4_CLI" */; buildConfigurationList = 90BAEDCB2D34F75500C6B480 /* Build configuration list for PBXNativeTarget "Connect4_CLI" */;
buildPhases = ( buildPhases = (
90BAEDFA2D34FAF100C6B480 /* Sources */, 90BAEDC02D34F75500C6B480 /* Sources */,
90BAEDFB2D34FAF100C6B480 /* Frameworks */, 90BAEDC12D34F75500C6B480 /* Frameworks */,
90BAEDFC2D34FAF100C6B480 /* CopyFiles */, 90BAEDC22D34F75500C6B480 /* CopyFiles */,
); );
buildRules = ( buildRules = (
); );
dependencies = ( dependencies = (
); );
name = Connect4_CLI; name = Connect4_CLI;
packageProductDependencies = (
90BAEE092D34FFA900C6B480 /* Model */,
);
productName = Connect4_CLI; productName = Connect4_CLI;
productReference = 90BAEDFE2D34FAF100C6B480 /* Connect4_CLI */; productReference = 90BAEDC42D34F75500C6B480 /* Connect4_CLI */;
productType = "com.apple.product-type.tool"; productType = "com.apple.product-type.tool";
}; };
/* End PBXNativeTarget section */ /* End PBXNativeTarget section */
/* Begin PBXProject section */ /* Begin PBXProject section */
90BAEDF62D34FAF100C6B480 /* Project object */ = { 90BAEDBC2D34F75500C6B480 /* Project object */ = {
isa = PBXProject; isa = PBXProject;
attributes = { attributes = {
BuildIndependentTargetsInParallel = 1; BuildIndependentTargetsInParallel = 1;
LastSwiftUpdateCheck = 1420; LastSwiftUpdateCheck = 1420;
LastUpgradeCheck = 1420; LastUpgradeCheck = 1420;
TargetAttributes = { TargetAttributes = {
90BAEDFD2D34FAF100C6B480 = { 90BAEDC32D34F75500C6B480 = {
CreatedOnToolsVersion = 14.2; CreatedOnToolsVersion = 14.2;
}; };
}; };
}; };
buildConfigurationList = 90BAEDF92D34FAF100C6B480 /* Build configuration list for PBXProject "Connect4_CLI" */; buildConfigurationList = 90BAEDBF2D34F75500C6B480 /* Build configuration list for PBXProject "Connect4_CLI" */;
compatibilityVersion = "Xcode 14.0"; compatibilityVersion = "Xcode 14.0";
developmentRegion = en; developmentRegion = en;
hasScannedForEncodings = 0; hasScannedForEncodings = 0;
@ -118,29 +107,29 @@
en, en,
Base, Base,
); );
mainGroup = 90BAEDF52D34FAF100C6B480; mainGroup = 90BAEDBB2D34F75500C6B480;
productRefGroup = 90BAEDFF2D34FAF100C6B480 /* Products */; productRefGroup = 90BAEDC52D34F75500C6B480 /* Products */;
projectDirPath = ""; projectDirPath = "";
projectRoot = ""; projectRoot = "";
targets = ( targets = (
90BAEDFD2D34FAF100C6B480 /* Connect4_CLI */, 90BAEDC32D34F75500C6B480 /* Connect4_CLI */,
); );
}; };
/* End PBXProject section */ /* End PBXProject section */
/* Begin PBXSourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */
90BAEDFA2D34FAF100C6B480 /* Sources */ = { 90BAEDC02D34F75500C6B480 /* Sources */ = {
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
90BAEE022D34FAF100C6B480 /* main.swift in Sources */, 90BAEDC82D34F75500C6B480 /* main.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
/* End PBXSourcesBuildPhase section */ /* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */ /* Begin XCBuildConfiguration section */
90BAEE032D34FAF100C6B480 /* Debug */ = { 90BAEDC92D34F75500C6B480 /* Debug */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
buildSettings = { buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO; ALWAYS_SEARCH_USER_PATHS = NO;
@ -200,7 +189,7 @@
}; };
name = Debug; name = Debug;
}; };
90BAEE042D34FAF100C6B480 /* Release */ = { 90BAEDCA2D34F75500C6B480 /* Release */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
buildSettings = { buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO; ALWAYS_SEARCH_USER_PATHS = NO;
@ -253,7 +242,7 @@
}; };
name = Release; name = Release;
}; };
90BAEE062D34FAF100C6B480 /* Debug */ = { 90BAEDCC2D34F75500C6B480 /* Debug */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
buildSettings = { buildSettings = {
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
@ -262,7 +251,7 @@
}; };
name = Debug; name = Debug;
}; };
90BAEE072D34FAF100C6B480 /* Release */ = { 90BAEDCD2D34F75500C6B480 /* Release */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
buildSettings = { buildSettings = {
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
@ -274,32 +263,25 @@
/* End XCBuildConfiguration section */ /* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */ /* Begin XCConfigurationList section */
90BAEDF92D34FAF100C6B480 /* Build configuration list for PBXProject "Connect4_CLI" */ = { 90BAEDBF2D34F75500C6B480 /* Build configuration list for PBXProject "Connect4_CLI" */ = {
isa = XCConfigurationList; isa = XCConfigurationList;
buildConfigurations = ( buildConfigurations = (
90BAEE032D34FAF100C6B480 /* Debug */, 90BAEDC92D34F75500C6B480 /* Debug */,
90BAEE042D34FAF100C6B480 /* Release */, 90BAEDCA2D34F75500C6B480 /* Release */,
); );
defaultConfigurationIsVisible = 0; defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release; defaultConfigurationName = Release;
}; };
90BAEE052D34FAF100C6B480 /* Build configuration list for PBXNativeTarget "Connect4_CLI" */ = { 90BAEDCB2D34F75500C6B480 /* Build configuration list for PBXNativeTarget "Connect4_CLI" */ = {
isa = XCConfigurationList; isa = XCConfigurationList;
buildConfigurations = ( buildConfigurations = (
90BAEE062D34FAF100C6B480 /* Debug */, 90BAEDCC2D34F75500C6B480 /* Debug */,
90BAEE072D34FAF100C6B480 /* Release */, 90BAEDCD2D34F75500C6B480 /* Release */,
); );
defaultConfigurationIsVisible = 0; defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release; defaultConfigurationName = Release;
}; };
/* End XCConfigurationList section */ /* End XCConfigurationList section */
/* Begin XCSwiftPackageProductDependency section */
90BAEE092D34FFA900C6B480 /* Model */ = {
isa = XCSwiftPackageProductDependency;
productName = Model;
};
/* End XCSwiftPackageProductDependency section */
}; };
rootObject = 90BAEDF62D34FAF100C6B480 /* Project object */; rootObject = 90BAEDBC2D34F75500C6B480 /* Project object */;
} }

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:">
</FileRef>
</Workspace>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

@ -10,15 +10,15 @@ import Foundation
public struct Board { public struct Board {
let rowNb: Int let rowNb: Int
let columnNb: Int let columnNb: Int
var grid: [[PlayerId]] var grid: [[Token]]
init(rowsNb: Int = 6, columnsNb: Int = 7) { init(rowsNb: Int = 6, columnsNb: Int = 7) {
rowNb = rowsNb rowNb = rowsNb
columnNb = columnsNb columnNb = columnsNb
grid = Array(repeating: Array(repeating: PlayerId.empty, count: columnsNb), count: rowsNb) grid = Array(repeating: Array(repeating: Token.empty, count: columnsNb), count: rowsNb)
} }
subscript(row: Int, column: Int) -> PlayerId { subscript(row: Int, column: Int) -> Token {
get { get {
guard row >= 0 && row < rowNb && column >= 0 && column < columnNb else { guard row >= 0 && row < rowNb && column >= 0 && column < columnNb else {
fatalError("Out of board") fatalError("Out of board")

@ -7,17 +7,18 @@
import Foundation import Foundation
public struct Connect4Rules : Rules { public struct Connect4Rules : Rules {
let piecesToAlign: Int let piecesToAlign: Int
init(piecesToAlign: Int) { init(piecesToAlign: Int) {
self.piecesToAlign = piecesToAlign self.piecesToAlign = piecesToAlign
} }
func add(board: inout Board, position pos: (Int, Int), playerId: PlayerId) -> Bool { func add(board: inout Board, position pos: (row: Int, column: Int), token: Token) -> Bool {
var currentRowNb = 0; var currentRowNb = 0;
for var row in board.grid { for var row in board.grid {
if row[pos.1] != PlayerId.empty && currentRowNb != 0 { if row[pos.column] != Token.empty && currentRowNb != 0 {
row[currentRowNb - 1] = playerId row[currentRowNb - 1] = token
return true return true
} }
currentRowNb += 1 currentRowNb += 1
@ -25,7 +26,7 @@ public struct Connect4Rules : Rules {
return false return false
} }
func isGameOver(board: Board) -> (Bool, PlayerId) { func isGameOver(board: Board) -> (result: Bool, winner: Token) {
// Vérification générale avec le nombre de pièces à aligner // Vérification générale avec le nombre de pièces à aligner
let maxRow = board.rowNb let maxRow = board.rowNb
let maxCol = board.columnNb let maxCol = board.columnNb
@ -70,12 +71,12 @@ public struct Connect4Rules : Rules {
for row in 0..<maxRow { for row in 0..<maxRow {
for col in 0..<maxCol { for col in 0..<maxCol {
if board[row, col] == .empty { if board[row, col] == .empty {
return (false, PlayerId.empty) return (false, Token.empty)
} }
} }
} }
return (true, PlayerId.empty) // Match nul si toutes les cases sont pleines return (true, Token.empty) // Match nul si toutes les cases sont pleines
} }
private func checkLine(board: Board, start: (Int, Int), direction: (Int, Int)) -> Bool { private func checkLine(board: Board, start: (Int, Int), direction: (Int, Int)) -> Bool {

@ -1,6 +0,0 @@
public struct Model {
public private(set) var text = "Hello, World!"
public init() {
}
}

@ -0,0 +1,10 @@
//
// AIPlayer.swift
//
//
// Created by Adam BONAFOS on 21/01/2025.
//
import Foundation
class AIPlayer : Player {
}

@ -0,0 +1,13 @@
//
// HumanPlayer.swift
//
//
// Created by Adam BONAFOS on 21/01/2025.
//
import Foundation
class HumanPlayer : Player {
override func play() -> (row: Int, column: Int) {
return (0, 0)
}
}

@ -0,0 +1,14 @@
//
// Player.swift
//
//
// Created by Adam BONAFOS on 21/01/2025.
//
import Foundation
class Player {
var name: String = ""
func play() -> (row: Int, column: Int){
fatalError("Must override in subclass")
}
}

@ -0,0 +1,13 @@
//
// RaandomPlayer.swift
//
//
// Created by Adam BONAFOS on 21/01/2025.
//
import Foundation
class RandomPlayer : AIPlayer {
override func play() -> (row: Int, column: Int) {
return (0, 0)
}
}

@ -1,5 +1,5 @@
// //
// File.swift // Rules.swift
// //
// //
// Created by Adam BONAFOS on 14/01/2025. // Created by Adam BONAFOS on 14/01/2025.
@ -7,6 +7,6 @@
import Foundation import Foundation
protocol Rules { protocol Rules {
func add(board: inout Board, position pos: (Int, Int), playerId: PlayerId) -> Bool func add(board: inout Board, position pos: (row: Int, column: Int), token: Token) -> Bool
func isGameOver(board: Board) -> (Bool, PlayerId) func isGameOver(board: Board) -> (result: Bool, winner: Token)
} }

@ -7,15 +7,17 @@
import Foundation import Foundation
public struct TicTacToeRules: Rules { public struct TicTacToeRules: Rules {
func add(board: inout Board, position pos: (Int, Int), playerId: PlayerId) -> Bool { func add(board: inout Board, position pos: (row: Int, column: Int), token: Token) -> Bool {
if board[pos.0, pos.1] == .empty { if board[pos.0, pos.1] == .empty {
board[pos.0, pos.1] = playerId board[pos.0, pos.1] = token
return true return true
} }
return false return false
} }
func isGameOver(board: Board) -> (Bool, PlayerId) { func isGameOver(board: Board) -> (result: Bool, winner: Token) {
<#code#> // Need to be complete
// TODO
return (false, Token.empty)
} }
} }

@ -1,13 +1,13 @@
// //
// File.swift // Token.swift
// //
// //
// Created by Adam BONAFOS on 14/01/2025. // Created by Adam BONAFOS on 14/01/2025.
// //
import Foundation import Foundation
enum PlayerId { enum Token {
case empty case empty
case player1 case red
case player2 case yellow
} }

@ -0,0 +1,98 @@
import XCTest
@testable import Model
final class IsGameOverTest: XCTestCase {
func testHorizontal() {
let rules = Connect4Rules(piecesToAlign: 4)
var board = Board()
board[0, 0] = Token.red
XCTAssertFalse(rules.isGameOver(board: board).result)
board[0, 6] = Token.yellow
XCTAssertFalse(rules.isGameOver(board: board).result)
board[0, 1] = Token.red
XCTAssertFalse(rules.isGameOver(board: board).result)
board[0, 5] = Token.yellow
XCTAssertFalse(rules.isGameOver(board: board).result)
board[0, 2] = Token.red
XCTAssertFalse(rules.isGameOver(board: board).result)
board[0, 4] = Token.yellow
XCTAssertFalse(rules.isGameOver(board: board).result)
board[0, 3] = Token.red
XCTAssertTrue(rules.isGameOver(board: board).result)
XCTAssertEqual(rules.isGameOver(board: board).winner, Token.red)
}
func testFullGridWithoutWin() {
// A REVOIR
let rules = Connect4Rules(piecesToAlign: 4)
var board = Board()
// Remplir une grille complète sans qu'il y ait de victoire
for row in 0..<board.rowNb {
for col in 0..<board.columnNb {
board[row, col] = (row + col) % 2 == 0 ? Token.red : Token.yellow
}
}
// Personne ne gagne, mais la partie est terminée
XCTAssertTrue(rules.isGameOver(board: board).result)
XCTAssertEqual(rules.isGameOver(board: board).winner, Token.empty)
}
func testVertical() {
let rules = Connect4Rules(piecesToAlign: 4)
var board = Board()
// Alternance des joueurs dans une colonne
board[0, 0] = Token.red
board[0, 1] = Token.yellow
board[1, 0] = Token.red
board[1, 1] = Token.yellow
board[2, 0] = Token.red
board[2, 1] = Token.yellow
// Rouge complète la colonne
board[3, 0] = Token.red
// Le rouge gagne
XCTAssertTrue(rules.isGameOver(board: board).result)
XCTAssertEqual(rules.isGameOver(board: board).winner, Token.red)
}
func testDiagonalAscending() {
let rules = Connect4Rules(piecesToAlign: 4)
var board = Board()
// Placer les jetons pour une victoire diagonale ascendante
board[0, 0] = Token.red
board[1, 1] = Token.yellow
board[1, 0] = Token.red
board[2, 2] = Token.yellow
board[2, 1] = Token.red
board[3, 3] = Token.yellow
board[3, 2] = Token.red
board[3, 1] = Token.yellow
board[3, 0] = Token.red
// Rouge gagne ici
XCTAssertTrue(rules.isGameOver(board: board).result)
XCTAssertEqual(rules.isGameOver(board: board).winner, Token.red)
}
func testDiagonalDescending() {
let rules = Connect4Rules(piecesToAlign: 4)
var board = Board()
// Placer les jetons pour une victoire diagonale descendante
board[3, 0] = Token.red
board[2, 1] = Token.yellow
board[2, 0] = Token.red
board[1, 2] = Token.yellow
board[1, 1] = Token.red
board[0, 3] = Token.yellow
board[0, 2] = Token.red
board[0, 1] = Token.yellow
board[0, 0] = Token.red
// Rouge gagne ici
XCTAssertTrue(rules.isGameOver(board: board).result)
XCTAssertEqual(rules.isGameOver(board: board).winner, Token.red)
}
}

@ -1,11 +0,0 @@
import XCTest
@testable import Model
final class ModelTests: XCTestCase {
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(Model().text, "Hello, World!")
}
}
Loading…
Cancel
Save