A Problem in Developing A "Concentration Game"

I am trying to develop the "Concentration" game following the Stanford Swift lessons on iTunes U. But I find that I can't make the card flip over. When I click the card, nothing just happens. But it's supposed to return a question mark "?". Could anybody help me out?

Here are my codes:

Replies

Your code does not show here, so hard to help.


Copy and paste at least the code where you flip the card. And post the exact reference of the Stanford course lesson.

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()
    }
}

Have you checked `updateViewFromModel()` is really called? By putting a break point or `print` statement.

Could you show all the print you get on console ?


Do you see "chosen card was not in cardBottons" ?


Have you checked also that all the buttons are connected to

@IBAction func touchCard


Note: you could simplify your chosseCard with a single statement


Replacing

    func chooseCard(at index: Int) {          
        if cards[index].isFaceUp {
            cards[index].isFaceUp = false          
        } else {
            cards[index].isFaceUp = true        
        }
    }

By

    func chooseCard(at index: Int) {           
        if index >= 0 && index < cards.count { cards[index].isFaceUp.toggle() }
    }


I propose you also to check for the validity of index, in case…

With your code, when I touch a button (that represents a card?) I can see a question mark on white background.

Touch again on the same button, it changes to orange and `?` disappears. Isn't this the expected behavior?


I guess, you are not connecting the action of the buttons.


Anyway, you should better check if all IBOutlets and button actions connected properly in your storyboard.