Posts

Post not yet marked as solved
5 Replies
I'm so sorry! I copied the screenshots but it didn't show.My code is as follows:import UIKit class ViewController: UIViewController { lazy var game = Concentration(numberOfPairsOfCards: (cardButtons.count + 1) / 2) var flipCount: Int = 0 { didSet { flipCountLabel.text = "Flips: \(flipCount)" } } @IBOutlet var cardButtons: [UIButton]! @IBOutlet weak var flipCountLabel: UILabel! @IBAction func touchCard(_ sender: UIButton) { flipCount += 1 if let cardNumber = cardButtons.firstIndex(of: sender) { game.chooseCard(at: cardNumber) updateViewFromModel() } else { print("chosen card was not in cardBottons") } } func updateViewFromModel() { 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: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) // white } else { button.setTitle("", for: UIControl.State.normal) button.backgroundColor = card.isMatched ? colorLiteral(red: 1, green: 1, blue: 1, alpha: 0) : colorLiteral(red: 1, green: 0.5763723254, blue: 0, alpha: 1) // true: transparent, false: orange } } } func emoji(for card: Card) -> String { return "?" } }import Foundation class Concentration { var cards = Array() func chooseCard(at index: Int) { if cards[index].isFaceUp { cards[index].isFaceUp = false } else { cards[index].isFaceUp = true } } init(numberOfPairsOfCards: Int) { for _ in 1...numberOfPairsOfCards { let card = Card() cards += [card, card] } // TODO: Shuffle the cards } }import Foundation struct Card { var isFaceUp = false var isMatched = false var identifier: Int static var identifierFactory = 0 static func getUniqueIdentifier() -> Int { identifierFactory += 1 return identifierFactory } init(){ self.identifier = Card.getUniqueIdentifier() } }