Move/split card file

This commit is contained in:
Christoph Hagen 2021-12-06 11:40:53 +01:00
parent 3db9652cad
commit 158a2ea03f
3 changed files with 100 additions and 51 deletions

View File

@ -5,34 +5,6 @@ typealias CardId = String
struct Card: Codable { 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 { enum Suit: Character, CaseIterable, Codable {
case eichel = "E" case eichel = "E"
case blatt = "B" case blatt = "B"
@ -54,12 +26,12 @@ struct Card: Codable {
self.symbol = symbol self.symbol = symbol
} }
init?(rawValue: String) { init?(id: CardId) {
guard rawValue.count == 2 else { guard id.count == 2 else {
return nil return nil
} }
guard let suit = Suit(rawValue: rawValue.first!), guard let suit = Suit(rawValue: id.first!),
let symbol = Symbol(rawValue: rawValue.last!) else { let symbol = Symbol(id: id.last!) else {
return nil return nil
} }
self.suit = suit self.suit = suit
@ -67,7 +39,7 @@ struct Card: Codable {
} }
var id: CardId { var id: CardId {
"\(suit.rawValue)\(symbol.rawValue)" "\(suit.rawValue)\(symbol.id)"
} }
var points: Int { 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 { extension Card: CustomStringConvertible {
var description: String { var description: String {
@ -94,21 +73,3 @@ extension Card: CustomStringConvertible {
extension Card: Hashable { 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
}

View File

@ -0,0 +1,12 @@
import Foundation
struct PlayableCard {
let card: Card
var isPlayable: Bool
var cardInfo: CardInfo {
.init(card: card.id, playable: isPlayable)
}
}

View File

@ -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
}
}
}