-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatchmaker.swift
75 lines (67 loc) · 1.93 KB
/
Matchmaker.swift
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
//
// Concentration.swift
// Concentration
//
// Created by Yiwen Gao on 3/13/18.
// Copyright © 2018 Yiwen Gao. All rights reserved.
//
import Foundation
class Matchmaker {
//classes get an init() for free
var cards = [Card]() //or Array<Card>()
var faceUpCardIndex: Int? //{
// get {
// if self.faceUpCardIndex == nil {
// for index in cards.indices {
// if cards[index].isFaceUp == true {
// return index
// }
// }
// }
// return nil
// }
// set {
//
// }
// }
init(numberOfButtons: Int) {
while cards.count < numberOfButtons {
/*putting structs in arrays also copies them
so actually three cards with same id*/
let card = Card()
var randomIndex = Int(arc4random_uniform(UInt32(cards.count)))
cards.insert(card, at: randomIndex)
randomIndex = Int(arc4random_uniform(UInt32(cards.count)))
cards.insert(card, at: randomIndex) //can also append with cards += [card, card]
}
}
func compareCards(at index: Int) -> Bool {
if faceUpCardIndex! == index {
return false
}
let faceUpCard = cards[faceUpCardIndex!]
let card = cards[index]
if faceUpCard.id == card.id {
cards[faceUpCardIndex!].isMatched = true
cards[index].isMatched = true
return true
}
return false
}
func determineFaceUpCardIndex(because index: Int) {
if faceUpCardIndex == nil {
faceUpCardIndex = index
}
else {
faceUpCardIndex = nil
}
}
func flipCard(at index: Int) {
if cards[index].isFaceUp {
cards[index].isFaceUp = true
}
else {
cards[index].isFaceUp = false
}
}
}