59 lines
1.1 KiB
Swift
59 lines
1.1 KiB
Swift
|
import Foundation
|
||
|
|
||
|
typealias CardId = String
|
||
|
|
||
|
struct Card {
|
||
|
|
||
|
enum Symbol: Character {
|
||
|
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"
|
||
|
}
|
||
|
|
||
|
enum Suit: Character {
|
||
|
case eichel = "E"
|
||
|
case blatt = "B"
|
||
|
case herz = "H"
|
||
|
case schelln = "S"
|
||
|
}
|
||
|
|
||
|
|
||
|
let symbol: Symbol
|
||
|
|
||
|
let suit: Suit
|
||
|
|
||
|
init(suit: Suit, symbol: Symbol) {
|
||
|
self.suit = suit
|
||
|
self.symbol = symbol
|
||
|
}
|
||
|
|
||
|
init?(rawValue: String) {
|
||
|
guard rawValue.count == 2 else {
|
||
|
return nil
|
||
|
}
|
||
|
guard let suit = Suit(rawValue: rawValue.first!),
|
||
|
let symbol = Symbol(rawValue: rawValue.last!) else {
|
||
|
return nil
|
||
|
}
|
||
|
self.suit = suit
|
||
|
self.symbol = symbol
|
||
|
}
|
||
|
|
||
|
var id: CardId {
|
||
|
"\(suit)\(symbol)"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
extension Card: CustomStringConvertible {
|
||
|
|
||
|
var description: String {
|
||
|
id
|
||
|
}
|
||
|
}
|
||
|
|