Developing Applications For iOS – Assignment 2: Set
Throughout lectures four and five Paul Hegarty introduced several key Foundation APIs, and discussed both the life cycle of view controllers and how to integrate multiple MVCs into an application. These concepts are covered in enough detail to tackle the next homework.
Assignment 2: Set builds on top of the Matchismo code base and requires adding a new tab bar to the application for a second game called Set. Set is a card game consisting of 81 cards that vary in four different features: number, symbol, shading, and color. The game has players looking for card sets, which are groups of three cards that either all have one feature in common while having each of the other features uniquely distinct, or all have uniquely distinct features.
The Set game must implement the same UI functionality provided in the existing card matching game, including the flip, score, and game play message labels as well as a button to re-deal all of the game cards.
This assignment also has optional requirements such as adding a third tab to track game scores, adding another tab to let the player change game settings, and integrating icons into the UI tab bar.
Solution
This version of Matchismo includes two games: Card Match and Set. Card Match is the same game implemented for Assignment 1 but fixed as a 2-card match version without a game mode selector.
The new Set game starts with 24 cards randomly chosen from a deck of 81 Set playing cards. Pressing a card selects it. There is a small score penalty each time a card is selected. Three selected cards are matched on number, symbol, shading, and color based on the Set game rules. Finding a set scores positively, while selecting three cards that do not make a set incurs a score penalty. Only matched cards become unplayable. At the top of the display there are both a flip counter and a game score. At the bottom of the display there is a game play message area as well as a control to scrub through the game messages and a button to start a new game by re-dealing a new set of cards from a new card deck.
Implementation Notes
My version of Matchismo for this assignment implements all required tasks including the addition of a UITabBar with tabs for each of the application screens; using NSAttributedString to draw the Set cards; and, providing flips, score, and game message labels as well as a “Deal” button in the Set game UI. The icons I used in the tab bar are part of the free IconBeast Lite collection by Charlene Hea on IconBeast.
For the Set game I leveraged as much logic as possible from the existing Card Match implementation. This involved generalizing CardMatchingGame to support both games without the class including game-specific play logic. Instead, each of the games’ Card class encapsulates its specific match rules.
In SetCard I defined each of the card features as enum because in my mind it made more object-oriented/readability sense. Yet, I have mixed feelings about my choice because enums are not first class objects and in certain parts of the code the only way to manipulate them is through their raw numerical values.
I implemented SetGameViewController as a kind of CardGameViewController, leveraging the later’s core functionality, and without exposing public attributes. I used custom string tokens to turn SetCards into attributed symbol strings. The implementation is a little elaborate but generic enough for rendering both the Set cards and the Set game play messages. I also used NSRegularExpression for this conversion to learn about the regular expression API.
My implementation of the Scoreboard feature is based on the code presented in lecture five with several modifications. Specifically, I changed GameResult to maintain a limited number of best scores for each game, return sorted score lists per game, and delete all saved scores. GameResultsViewController ended up being pretty trivial, but the UI is nothing to write home about. 🙂
I also added a simple Settings screen with a single control. My goal with it was to replicate the general look of settings screens from other iOS apps in a short amount of time. I may have gone a little too low-level styling things in GameSettingsViewController because I couldn’t identify obvious components in IB to group controls in a rounded panel and with engraved labels.
Source Code
The full Xcode project with my solution to Assignment 2 is available here.
Pingback: Arcanesmith » Developing Applications For iOS – Assignment 3: Graphical Set