forked from BH12ri/ConcentrationGameApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConcentrationViewController.swift
173 lines (147 loc) · 5.33 KB
/
ConcentrationViewController.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// ViewController.swift
// Concentration
//
// Created by BHAVANASINGH on 02/01/19.
// Copyright © 2019 Stanford University. All rights reserved.
//
import UIKit
class ConcentrationViewController: UIViewController {
private var countLabel = ""
private var gameThemeSelected = false
private lazy var game = Concentration(numberOfPairOfCards: numberOfPairOfCards)
var numberOfPairOfCards: Int {
get{
return (cardButtons.count+1)/2
}
}
@IBOutlet private var cardButtons: [UIButton]!
// ------LABEL UPDATE AND DISPLAY-------
private(set) var score = 0{
didSet{
countLabel = "Score:"
updateLabel(Label : countLabel , Value : score, LabelType: scoreLabel)
}
}
private(set) var flipCount = 0 {
didSet {
countLabel = "Flips:"
updateLabel(Label : countLabel, Value : flipCount, LabelType: flipCountLabel)
}
}
@IBOutlet private weak var flipCountLabel: UILabel!{
didSet{
countLabel = "Flips:"
updateLabel(Label : countLabel, Value : flipCount, LabelType: flipCountLabel)
}
}
@IBOutlet weak var scoreLabel: UILabel!{
didSet{
countLabel = "Score:"
updateLabel(Label : countLabel, Value : score, LabelType: scoreLabel)
}
}
private func updateLabel(Label: String, Value : Int, LabelType : UILabel){
let attributes : [NSAttributedString.Key : Any] = [
.strokeWidth : 5.0,
.strokeColor : #colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1)
]
let attributedString = NSAttributedString(string:"\(Label) \(Value)" , attributes: attributes)
LabelType.attributedText = attributedString
}
//-----------***-------
// Funtion: When a card is touched to flip it over
@IBAction private func touchCard(_ sender: UIButton) {
flipCount += 1
let cardCount = cardButtons.index(of: sender)!
game.chooseCard(at: cardCount)
score = game.updateScoreValue()
updateViewFromModel()
}
// Function : To restart the game
@IBAction private func newGame(_ sender: UIButton) {
game = Concentration(numberOfPairOfCards: numberOfPairOfCards)
flipCount = 0
score = 0
gameThemeSelected = false
updateViewFromModel()
}
// ---Function : Update View of Game during playing Concentration----
private func updateViewFromModel(){
//if gameThemeSelected{
if cardButtons != nil {
for index in cardButtons.indices{
let button = cardButtons[index]
let card = game.cards[index]
if card.isFaceUp{
button.setTitle(emoji(for: card), for: UIControl.State.normal)
button.backgroundColor = #colorLiteral(red: 0.4352941176, green: 0.4431372549, blue: 0.4745098039, alpha: 1)
}else{
button.setTitle("", for: UIControl.State.normal)
button.backgroundColor = card.isMatched ?#colorLiteral(red: 1, green: 0.5763723254, blue: 0, alpha: 0) : #colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1)
}
}
}
// }else{
// gameThemeSelected = true
// gameThemeCaseRandomSelect()
//
// }
}
// -------EMOJI THEME SELECTION (assignment)-------
// private var theme : Theme! {
// didSet {
// implementTheme()
// }
// }
// private func gameThemeCaseRandomSelect(){
// print(gameThemeSelected)
// theme = Theme(rawValue: Theme.count.arc4random) ?? Theme.Halloween
// }
//
// private func implementTheme() {
// emoji = [:]
// unusedEmojis = theme.emojiChoices
// updateViewFromModel()
//
// }
// ----Code for selection of the emojis to appear on cards---
// private var emoji = [Card:String]()
// private var unusedEmojis: String!
//
// private func emoji(for card: Card) -> String {
// if emoji[card] == nil, unusedEmojis != nil {
// let randomStringIndex = unusedEmojis.index(unusedEmojis.startIndex, offsetBy: unusedEmojis.count.arc4random)
// emoji[card] = String(unusedEmojis.remove(at: randomStringIndex))
// }
// return emoji[card] ?? "?"
// }
// -----Theme Selection using Multiple MVC Concept------
var theme: String?{
didSet {
emojiChoices = theme ?? ""
emoji = [:]
updateViewFromModel()
}
}
private var emojiChoices = "🐶🐣🐼🐙🦋🐳🐯🦁"
private var emoji = [Card:String]()
private func emoji(for card: Card) -> String {
if emoji[card] == nil, emojiChoices.count > 0 {
let randomStringIndex = emojiChoices.index(emojiChoices.startIndex, offsetBy: emojiChoices.count.arc4random)
emoji[card] = String(emojiChoices.remove(at: randomStringIndex))
}
return emoji[card] ?? "?"
}
}
extension Int {
var arc4random : Int {
if self < 0 {
return -Int(arc4random_uniform(UInt32(abs(self))))
}else if self > 0 {
return Int(arc4random_uniform(UInt32(self)))
}else {
return 0
}
}
}