CasinoGames/Baccarat/Models/Card.swift

113 lines
3.0 KiB
Swift

//
// Card.swift
// Baccarat
//
// A playing card model with suit, rank, and baccarat value calculation.
//
import Foundation
/// Represents the four suits in a standard deck of cards.
enum Suit: String, CaseIterable, Identifiable {
case hearts = ""
case diamonds = ""
case clubs = ""
case spades = ""
var id: String { rawValue }
/// Whether this suit is red (hearts or diamonds).
var isRed: Bool {
self == .hearts || self == .diamonds
}
/// Accessibility name for VoiceOver.
var accessibilityName: String {
switch self {
case .hearts: return String(localized: "Hearts")
case .diamonds: return String(localized: "Diamonds")
case .clubs: return String(localized: "Clubs")
case .spades: return String(localized: "Spades")
}
}
}
/// Represents the rank of a card from Ace through King.
enum Rank: Int, CaseIterable, Identifiable {
case ace = 1
case two = 2
case three = 3
case four = 4
case five = 5
case six = 6
case seven = 7
case eight = 8
case nine = 9
case ten = 10
case jack = 11
case queen = 12
case king = 13
var id: Int { rawValue }
/// The display symbol for this rank.
var symbol: String {
switch self {
case .ace: return "A"
case .jack: return "J"
case .queen: return "Q"
case .king: return "K"
default: return String(rawValue)
}
}
/// The baccarat point value for this rank.
/// Aces = 1, 2-9 = face value, 10/J/Q/K = 0
var baccaratValue: Int {
switch self {
case .ace: return 1
case .two, .three, .four, .five, .six, .seven, .eight, .nine:
return rawValue
case .ten, .jack, .queen, .king:
return 0
}
}
/// Accessibility name for VoiceOver.
var accessibilityName: String {
switch self {
case .ace: return String(localized: "Ace")
case .two: return String(localized: "Two")
case .three: return String(localized: "Three")
case .four: return String(localized: "Four")
case .five: return String(localized: "Five")
case .six: return String(localized: "Six")
case .seven: return String(localized: "Seven")
case .eight: return String(localized: "Eight")
case .nine: return String(localized: "Nine")
case .ten: return String(localized: "Ten")
case .jack: return String(localized: "Jack")
case .queen: return String(localized: "Queen")
case .king: return String(localized: "King")
}
}
}
/// Represents a single playing card with a suit and rank.
struct Card: Identifiable, Equatable {
let id = UUID()
let suit: Suit
let rank: Rank
/// The baccarat point value of this card.
var baccaratValue: Int {
rank.baccaratValue
}
/// Display string showing rank and suit together.
var display: String {
"\(rank.symbol)\(suit.rawValue)"
}
}