Skip to content

Instantly share code, notes, and snippets.

@ChrisMash
Created February 16, 2022 12:26
Show Gist options
  • Select an option

  • Save ChrisMash/b80ef53ea924ea53dd3352379e3a4ede to your computer and use it in GitHub Desktop.

Select an option

Save ChrisMash/b80ef53ea924ea53dd3352379e3a4ede to your computer and use it in GitHub Desktop.
Snapshot of GameState from https://github.com/ChrisMash/SwiftUI-Playing-Cards for my blog
// A representation of the current state of the game,
// including which cards each player has and the deck
// of cards that can be picked up from
class GameState {
private(set) var deck = Hand()
private(set) var hands = [Hand]()
init() {
// Fill the deck with 20 cards, shuffled
for i in 1...20 {
deck.cards.append(Card(number: i))
}
deck.cards.shuffle()
// Setup two hands of cards with one card each from the deck
for _ in 0..<2 {
let hand = Hand()
for _ in 0..<1 {
guard let card = deck.cards.popLast() else {
fatalError("Tried to deal more cards than in the deck")
}
hand.cards.append(card)
}
hands.append(hand)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment