
Model 의 전체적인 로직은 2021 강의를 들으면서 했던 것과 유사해서 생략하고, ViewController 관련 내용만 정리해 볼 생각이다

이 과제를 하면서 가장 많이 고민한 부분이었는데, 카드의 속성은 Model 로부터 주어지지만, 이를 화면에 어떻게 그리느냐였다. 나는 문양, 색깔, 개수, 무늬의 4가지 속성을 모두 각각의 enum 으로 선언해서 이걸 사용하긴 해야할 것 같은데 어떻게??? 가 난관이었다.
유저가 카드를 터치할 때마다 뭔가 나타내야할 변화가 생길 것이므로 updateViewFromModel() 메서드를 만들어서 touchCard(_:) 메서드가 내부에서 호출하는 것까지는 Concentration 과 같았고 이제 updateViewFromModel() 의 내부만 구현하면 됐는데,
먼저 버튼과 playingCards 의 개수가 게임이 진행됨에 따라 다를 텐데 while 문을 어떻게 작성해야할지가 고민됐다. UI 상에는 24개의 버튼이 있는데, 애초에 게임을 시작할 때는 12개의 카드로 시작하기도하고, 게임이 진행됨에 따라 카드 개수가 24개보다 적을 수 있다(카드가 24개면 더 이상 추가할 수 없도록 해서 카드 개수가 24개를 초과할 수는 없다). 카드 개수가 24개보다 적은 경우에는 나머지 버튼들을 투명하게 해줘야 했다. 카드 버튼을 기준으로 돌리되, playingCards 에 있으면 카드에 따라 그리고, 없는 경우 투명하게 했다.
class ViewController: UIViewController {
private func updateViewFromModel() {
for index in cardButtons.indices {
let button = cardButtons[index]
if game.playingCards.indices.contains(index) {
// some code
}
}
}
}
class ViewController: UIViewController {
private func getAttributedString(for card: Card) -> NSAttributedString {
var attributes: [NSAttributedString.Key:Any] = [
.font: UIFont.systemFont(ofSize: 26)
]
var string: String
switch card.shape {
case .diamond:
string = "▲"
case .oval:
string = "●"
case .squiggle:
string = "■"
}
switch card.number {
case .one:
break
case .two:
string += string
case .three:
string += (string + string)
}
switch card.color {
case .red:
attributes[.foregroundColor] = UIColor.systemRed
case .green:
attributes[.foregroundColor] = UIColor.systemGreen
case .purple:
attributes[.foregroundColor] = UIColor.systemPurple
}
switch card.shading {
case .open:
attributes[.strokeWidth] = 8
case .striped:
let color: UIColor = attributes[.foregroundColor] as! UIColor
attributes[.foregroundColor] = color.withAlphaComponent(0.3)
case .solid:
attributes[.strokeWidth] = -1
}
return NSAttributedString(string: string, attributes: attributes)
}
}
button.layer.borderColor = UIColor.blue.cgColor
button.layer.borderWidth = 1.0

class ViewController: UIViewController {
private func updateViewFromModel() {
if !game.cardDeck.isEmpty
&& (game.playingCards.count < 24 || game.isSet) {
dealThreeCardsButton.isEnabled = true
} else {
dealThreeCardsButton.isEnabled = false
}
}
}

class ViewController: UIViewController {
private func updateViewFromModel() {
for index in cardButtons.indices {
let button = cardButtons[index]
if game.playingCards.indices.contains(index) {
// some code
} else {
button.backgroundColor = .clear
button.layer.borderColor = UIColor.clear.cgColor
button.setAttributedTitle(nil, for: .normal)
}
}
}
}