Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // SetDeck.swift
- // SetGame
- //
- // Created by Edward Biswas on 10/10/18.
- // Copyright © 2018 Edward Biswas. All rights reserved.
- //
- import Foundation
- struct SetDeck {
- private var deck: [SetCard]
- // Get the total number of cards in deck
- func count() -> Int {
- return self.deck.count
- }
- // Checking if the deck is empty
- func isEmpty() -> Bool {
- return self.deck.count == 0 ? true : false
- }
- // Get single card from the deck
- mutating func dealCard() -> SetCard? {
- if (!self.isEmpty()) {
- let card = self.deck.remove(at: 0)
- return card
- }
- return nil
- }
- // Initializer
- init() {
- self.deck = [SetCard]()
- // Creating deck
- for shape in SetCard.Shapes.all {
- for color in SetCard.Colors.all {
- for shade in SetCard.Shades.all {
- for count in 1...3 {
- let card = SetCard(shape: shape,
- color: color,
- shade: shade,
- count: count)
- self.deck.append(card)
- }
- }
- }
- }
- // Randomizing the card in the deck
- for _ in 1...10 {
- for index in self.deck.indices {
- let randomIndex = index.arc4random
- let card = self.deck.remove(at: randomIndex)
- self.deck.append(card)
- }
- }
- }
- }
- extension Int {
- var arc4random: Int {
- if (self > 0){
- return Int(arc4random_uniform(UInt32(self)))
- } else if (self < 0) {
- return Int(arc4random_uniform(UInt32(self)))
- } else {
- return 0
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement