From 158a2ea03f2c91d49673e620c050d0cdbc450827 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Mon, 6 Dec 2021 11:40:53 +0100 Subject: [PATCH] Move/split card file --- Sources/App/Model/{ => Card}/Card.swift | 63 ++++--------------- Sources/App/Model/Card/PlayableCard.swift | 12 ++++ Sources/App/Model/Card/Symbol.swift | 76 +++++++++++++++++++++++ 3 files changed, 100 insertions(+), 51 deletions(-) rename Sources/App/Model/{ => Card}/Card.swift (50%) create mode 100644 Sources/App/Model/Card/PlayableCard.swift create mode 100644 Sources/App/Model/Card/Symbol.swift diff --git a/Sources/App/Model/Card.swift b/Sources/App/Model/Card/Card.swift similarity index 50% rename from Sources/App/Model/Card.swift rename to Sources/App/Model/Card/Card.swift index 2b24b90..146d6b8 100644 --- a/Sources/App/Model/Card.swift +++ b/Sources/App/Model/Card/Card.swift @@ -5,34 +5,6 @@ typealias CardId = String struct Card: Codable { - enum Symbol: Character, CaseIterable, Codable { - case ass = "A" - case zehn = "Z" - case könig = "K" - case ober = "O" - case unter = "U" - case neun = "9" - case acht = "8" - case sieben = "7" - - var points: Int { - switch self { - case .ass: - return 11 - case .zehn: - return 10 - case .könig: - return 4 - case .ober: - return 3 - case .unter: - return 2 - default: - return 0 - } - } - } - enum Suit: Character, CaseIterable, Codable { case eichel = "E" case blatt = "B" @@ -54,12 +26,12 @@ struct Card: Codable { self.symbol = symbol } - init?(rawValue: String) { - guard rawValue.count == 2 else { + init?(id: CardId) { + guard id.count == 2 else { return nil } - guard let suit = Suit(rawValue: rawValue.first!), - let symbol = Symbol(rawValue: rawValue.last!) else { + guard let suit = Suit(rawValue: id.first!), + let symbol = Symbol(id: id.last!) else { return nil } self.suit = suit @@ -67,7 +39,7 @@ struct Card: Codable { } var id: CardId { - "\(suit.rawValue)\(symbol.rawValue)" + "\(suit.rawValue)\(symbol.id)" } var points: Int { @@ -84,6 +56,13 @@ struct Card: Codable { }() } +extension Card { + + func isTrump(in game: GameType) -> Bool { + game.sortingType.isTrump(self) + } +} + extension Card: CustomStringConvertible { var description: String { @@ -94,21 +73,3 @@ extension Card: CustomStringConvertible { extension Card: Hashable { } - -struct PlayableCard { - - let card: Card - - let isPlayable: Bool - - var cardInfo: CardInfo { - .init(card: card.id, playable: isPlayable) - } -} - -struct CardInfo: Codable, Equatable { - - let card: CardId - - let playable: Bool -} diff --git a/Sources/App/Model/Card/PlayableCard.swift b/Sources/App/Model/Card/PlayableCard.swift new file mode 100644 index 0000000..2f3e293 --- /dev/null +++ b/Sources/App/Model/Card/PlayableCard.swift @@ -0,0 +1,12 @@ +import Foundation + +struct PlayableCard { + + let card: Card + + var isPlayable: Bool + + var cardInfo: CardInfo { + .init(card: card.id, playable: isPlayable) + } +} diff --git a/Sources/App/Model/Card/Symbol.swift b/Sources/App/Model/Card/Symbol.swift new file mode 100644 index 0000000..37f6b82 --- /dev/null +++ b/Sources/App/Model/Card/Symbol.swift @@ -0,0 +1,76 @@ +import Foundation + +extension Card { + + enum Symbol: Int, CaseIterable, Comparable, Codable { + + case ass = 1 + case zehn = 2 + case könig = 3 + case ober = 4 + case unter = 5 + case neun = 6 + case acht = 7 + case sieben = 8 + + init?(id: Character) { + switch id { + case "A": self = .ass + case "Z": self = .zehn + case "K": self = .könig + case "O": self = .ober + case "U": self = .unter + case "9": self = .neun + case "8": self = .acht + case "7": self = .sieben + default: + return nil + } + } + + var id: Character { + switch self { + case .ass: return "A" + case .zehn: return "Z" + case .könig: return "K" + case .ober: return "O" + case .unter: return "U" + case .neun: return "9" + case .acht: return "8" + case .sieben: return "7" + } + } + + var points: Int { + switch self { + case .ass: + return 11 + case .zehn: + return 10 + case .könig: + return 4 + case .ober: + return 3 + case .unter: + return 2 + default: + return 0 + } + } + + var isTrumpOrAce: Bool { + switch self { + case .ass, .ober, .unter: + return true + default: + return false + } + } + + static func < (lhs: Card.Symbol, rhs: Card.Symbol) -> Bool { + lhs.rawValue < rhs.rawValue + } + + } + +}