-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet.swift
More file actions
101 lines (93 loc) · 3.22 KB
/
Set.swift
File metadata and controls
101 lines (93 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// Set.swift
// Set
//
// Created by Raj Makda on 2/13/18.
// Copyright © 2018 Raj Makda. All rights reserved.
//
import Foundation
class Set {
var deck = [Card]()
var cardsInPlay = [Card]()
var selectedCards = [Card]()
var matchedCards = [Card]()
var isComplete : Bool {
return matchedCards.count == 81
}
var score = 0
let colorSet = ["green","purple","red"]
let numberSet = [1,2,3]
let shapeSet = ["●","■","▲"]
let shadingSet = ["solid","striped","empty"]
init() {
for i in 0..<3 {
for j in 0..<3 {
for k in 0..<3 {
for l in 0..<3 {
let card = Card(color: colorSet[i], number: numberSet[j], shape: shapeSet[k], shading: shadingSet[l])
deck.append(card)
}
}
}
}
deck = shuffleCards(in: deck)
}
func shuffleCards(in cards: [Card]) -> [Card] {
var shuffledCards = cards
var last = shuffledCards.count - 1
while(last > 0)
{
let rand = Int(arc4random_uniform(UInt32(last)))
shuffledCards.swapAt(last, rand)
last -= 1
}
return shuffledCards
}
func chooseCard(_ card: Card) {
var indexOfPreviouslySelectedCard = selectedCards.index(of: card)
//three cards selected
if selectedCards.count == 3 {
indexOfPreviouslySelectedCard = nil
selectedCards.removeAll()
} //two cards selected
else if selectedCards.count == 2 {
//check if 3 selected cards are matching
//if match replace them with cards from the deck
if Card.isSetOfThree(first: selectedCards[0], second: selectedCards[1], third: card) {
score += 3
let matchingCards = [selectedCards[0], selectedCards[1], card]
matchedCards.append(contentsOf: matchingCards)
for matchedCard in matchingCards {
let indexOfMatchedCardInPlay = cardsInPlay.index(of: matchedCard)
if let index = indexOfMatchedCardInPlay {
if !deck.isEmpty {
cardsInPlay[index] = deck.popLast()!
} else {
cardsInPlay.remove(at: index)
}
}
}
selectedCards.removeAll()
} else {
//cards do not match
if selectedCards[0] != card && selectedCards[1] != card {
score -= 4
}
}
}
if indexOfPreviouslySelectedCard == nil && !matchedCards.contains(card) {
selectedCards.append(card)
} else if let index = indexOfPreviouslySelectedCard {
score -= 1
selectedCards.remove(at: index)
}
}
func drawCards(numberOfCards: Int) {
if !deck.isEmpty && deck.count >= numberOfCards {
cardsInPlay.append(contentsOf: deck[0..<numberOfCards])
deck.removeSubrange(0..<numberOfCards)
} else {
print("deck contents: \(deck)")
}
}
}