From 62634d2beafb89336562cd9edb2ce044fde7db97 Mon Sep 17 00:00:00 2001 From: Maksymilian Jopek Date: Mon, 27 Mar 2023 18:58:16 +0200 Subject: Full game --- .gitignore | 9 ++++ Package.swift | 19 +++++++ README.md | 5 ++ Sources/jednadvacet/main.swift | 115 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 .gitignore create mode 100644 Package.swift create mode 100644 README.md create mode 100644 Sources/jednadvacet/main.swift diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b29812 --- /dev/null +++ b/.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/Package.swift b/Package.swift new file mode 100644 index 0000000..d8795ff --- /dev/null +++ b/Package.swift @@ -0,0 +1,19 @@ +// swift-tools-version: 5.6 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "jednadvacet", + 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. + .executableTarget( + name: "jednadvacet", + dependencies: []), + ] +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..a7e13b4 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# jednadvacet + +A game of `Twenty-One` made with swift in console. + +To start run `swift run` diff --git a/Sources/jednadvacet/main.swift b/Sources/jednadvacet/main.swift new file mode 100644 index 0000000..fa22ee4 --- /dev/null +++ b/Sources/jednadvacet/main.swift @@ -0,0 +1,115 @@ +import Foundation + +let values = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "W", "D", "K", "A"] +let colors = ["♠", "♥", "♦", "♣"] +let valuesToPoints = ["2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "10": 10, "W": 2, "D": 3, "K": 4, "A": 11] + +class Card: Hashable { + var color = ""; + var value = ""; + + init(_ c: String, _ v: String) { + color = c; + value = v; + } + + func points() -> Int { + valuesToPoints[value]! + } + func str() -> String { + color + value + } + + func hash(into hasher: inout Hasher) { + hasher.combine(color + value) + } +} +func ==(card1: Card, card2: Card) -> Bool { + return card1.str() == card2.str() +} + +func persianEye(_ cs: Set) -> Bool { + if cs.count == 2 { + for c in cs { + if c.value != "A" { + return false + } + } + return true + } + return false +} + +var cards: Set = [] +var player: Set = [] +var playerPoints = 0 +var bot: Set = [] +var botPoints = 0 + +for c in colors { + for v in values { + cards.insert(Card(c, v)) + } +} + +var choice = "" +print("Player:") +while choice.lowercased() != "pas" { + let card = cards.randomElement()! + cards.remove(card) + player.insert(card) + playerPoints += card.points() + for (i, c) in player.enumerated() { + var term = ", " + if i == player.count - 1 { + term = "" + } + print(c.str() + " " + "(\(c.points()))", terminator: term) + } + print("\t\tΣ \(playerPoints)") + if playerPoints == 21 || persianEye(player) { + print("\nPlayer won!!!") + exit(0) + } else if playerPoints > 21 { + print("\nComputer won!!!") + exit(0) + } + choice = readLine()! +} + +print("\nComputer:") +while true { + let card = cards.randomElement()! + cards.remove(card) + bot.insert(card) + botPoints += card.points() + for (i, c) in bot.enumerated() { + var term = ", " + if i == bot.count - 1 { + term = "" + } + print(c.str() + " " + "(\(c.points()))", terminator: term) + } + print("\t\tΣ \(botPoints)") + if botPoints == 21 || persianEye(bot) { + print("\nComputer won!!!") + exit(0) + } else if botPoints > 21 { + print("\nPlayer won!!!") + exit(0) + } else if playerPoints < botPoints && (botPoints + 5 > 21) { + print("Pas") + break + } else { + print("Dobieram") + } + usleep(500_000) +} + +if playerPoints > botPoints { + print("\nPlayer won!!!") +} else if playerPoints == botPoints { + print("\nRemis!!!") +} else { + print("\nComputer won!!!") +} -- cgit v1.3.1