O'Reilly logo

Swift 2 By Example by Giordano Scalzo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Finishing the game

In this section, we will finally be able to play the game.

Implementing the game logic

After having all the required functions in place, it's now a straightforward task to complete the game. First of all, we add the instance variables to hold the number of the pairs already created, the current score, and the list of selected cards turned up:

private var selectedIndexes = Array<NSIndexPath>()
private var numberOfPairs = 0
private var score = 0

Then, we apply the logic when a card is selected:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { if selectedIndexes.count == 2 || selectedIndexes .contains(indexPath) { return } selectedIndexes.append(indexPath) let cell = collectionView.cellForItemAtIndexPath(indexPath) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required