Restart of Match Game

I have the code and i think it is resetting the game, but the removed cards are not visible after the restart. I can flip them so they are there and i can hear the sounds, but i acnnot see them, as they were removed during the game, how do i un remove them please?

are you sure that you “overwritten” your old card array?, it should reset all the values including flipped index as well

yes i set the score and then clear the arrays, and i can then click on the cards so the isFlipped is reset but the cards are not visible?

            let score = String(flipCount)
            title = "GAME OVER"
            message = "Congratulations you cleared in \(score) attempts"
            
            showAlert(title!,message)
    
            model = CardModel()
            cardArray = [Card]()
            cardArray = model.getCards()

it must be something to do with this, cos resetting the array does not undo this process…

//remove the cards
            cardOneCell?.remove()
            cardTwoCell?.remove()

ah i think i know the problem. you need to re set the alpha of the card to 1 because it became hidden

or check the codes on the “remove” function on what other values where set on the card

yes, the remove sets the alpha to 0

func remove() {
    backImageView.alpha = 0
    
    UIView.animate(withDuration: 0.3, delay: 0.5, options: .curveEaseOut, animations: {
        self.frontImageView.alpha = 0
    }, completion: nil)
}

So i need to run through all the cards and set the alpha front and back to 1… but not sure how i do that.
i assumed you had the project code, but obviously not as you don’t know how it was written, but thanks anyway i will continue to try and work it out.

i actually do im just trying to remember from memory.

how about doing a .reloadData instead maybe it will help you “reset” everything after you reset the arrays

so collectionview.reloadData()

https://developer.apple.com/documentation/uikit/uicollectionview/1618078-reloaddata

Did it…
`added a fucntion to the Card CollectionView

 func show() {
    
    frontImageView.alpha = 1
    backImageView.alpha = 1
    
}

then added a line to the ViewController

 //set the card
    cell.setCard(card)
    cell.show()

Voila, the cards are all visible

Thanks for help and suggestions we go there in the end

1 Like

Hi Filippo

The way to have a restart is to do the following

Add a function to the CardCollectionViewCell called show()

func show() {

    frontImageView.alpha = 1
    backImageView.alpha = 1
    
  }

Then add this line to the collectionView function in viewController, after the line Cell.setCard(card)

    cell.show()

Then in the function checkGameEnded add these after the alert

            model = CardModel()
            cardArray = [Card]()
            cardArray = model.getCards()

That will give you a new game after the last one was completed.

Enjoy

Hello

I did everything as described, but nothing happens when I tap OK. What am I doing wrong?
I hope you can help me.

:slight_smile:

hello, you need to have a “handler” for your UIAlertAction or else clicking OK will not do anything

Hey guys, I was having trouble following the suggestions (I really am a beginner) so after searching I found an easier way, I’m still learning so probably there’s a better way but here’s what worked for me, I modified the handler of the button in the showAlert function, and I added the restartApp function below that:

func showAlert(title:String, message:String) {

    // Create the alert
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    
    // Add a button for the user to dismiss it
    let okAction = UIAlertAction(title: "OK", style: .default, handler: {action -> Void in self.resetApp()})
    
    alert.addAction(okAction)

    // Show the alert
    self.present(alert, animated: true, completion: nil)
    
}
func resetApp() {
    cardsArray = [Card]()
    cardsArray = model.getCards()
    milliseconds = 50000
    soundPlayer.playSound(effect: .shuffle)
    timerLabel.textColor = UIColor.black
    collectionView.reloadData()
    timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(timerFired), userInfo: nil, repeats: true)
    RunLoop.main.add(timer!, forMode: .common)
}
2 Likes

Hi, This restarts the game but the timer goes in negative. Cant actually start a new game.

Can you share your code please?

sure.

in the func resetApp you are selection 5 seconds, not 50 (it should be 50,000 milliseconds). Also try to check the if statement in the timerFired function. The if == 0 should stop the timer when getting to 0

Sorry to disturb. The timer still goes in negative. and suppose if i finish the game before 5 mins and when i click on ok the game starts from reducing that 5 mins. The timer actually not starts from the beginning. and I also checked in timer method.

Right…please share your code for the timerFire function

all the code seems right, I don’t know if this is going to solve, but try to leave a space at the beginning between the parenthesis and the key.
@objc func timerFired**() {**

Thank you, thank you, thank you!
I was tearing my hair out trying to reset the game - this feels a bit like duplicated code - why can’t I just say go back to the beginning… but it works.
I feel there should be a better “model” for reset or initial behaviour - or if I have a reset function - I should call it from all places that need it.

1 Like