Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. //
  2. // SetDeck.swift
  3. // SetGame
  4. //
  5. // Created by Edward Biswas on 10/10/18.
  6. // Copyright © 2018 Edward Biswas. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10.  
  11. struct SetDeck {
  12.  
  13. private var deck: [SetCard]
  14.  
  15. // Get the total number of cards in deck
  16. func count() -> Int {
  17. return self.deck.count
  18. }
  19.  
  20. // Checking if the deck is empty
  21. func isEmpty() -> Bool {
  22. return self.deck.count == 0 ? true : false
  23. }
  24.  
  25. // Get single card from the deck
  26. mutating func dealCard() -> SetCard? {
  27. if (!self.isEmpty()) {
  28. let card = self.deck.remove(at: 0)
  29. return card
  30. }
  31. return nil
  32. }
  33.  
  34. // Initializer
  35. init() {
  36. self.deck = [SetCard]()
  37.  
  38. // Creating deck
  39. for shape in SetCard.Shapes.all {
  40. for color in SetCard.Colors.all {
  41. for shade in SetCard.Shades.all {
  42. for count in 1...3 {
  43. let card = SetCard(shape: shape,
  44. color: color,
  45. shade: shade,
  46. count: count)
  47. self.deck.append(card)
  48. }
  49. }
  50. }
  51. }
  52.  
  53. // Randomizing the card in the deck
  54. for _ in 1...10 {
  55. for index in self.deck.indices {
  56. let randomIndex = index.arc4random
  57. let card = self.deck.remove(at: randomIndex)
  58. self.deck.append(card)
  59. }
  60. }
  61. }
  62.  
  63. }
  64.  
  65. extension Int {
  66. var arc4random: Int {
  67. if (self > 0){
  68. return Int(arc4random_uniform(UInt32(self)))
  69. } else if (self < 0) {
  70. return Int(arc4random_uniform(UInt32(self)))
  71. } else {
  72. return 0
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement