Feel free to follow me on twitter

CS193P – Assignment 3 – Extra Tasks

Posted on Sep 1, 2013

So onto the extra tasks as part of Assignment 3.

Extra Task 1

“Animate the removal of matched cards. UICollectionView will do this for you if you call the deleteItemsAtIndexPaths: method. Just remember that at any time you call deleteItemsAtIndexPaths:, the UICollectionView’s dataSource (your Controller on behalf of your Model) must be in the state that will exist after the deletion happens. Otherwise you will crash with an assertion in the delete (because it will delete the items and try to reset everything and the new state of the UICollectionView will not match its dataSource’s idea of things). You can animate the dealing of 3 new cards as well using insertItemsAtIndexPaths: (though that animation is just “dissolving in” the new cards … deletion is far more exciting).”

Our set card game already does this, you could have also just called reloadData once your model has been adjusted. I used this implementation for the adding of 3 cards.

Extra Task 2

“Let the user choose how many cards to deal in the Playing Card game. Set is standardized to 12 cards to start a game, but the Playing Card game is flexible.”

For this I added a further slider under the settings menu allowing the user to choose the number of cards initially dealt within the playing card game. For this you’ll need a new numberOfPlayingCards property along with defining a new NUMBER_OF_PLAYINGCARDS key.

Extra Task 3

“Add a section (or sections) to your UICollectionView to show “found matches.” In other words, the user can scroll down in the UICollectionView (below the game) and see all of the matches they’ve found so far in the current game. The actual cards should appear (perhaps miniaturized, perhaps not, up to you). You will likely want to create a new UICollectionViewCell with 2 or 3 instances of a custom UIView subclass (that you’ve already written) as subviews and maybe some nice adornment.”

For this I added a new section to the collection view to hold the matched cards along with adding a header to split off the section.  I added an NSMutableArray to hold the matchedCards which is filled each time a matched card is flipped. I then used these cards to populate the second section of the UICollectionView.

Extra Task 4

“You could add better score-keeping to the Set part of this application if you can figure out an algorithm for calculating whether a Set exists in the current cards in play. Then you can penalize the user not only for mismatches, but for clicking the “deal 3 more cards” button if he or she missed a Set. You’d also know when the game was “over” (because the user would click on “deal 3 more cards” and there would be no more cards in the deck and no more Sets to choose).”

Here I created a public method in the card-matching-game model which returns the first set of matching cards. The first possible combination of cards is just the first cards. I then generated an array of their indexes with a simple for loop. Then I checked if the combination is valid (e.g. contains no unplayable card) and separated the first card and from the others using helper functions to check if they match. If they do, I returned those cards otherwise I got the next possible combination, and start again with the checks.

Extra Task 5

“Knowing how to find Sets in the remaining cards also would allow you to let the user cheat. Have a button that will show them a Set (if available). It’s up to you how you want to show it, but maybe some little indicator (a star or something) on each of the 3 cards?”

Here I added a ‘cheat’ button to the setGameView, which when pressed populates an array of matchingCards. I then called updateUI and added a couple of helper functions that are called from within updateUI to add a little star in the corner of the cards that match if there are any matches remaining.

Again the full game code, including the completed additional tasks can be found on my  GitHub.