Match Card Game Lesson 5

@Optikz
Hi Jason,
Sorry about the delay in responding. I have downloaded the project and will have a look while you sleep.
:rofl:

@Optikz
Hi Jason,

Here’s the updated project. Hope you can download it successfully.

It took me a while to figure out the issue but in order to make is work I re-created the collectionView in storyboard and connected up everything. There was an error in didSelectRowAt insofar as the logic test for card.isFlipped.

What you had was:
if card.isFlipped == true { whereas it needed to be if card.isFlipped == false {
(You might have changed the initial if statement for testing purposes)

So this is the updated code you will see in the project.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    print("Cell is tapped: \(indexPath.row)")
    
    let cell = collectionView.cellForItem(at: indexPath) as! CardCollectionViewCell
    
    let card = cardArray[indexPath.row]
    
    if card.isFlipped == false {
        
        cell.flip()
        //set the status of the card
        card.isFlipped = true
        
    } else {
        
        cell.flipBack()
        //set the status of the card
        card.isFlipped = false
    }
    // Flip the card 
    // cell.flip()
}

You will note that I have commented out the last cell.flip(). You probably had there there for testing purposes.

I also fixed the issue with the collectionView background colour needing to be clear. That change was in storyboard so you’ll see that as you look around.

Hope everything goes well for you for the rest of the project.

Much appreciated Chris! I downloaded your code and it works. I compared it line by line with mine and mine still doesn’t work… lol. Thank you for the help though! I’ll use yours to finish the project and see if I can at least get a finished working game. One thing that frustrates me is when I debug mine and put a breakpoint in on line 56 in the viewController (same spot as your, same line of code) mine won’t get to that point. I’m at a lose. Starting out with no knowledge of coding isn’t helping, hopefully I can figure this out.

@Optikz

I think the reason that breakpoint was not triggering is because there was something amiss with the UI (in storyboard). By me rebuilding the collectionView, adding the imageViews for the cards and adding all the constraints again solved that problem. I don’t know exactly where the problem was but clearly rebuilding it solved the issue.

It’s probably why guys like Sean Allen like to build the entire UI in code.

I followed a course from Paul Hudson called 100 Days Of Swift (I highly recommend that course to anyone as you will learn so much) and in that we were set a challenge (1 of the many challenges) to create a game called Hangman and do it completely in code. It worked really well but there was a lot of code to write.

He created a new course in early October - 100 Days of SwiftUI - which I have also completed. Yet another brilliant course.

Funny you mention Paul Hudson. I just started going through that course about 4 or 5 days ago. It felt like I was missing some things from what I’ve watched from Code with Chris. It’s not bad but maybe another course will solidify a few things.

I think coding is going to be such a valuable skill I the future, I mean it is now but only getting more so… I’m 42 so it may be a little more of a challenge for me compared to a 20 year old but I’m going to keep pressing forward. Recently got my pilots license so feel this is just another good challenge. :slight_smile:

I’m 66 so you are never too old to learn something new.

I started this iOS journey back in July 2018 so it’s been an amazing ride so far. If you eventually have a crack at SwifUI you will love how easy it is to create something with very few lines of code.

You’re definitely right about that!
Will definitely check out swiftUI after Swift 100days.
Do you have any apps in the App Store? What was your motivation for learning to code?

Nothing in the App Store as yet because I have not come up with something worth while that isn’t already there or something that what you would consider as being a ‘point of difference’ as it were or better than what there is already.

Motivation: Just wanted to keep my brain active now that I’m retired.

My current project is this:

That’s awesome Chris. As I learn maybe you’ll be willing to help me with my idea for a couple of apps for my work. I’m a critical care paramedic with our air ambulance service in Ontario. We are behind on the tech front when it comes to our medical directives and medication books that we carry and reference. I’ll have to pull info from PDF files and organize it and find an intuitive way to interact with it to find info quickly. It’s one reason why I decided to finally start learning to code, the organization is pretty slow at moving forward with some things being a private none profit organization and all.