🚧 need to solve my test bug

master
Adam BONAFOS 1 week ago
parent 082ee3e222
commit 552c8b2c87

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

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

@ -7,17 +7,18 @@
import Foundation
public struct Connect4Rules : Rules {
let piecesToAlign: Int
init(piecesToAlign: Int) {
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;
for var row in board.grid {
if row[pos.1] != PlayerId.empty && currentRowNb != 0 {
row[currentRowNb - 1] = playerId
if row[pos.column] != Token.empty && currentRowNb != 0 {
row[currentRowNb - 1] = token
return true
}
currentRowNb += 1
@ -25,7 +26,7 @@ public struct Connect4Rules : Rules {
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
let maxRow = board.rowNb
let maxCol = board.columnNb
@ -70,12 +71,12 @@ public struct Connect4Rules : Rules {
for row in 0..<maxRow {
for col in 0..<maxCol {
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 {

@ -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.
@ -7,6 +7,6 @@
import Foundation
protocol Rules {
func add(board: inout Board, position pos: (Int, Int), playerId: PlayerId) -> Bool
func isGameOver(board: Board) -> (Bool, PlayerId)
func add(board: inout Board, position pos: (row: Int, column: Int), token: Token) -> Bool
func isGameOver(board: Board) -> (result: Bool, winner: Token)
}

@ -7,15 +7,17 @@
import Foundation
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 {
board[pos.0, pos.1] = playerId
board[pos.0, pos.1] = token
return true
}
return false
}
func isGameOver(board: Board) -> (Bool, PlayerId) {
<#code#>
func isGameOver(board: Board) -> (result: Bool, winner: Token) {
// 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.
//
import Foundation
enum PlayerId {
enum Token {
case empty
case player1
case player2
case red
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