48 lines
1.1 KiB
Swift
48 lines
1.1 KiB
Swift
//
|
|
// Hand.swift
|
|
// Baccarat
|
|
//
|
|
// Represents a baccarat hand (Player or Banker) with cards and scoring.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
/// Represents a hand of cards in baccarat.
|
|
struct Hand: Identifiable {
|
|
let id = UUID()
|
|
private(set) var cards: [Card] = []
|
|
|
|
/// The total baccarat value of the hand (0-9).
|
|
var value: Int {
|
|
let total = cards.reduce(0) { $0 + $1.baccaratValue }
|
|
return total % 10
|
|
}
|
|
|
|
/// Whether this hand is a "natural" (8 or 9 on first two cards).
|
|
var isNatural: Bool {
|
|
cards.count == 2 && value >= 8
|
|
}
|
|
|
|
/// The number of cards in this hand.
|
|
var cardCount: Int {
|
|
cards.count
|
|
}
|
|
|
|
/// Adds a card to the hand.
|
|
/// - Parameter card: The card to add.
|
|
mutating func addCard(_ card: Card) {
|
|
cards.append(card)
|
|
}
|
|
|
|
/// Clears all cards from the hand.
|
|
mutating func clear() {
|
|
cards = []
|
|
}
|
|
|
|
/// Returns the third card if one was drawn, nil otherwise.
|
|
var thirdCard: Card? {
|
|
cards.count >= 3 ? cards[2] : nil
|
|
}
|
|
}
|
|
|