Brenn Palma's profile

Swift Memory Game

Memory Card Game
For this card game, I inserted...
- 12 buttons that will act as the cards
- A button to shuffle the cards
- A text label to tell you whether you won or not.
This is what the screen would look like when you finish a round of the game.
For this first bit of code, I stated all of the global variables and constants that I would need to keep track. 

These variables and constants...
- keeps track of the number of pairs you matched and the total number of pairs
- the images for each face value 
- a list of paired numbers that keep track of the face values
- keeps track of the first card flipped and it's face value ( same for the second card)
- tell whether the first card has been flipped

The first strip of code is connected to the text label.

The next code is connected to the Shuffle button. 
The code ensures that all the cards are faced down, contain a random face value, and is enabled every time the user taps the shuffle button. It also resets the player progress.
(The face value is always from the ButtonIDs list)

The last strip of code holds the collection of cards as UIButtons.
This is the action function that is connected to the 12 cards. This function is called whenever the buttons are tapped. I will breakdown what each section does.
This first section of the CardTapped function is where the variables NumberOfButton and Card is created. 

The NumberOfButton keeps track of the index from which the card that was tapped is in the buttonArray list.

The Card variable keeps track of the UIButton itself for future reference inside the function.
This next part of the CardTapped function senses whether the first card has been flipped. 

If the first card is not flipped, it stores the index and UIButton as the FirstCardValue and FirstCard respectively. It then makes the FirstCardflipped true so the next time a card is tapped, it will do the next set of code instead.

However, if the first card is already flipped, it will run the same process but it will store the values as SecondCardValue and SecondCard respectively.
When the second card has been flipped, this code senses if the images of the first card and second card. If the images match, the current pairs is increased by one and the two cards are disabled so the player can't keep tapping them.

The second condition scans if the current pairs match the total pair of cards. If so, it finishes the game.

Else, if the cards don't match, the system will wait for 600 milliseconds and continue with the code.
The following code reads that both of the flipped cards will be flipped back and enabled.
This last bit of the CardTapped function. This resets the FirstCardFlipped to ensure that the next card chosen will count as the "FirstCard" for the next pairing process.
This is the built-in function that tells the program that when the view loads, then the view did load. I was originally supposed to call an independent shuffle function here but since I moved the shuffle to the @IBAction ShuffleButton, it shuffled whenever the view loads.
Swift Memory Game
Published:

Swift Memory Game

A small assignment for my coding class.

Published:

Tools

Creative Fields