Restart of Match Game

Does anyone have suggestions how to restart the game tapping on OK of the alert message?
Apparently has already been asked but I don’t find any message in the community.
F

Hi Fillppo,

All I did was create a Bool called newgame and set the Bool to true at the end of the current game. Then added a condition to start game button to reset everything.

I also have three time levels the user can choose.

if newgame {
            
            model = CardModel()
            cardAarry = [Card]()
            cardAarry = model.getCards()
            seconds = gameChoice //user chooses one of three time levels.
        }

Blessings,
—Mark

1 Like

Hi Mark,
thank you for your help.
Sorry but I’m a real beginner and I’m not able to implement your hint.
As soon as I put the line
if new game {
I have the following message error

Where I have to start?
F

Hi Filippo,

No worries, I am too!

You need to create a class variable var newGame = false

After the game completes, you set the variable newGame to true and it will run your startup code again, above.

Blessings,
—Mark

1 Like

also remove your "if(newgame { " its outside of any function thats why its causing an error

you can also just quick fix the missing protocol by letting xcode handle it

Why will it run your restart code again?
How does it know to do that, from a single variable?

I have this code in my end game section, but it does nothing.
Yes it resets the ARRAY not the Aarry but it doesn’t reload all the cards and start again?

Hi @Kevin_Stevenson Welcome to the community!

In the Start button that begins the game, I have the following code to reset:

if newgame {
            
            model = CardModel()
            cardAarry = [Card]()
            cardAarry = model.getCards()
            seconds = gameChoice
        }

This get the wheels back in motion.
Blessings,
—Mark

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.