iOS Development

ios – I’m creating an card matching sport however the playing cards aren’t flipping round and I wish to show the pairs created

Spread the love

That is the code that I’ve up to now. It doesn’t have any errors however the playing cards will not flip round. They’re within the property and are related. I do not why they don’t seem to be. I additionally wish to understand how I’d show the variety of pairs made when the two playing cards are matched.

import UIKit
import AVFoundation

class FirstViewController: UIViewController {
    // MARK: - IBOutlets
    @IBOutlet weak var c0: UIButton!
    @IBOutlet weak var c1: UIButton!
    @IBOutlet weak var c2: UIButton!
    @IBOutlet weak var c3: UIButton!
    @IBOutlet weak var c4: UIButton!
    @IBOutlet weak var c5: UIButton!
    @IBOutlet weak var c6: UIButton!
    @IBOutlet weak var c7: UIButton!
    @IBOutlet weak var c8: UIButton!
    @IBOutlet weak var c9: UIButton!
    @IBOutlet weak var c10: UIButton!
    @IBOutlet weak var c11: UIButton!
    @IBOutlet weak var timerLabel: UILabel!
    @IBOutlet weak var numberOfPairsLabel: UILabel!
    // MARK: - Variables and Constants
    var buttonsArray = [UIButton]()
    var solutions = [Int]()
    var faceDown = [true, true, true, true, true, true, true, true, true, true, true, true]
    var matchedCards = [false, false, false, false, false, false, false, false, false, false, false, false]
    var randCard = -1
    var cardDrawn  = -1
    var numFaceUp = 0
    var disabledCards = false
    var numberOfPairs = 0
    var participant: AVAudioPlayer!
    var timeRemaining = 30
    var timer = Timer() //variable sort is timer
    @objc func countDown (){
        timeRemaining = timeRemaining - 1
        timerLabel.textual content = String(timeRemaining)
        if timeRemaining == 0 {
    } // finish countDown perform
    func GameOver () {
        performSegue(withIdentifier: "popUpView", sender: self)
    @IBAction func cardClicked(_ sender: UIButton) {
        cardDrawn = Int(sender.restorationIdentifier!)!
        //if card is at present facedown, then present card quantity that matches drawn card
        if (faceDown[cardDrawn] == true){
            buttonsArray[cardDrawn].setBackgroundImage(UIImage(named: String(solutions[cardDrawn])), for: .regular)
            faceDown[cardDrawn] = false
        //if card is face-up then present again of the cardboard
            buttonsArray[cardDrawn].setBackgroundImage(UIImage(named: "again"), for: .regular)
            faceDown[cardDrawn] = true
        //verify what number of playing cards are at present confronted up
        numFaceUp = faceDown.filter({$0 == false}).rely
        if numFaceUp == 2{
            disabledCards = true
        //if 2 playing cards are face-up then disable all different playing cards untill they're bith face down once more or a match is discovered.
        if disabledCards == true {
            var flippedCardFound = false
            var cardPos1 = -1
            var cardPos2 = -1
            if numFaceUp == 2{
                //is card at i is facedown then disable it.
                for i in 0...11{
                    if (faceDown[i] == true)
                        buttonsArray[i].isUserInteractionEnabled = false
                    //if card is face up file its place
                    if (faceDown[i] == false && flippedCardFound == false){
                        cardPos1 = i
                        flippedCardFound = true
                    //if te seoncd card is face up file its place
                    else if (faceDown[i] == false && flippedCardFound == true){
                        cardPos2 = i
            if ((cardPos2 != -1) && (solutions[cardPos1]==solutions[cardPos2])){
                matchedCards[cardPos1] = true
                matchedCards[cardPos2] = true
                buttonsArray[cardPos1].isUserInteractionEnabled = false
                buttonsArray[cardPos2].isUserInteractionEnabled = false
                numFaceUp = 0
                if (matchedCards.filter({ $0 == true}).rely) == 12{
                    let alertController = UIAlertController(title: "You Win!", message: "Congrats!", preferredStyle: UIAlertController.Fashion.alert)
                    alertController.addAction(UIAlertAction(title: "Okay", type: UIAlertAction.Fashion.default,handler: nil))
                    self.current(alertController, animated: true, completion: nil)
        //if variety of face-up playing cards is again to 0 then reenable unmatched playing cards.
        if numFaceUp == 0 {
            disabledCards = false
            faceDown = [true, true, true, true, true, true, true, true, true, true, true, true]
            for i in 0...11{
                if matchedCards[i] == false{
                    buttonsArray[i].isUserInteractionEnabled = true
        if timeRemaining == 0 {
    @IBAction func newGameAction(_ sender: UIButton) {
    func newGame (){
        timerLabel.textual content = "30"
        timeRemaining = 30
        timer = Timer.scheduledTimer(timeInterval: 1, goal: self, selector: #selector(self.countDown), userInfo: nil, repeats: true)
        disabledCards = false
        numFaceUp = 0
        faceDown = [true, true, true, true, true, true, true, true, true, true, true, true]
        matchedCards = [false, false, false, false, false, false, false, false, false, false, false, false]
        //this loop units the background of every button to "again and permits every button.
        for i in 0...11{
            buttonsArray[i].isUserInteractionEnabled = true
            buttonsArray[i].setBackgroundImage(UIImage(named: "again"), for: .regular)
        //removes all solutions from solutions array in order that new ones will be added at first of every sport
        //this array shall be used to create the array for the random card draw
        //every quantity is in there twice to symbolize the identical card twice
        var nums = [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]
        //this loop randomely selects a quantity kind the nums array after which appends that quantity to the following
        //accessible positions within the solutions array. That quantity is then faraway from the nums array in order that it can't be used twice
        for i in 0...11{
            let randCard = Int(arc4random_uniform(UInt32(nums.rely)))
            //[2, 1, 4]
            nums.take away(at: randCard)
            //[0, 1, 3, 5, 0, 2, 3, 4, 5]
    override func viewDidAppear(_ animated: Bool) {
        buttonsArray = [c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11]
    //if {
    //numberOfPairs = numberOfPairs + 1
    //numberOfPairsLabel.textual content = (numberOfPairs)
    // MARK: - IBActions and Features
    override func viewDidLoad() {
        // Do any extra setup after loading the view.

Leave a Reply

Your email address will not be published. Required fields are marked *