-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
130 lines (121 loc) · 5.21 KB
/
ViewController.swift
File metadata and controls
130 lines (121 loc) · 5.21 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
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
//
// ViewController.swift
// Set
//
// Created by Raj Makda on 2/13/18.
// Copyright © 2018 Raj Makda. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var game = Set()
var gameStarted = false
@IBOutlet lazy var cardButtons: [UIButton]! = {
setInitialButtonsView()
}()
@IBOutlet weak var dealCardsButton: UIButton!
@IBOutlet weak var scoreLabel: UILabel!
func setInitialButtonsView() -> [UIButton] {
for index in game.cardsInPlay.indices {
cardButtons[index].setTitle("", for: UIControlState.normal)
cardButtons[index].isEnabled = false;
cardButtons[index].layer.borderWidth = 3.0
cardButtons[index].layer.borderColor = UIColor.black.cgColor
cardButtons[index].layer.cornerRadius = 8.0
}
return cardButtons
}
@IBAction func selectCard(_ sender: UIButton) {
if !game.isComplete {
if let indexOfSelectedCard = cardButtons.index(of: sender) {
game.chooseCard(game.cardsInPlay[indexOfSelectedCard])
}
syncUIWithModel()
}
print("Cards in play = \(game.cardsInPlay.count) , Cards left in deck = \(game.deck.count), Matching cards = \(game.matchedCards.count)")
}
@IBAction func drawCards(_ sender: UIButton) {
if game.cardsInPlay.count < 24 {
if !gameStarted {
game.drawCards(numberOfCards: 12)
dealCardsButton.setTitle("Deal 3 cards", for: UIControlState.normal)
gameStarted = !gameStarted
} else {
game.drawCards(numberOfCards: 3)
}
syncUIWithModel()
}
}
func syncUIWithModel() {
scoreLabel.text = "Score: \(game.score)"
if game.deck.isEmpty || game.cardsInPlay.count >= 24 {
dealCardsButton.isEnabled = false
} else if game.cardsInPlay.count < 24 {
dealCardsButton.isEnabled = true
}
for index in cardButtons.indices {
if index < game.cardsInPlay.count {
cardButtons[index].isEnabled = true
cardToButtonView(for: cardButtons[index], with: game.cardsInPlay[index])
} else {
cardButtons[index].isEnabled = false
cardToButtonView(for: cardButtons[index], with: Card(color: "white", number: 0, shape: "", shading: "empty"))
}
}
if game.isComplete {
game = Set()
gameStarted = false
dealCardsButton.setTitle("Restart Game", for: UIControlState.normal)
dealCardsButton.isEnabled = true
}
selectedCardUI()
}
func selectedCardUI() {
for index in game.cardsInPlay.indices {
if game.selectedCards.contains(game.cardsInPlay[index]) {
cardButtons[index].layer.borderWidth = 3.0
cardButtons[index].layer.borderColor = UIColor.blue.cgColor
cardButtons[index].layer.cornerRadius = 8.0
} else {
cardButtons[index].layer.borderWidth = 3.0
cardButtons[index].layer.borderColor = UIColor.black.cgColor
cardButtons[index].layer.cornerRadius = 8.0
}
}
for index in game.cardsInPlay.count..<cardButtons.count {
cardButtons[index].layer.borderWidth = 3.0
cardButtons[index].layer.borderColor = UIColor.black.cgColor
cardButtons[index].layer.cornerRadius = 8.0
}
}
func cardToButtonView(for button: UIButton ,with card: Card) {
var buttonTitle = ""
let cardShape = card.shape
let cardNumber = card.number
let cardColor = card.color
var swiftColor = UIColor.white
if cardColor == "red" {
swiftColor = UIColor.red
} else if cardColor == "green" {
swiftColor = UIColor.green
} else if cardColor == "purple" {
swiftColor = UIColor.purple
}
let cardShading = card.shading
for _ in 0..<cardNumber {
buttonTitle = buttonTitle + cardShape
}
if cardShading == "empty" {
let myAttribute = [ NSAttributedStringKey.foregroundColor: swiftColor, NSAttributedStringKey.strokeWidth: 2] as [NSAttributedStringKey : Any]
let myAttrString = NSAttributedString(string: buttonTitle, attributes: myAttribute)
button.setAttributedTitle(myAttrString, for: UIControlState.normal)
} else if cardShading == "striped" {
let myAttribute = [ NSAttributedStringKey.foregroundColor: swiftColor.withAlphaComponent(0.25)] as [NSAttributedStringKey : Any]
let myAttrString = NSAttributedString(string: buttonTitle, attributes: myAttribute)
button.setAttributedTitle(myAttrString, for: UIControlState.normal)
} else {
let myAttribute = [ NSAttributedStringKey.foregroundColor: swiftColor.withAlphaComponent(1)] as [NSAttributedStringKey : Any]
let myAttrString = NSAttributedString(string: buttonTitle, attributes: myAttribute)
button.setAttributedTitle(myAttrString, for: UIControlState.normal)
}
}
}