From c0babc3e3576b33a0c4356b015717165412303b2 Mon Sep 17 00:00:00 2001 From: Alexis Drai Date: Mon, 23 Jan 2023 19:56:56 +0100 Subject: [PATCH] :tada: --- .gitignore | 130 ++++++++ Connect4/Connect4.xcodeproj/project.pbxproj | 110 +++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + .../connect4_cli.xcodeproj/project.pbxproj | 305 ++++++++++++++++++ Connect4/connect4_cli/connect4_cli/main.swift | 18 ++ Connect4/connect4_lib/.gitignore | 9 + Connect4/connect4_lib/Package.swift | 28 ++ .../Sources/connect4_lib/Board.swift | 83 +++++ .../Tests/connect4_libTests/BoardTest.swift | 8 + README.md | 2 + 11 files changed, 708 insertions(+) create mode 100644 .gitignore create mode 100644 Connect4/Connect4.xcodeproj/project.pbxproj create mode 100644 Connect4/Connect4.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Connect4/Connect4.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 Connect4/connect4_cli/connect4_cli.xcodeproj/project.pbxproj create mode 100644 Connect4/connect4_cli/connect4_cli/main.swift create mode 100644 Connect4/connect4_lib/.gitignore create mode 100644 Connect4/connect4_lib/Package.swift create mode 100644 Connect4/connect4_lib/Sources/connect4_lib/Board.swift create mode 100644 Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..375f8be --- /dev/null +++ b/.gitignore @@ -0,0 +1,130 @@ +# ---> Xcode +## User settings +xcuserdata/ + +## Xcode 8 and earlier +*.xcscmblueprint +*.xccheckout + +# ---> Swift +# Xcode +# +# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore + +## User settings +xcuserdata/ + +## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) +*.xcscmblueprint +*.xccheckout + +## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) +build/ +DerivedData/ +*.moved-aside +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 + +## Obj-C/Swift specific +*.hmap + +## App packaging +*.ipa +*.dSYM.zip +*.dSYM + +## Playgrounds +timeline.xctimeline +playground.xcworkspace + +# Swift Package Manager +# +# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. +# Packages/ +# Package.pins +# Package.resolved +# *.xcodeproj +# +# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata +# hence it is not needed unless you have added a package configuration file to your project +# .swiftpm + +.build/ + +# CocoaPods +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# +# Pods/ +# +# Add this line if you want to avoid checking in source code from the Xcode workspace +# *.xcworkspace + +# Carthage +# +# Add this line if you want to avoid checking in source code from Carthage dependencies. +# Carthage/Checkouts + +Carthage/Build/ + +# Accio dependency management +Dependencies/ +.accio/ + +# fastlane +# +# It is recommended to not store the screenshots in the git repo. +# Instead, use fastlane to re-generate the screenshots whenever they are needed. +# For more information about the recommended setup visit: +# https://docs.fastlane.tools/best-practices/source-control/#source-control + +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots/**/*.png +fastlane/test_output + +# Code Injection +# +# After new code Injection tools there's a generated folder /iOSInjectionProject +# https://github.com/johnno1962/injectionforxcode + +iOSInjectionProject/ + +# ---> macOS +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + + diff --git a/Connect4/Connect4.xcodeproj/project.pbxproj b/Connect4/Connect4.xcodeproj/project.pbxproj new file mode 100644 index 0000000..c4f101e --- /dev/null +++ b/Connect4/Connect4.xcodeproj/project.pbxproj @@ -0,0 +1,110 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 56; + objects = { + +/* Begin PBXContainerItemProxy section */ + EC7D1657297F0ECA001832F1 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = EC7D1653297F0ECA001832F1 /* connect4_cli.xcodeproj */; + proxyType = 2; + remoteGlobalIDString = EC7D1649297F0ECA001832F1; + remoteInfo = connect4_cli; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXFileReference section */ + EC7D163F297F0EAF001832F1 /* connect4_lib */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = connect4_lib; sourceTree = ""; }; + EC7D1653297F0ECA001832F1 /* connect4_cli.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = connect4_cli.xcodeproj; path = connect4_cli/connect4_cli.xcodeproj; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXGroup section */ + EC7D1638297F0E94001832F1 = { + isa = PBXGroup; + children = ( + EC7D1653297F0ECA001832F1 /* connect4_cli.xcodeproj */, + EC7D163F297F0EAF001832F1 /* connect4_lib */, + ); + sourceTree = ""; + }; + EC7D1654297F0ECA001832F1 /* Products */ = { + isa = PBXGroup; + children = ( + EC7D1658297F0ECA001832F1 /* connect4_cli */, + ); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXProject section */ + EC7D1639297F0E94001832F1 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastUpgradeCheck = 1420; + }; + buildConfigurationList = EC7D163C297F0E94001832F1 /* Build configuration list for PBXProject "Connect4" */; + compatibilityVersion = "Xcode 14.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = EC7D1638297F0E94001832F1; + projectDirPath = ""; + projectReferences = ( + { + ProductGroup = EC7D1654297F0ECA001832F1 /* Products */; + ProjectRef = EC7D1653297F0ECA001832F1 /* connect4_cli.xcodeproj */; + }, + ); + projectRoot = ""; + targets = ( + ); + }; +/* End PBXProject section */ + +/* Begin PBXReferenceProxy section */ + EC7D1658297F0ECA001832F1 /* connect4_cli */ = { + isa = PBXReferenceProxy; + fileType = "compiled.mach-o.executable"; + path = connect4_cli; + remoteRef = EC7D1657297F0ECA001832F1 /* PBXContainerItemProxy */; + sourceTree = BUILT_PRODUCTS_DIR; + }; +/* End PBXReferenceProxy section */ + +/* Begin XCBuildConfiguration section */ + EC7D163D297F0E94001832F1 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + }; + name = Debug; + }; + EC7D163E297F0E94001832F1 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + EC7D163C297F0E94001832F1 /* Build configuration list for PBXProject "Connect4" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + EC7D163D297F0E94001832F1 /* Debug */, + EC7D163E297F0E94001832F1 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = EC7D1639297F0E94001832F1 /* Project object */; +} diff --git a/Connect4/Connect4.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Connect4/Connect4.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Connect4/Connect4.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Connect4/Connect4.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Connect4/Connect4.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Connect4/Connect4.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Connect4/connect4_cli/connect4_cli.xcodeproj/project.pbxproj b/Connect4/connect4_cli/connect4_cli.xcodeproj/project.pbxproj new file mode 100644 index 0000000..d098324 --- /dev/null +++ b/Connect4/connect4_cli/connect4_cli.xcodeproj/project.pbxproj @@ -0,0 +1,305 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 56; + objects = { + +/* Begin PBXBuildFile section */ + EC7D164D297F0ECA001832F1 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC7D164C297F0ECA001832F1 /* main.swift */; }; + EC7D165B297F0F37001832F1 /* connect4_lib in Frameworks */ = {isa = PBXBuildFile; productRef = EC7D165A297F0F37001832F1 /* connect4_lib */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + EC7D1647297F0ECA001832F1 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + EC7D1649297F0ECA001832F1 /* connect4_cli */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = connect4_cli; sourceTree = BUILT_PRODUCTS_DIR; }; + EC7D164C297F0ECA001832F1 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + EC7D1646297F0ECA001832F1 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + EC7D165B297F0F37001832F1 /* connect4_lib in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + EC7D1640297F0ECA001832F1 = { + isa = PBXGroup; + children = ( + EC7D164B297F0ECA001832F1 /* connect4_cli */, + EC7D164A297F0ECA001832F1 /* Products */, + EC7D1659297F0F37001832F1 /* Frameworks */, + ); + sourceTree = ""; + }; + EC7D164A297F0ECA001832F1 /* Products */ = { + isa = PBXGroup; + children = ( + EC7D1649297F0ECA001832F1 /* connect4_cli */, + ); + name = Products; + sourceTree = ""; + }; + EC7D164B297F0ECA001832F1 /* connect4_cli */ = { + isa = PBXGroup; + children = ( + EC7D164C297F0ECA001832F1 /* main.swift */, + ); + path = connect4_cli; + sourceTree = ""; + }; + EC7D1659297F0F37001832F1 /* Frameworks */ = { + isa = PBXGroup; + children = ( + ); + name = Frameworks; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + EC7D1648297F0ECA001832F1 /* connect4_cli */ = { + isa = PBXNativeTarget; + buildConfigurationList = EC7D1650297F0ECA001832F1 /* Build configuration list for PBXNativeTarget "connect4_cli" */; + buildPhases = ( + EC7D1645297F0ECA001832F1 /* Sources */, + EC7D1646297F0ECA001832F1 /* Frameworks */, + EC7D1647297F0ECA001832F1 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = connect4_cli; + packageProductDependencies = ( + EC7D165A297F0F37001832F1 /* connect4_lib */, + ); + productName = connect4_cli; + productReference = EC7D1649297F0ECA001832F1 /* connect4_cli */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + EC7D1641297F0ECA001832F1 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1420; + LastUpgradeCheck = 1420; + TargetAttributes = { + EC7D1648297F0ECA001832F1 = { + CreatedOnToolsVersion = 14.2; + }; + }; + }; + buildConfigurationList = EC7D1644297F0ECA001832F1 /* Build configuration list for PBXProject "connect4_cli" */; + compatibilityVersion = "Xcode 14.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = EC7D1640297F0ECA001832F1; + productRefGroup = EC7D164A297F0ECA001832F1 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + EC7D1648297F0ECA001832F1 /* connect4_cli */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + EC7D1645297F0ECA001832F1 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + EC7D164D297F0ECA001832F1 /* main.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + EC7D164E297F0ECA001832F1 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 13.1; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + EC7D164F297F0ECA001832F1 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 13.1; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = macosx; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + }; + name = Release; + }; + EC7D1651297F0ECA001832F1 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + EC7D1652297F0ECA001832F1 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + EC7D1644297F0ECA001832F1 /* Build configuration list for PBXProject "connect4_cli" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + EC7D164E297F0ECA001832F1 /* Debug */, + EC7D164F297F0ECA001832F1 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + EC7D1650297F0ECA001832F1 /* Build configuration list for PBXNativeTarget "connect4_cli" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + EC7D1651297F0ECA001832F1 /* Debug */, + EC7D1652297F0ECA001832F1 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + +/* Begin XCSwiftPackageProductDependency section */ + EC7D165A297F0F37001832F1 /* connect4_lib */ = { + isa = XCSwiftPackageProductDependency; + productName = connect4_lib; + }; +/* End XCSwiftPackageProductDependency section */ + }; + rootObject = EC7D1641297F0ECA001832F1 /* Project object */; +} diff --git a/Connect4/connect4_cli/connect4_cli/main.swift b/Connect4/connect4_cli/connect4_cli/main.swift new file mode 100644 index 0000000..dd75262 --- /dev/null +++ b/Connect4/connect4_cli/connect4_cli/main.swift @@ -0,0 +1,18 @@ +import Foundation +import connect4_lib + +var status: Bool + +if var b = Board() { + print(b) + status = b.insertChip(from: 1, atCol: 1) + print(b) + status = b.insertChip(from: 2, atCol: 2) + print(b) + status = b.insertChip(from: 1, atCol: 1) + print(b) + status = b.insertChip(from: 2, atCol: 1) + print(b) + status = b.insertChip(from: 1, atCol: 1) + print(b) +} diff --git a/Connect4/connect4_lib/.gitignore b/Connect4/connect4_lib/.gitignore new file mode 100644 index 0000000..3b29812 --- /dev/null +++ b/Connect4/connect4_lib/.gitignore @@ -0,0 +1,9 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ +DerivedData/ +.swiftpm/config/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/Connect4/connect4_lib/Package.swift b/Connect4/connect4_lib/Package.swift new file mode 100644 index 0000000..ea8342b --- /dev/null +++ b/Connect4/connect4_lib/Package.swift @@ -0,0 +1,28 @@ +// swift-tools-version: 5.7 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "connect4_lib", + products: [ + // Products define the executables and libraries a package produces, and make them visible to other packages. + .library( + name: "connect4_lib", + targets: ["connect4_lib"]), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .target( + name: "connect4_lib", + dependencies: []), + .testTarget( + name: "connect4_libTests", + dependencies: ["connect4_lib"]), + ] +) diff --git a/Connect4/connect4_lib/Sources/connect4_lib/Board.swift b/Connect4/connect4_lib/Sources/connect4_lib/Board.swift new file mode 100644 index 0000000..551000c --- /dev/null +++ b/Connect4/connect4_lib/Sources/connect4_lib/Board.swift @@ -0,0 +1,83 @@ +import Foundation + +public struct Board : CustomStringConvertible { + + public let nbRows: Int + public let nbCols: Int + private var nbFree: Int + private var grid: [[Int?]] + + public init?(withRows nbRows: Int = 6, andWithCols nbCols: Int = 7) { + guard(nbRows > 0 && nbCols > 0) else { return nil } + self.nbRows = nbRows + self.nbCols = nbCols + self.nbFree = nbRows * nbCols + self.grid = Array(repeating: Array(repeating: nil, count: nbCols), count: nbRows) + } + + public init?(withGrid grid: [[Int?]]) { + guard(grid.allSatisfy{ $0.count == grid[0].count }) else { return nil } + self.nbRows = grid.count + self.nbCols = grid[0].count + self.nbFree = nbRows * nbCols + self.grid = grid + } + + private func isWithinBounds(_ row: Int, and col: Int) -> Bool { + return 0 <= row && row < nbRows + && 0 <= col && col < nbCols + } + + public var description: String { + var string = String() + for row in grid { + for tile in row { + string.append(" ") + switch(tile) { + case 1: string.append("O") + case 2: string.append("X") + default: string.append("_") + } + } + string.append("\n") + } + string.append("\n") + return string + } + + private mutating func insertChip(from playerId: Int, atRow row: Int, atCol col: Int) -> Bool { + guard(isWithinBounds(row, and: col)) else { return false } + guard((playerId == 1 || playerId == 2)) else { return false } + guard(!isFull()) else { return false } + guard((grid[row][col] == nil)) else { return false } + + grid[row][col] = playerId + nbFree -= 1 + return true + } + + public mutating func insertChip(from playerId: Int, atCol col: Int) -> Bool { + guard(0 < col && col <= nbCols) else { return false } + + if grid[0][col] != nil { return false } + + for i in stride(from: nbRows - 1, through: 0, by: -1) { + if grid[i][col] == nil { + return insertChip(from: playerId, atRow: i, atCol: col) + } + } + + return false + } + + private mutating func removeChip(fromRow row: Int, fromCol col: Int) { + assert(isWithinBounds(row, and: col)) + grid[row][col] = nil + nbFree += 1 + } + + public func isFull() -> Bool { + return nbFree <= 0 + } +} + diff --git a/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift b/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift new file mode 100644 index 0000000..641ec0c --- /dev/null +++ b/Connect4/connect4_lib/Tests/connect4_libTests/BoardTest.swift @@ -0,0 +1,8 @@ +// +// File.swift +// +// +// Created by etudiant on 2023-01-23. +// + +import Foundation diff --git a/README.md b/README.md new file mode 100644 index 0000000..110881e --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Connect 4 +## XCode have mercy