• Home   /  
  • Archive by category "1"

Stanford Cs193p Assignment 2

Matchismo is a simple card game based on assignment 2 of course CS193P “Developing Apps for iOS” from Stanford University. In this game you have to match cards (from a poker deck) by suit or by rank in order to gain points. Of course matching by rank gives you more points. You can also choose the number of cards you want to match between 2 and 3.

From an OO point of view, I have written the code in order to make the project more extensible and changeable. In particular the match method in PlayingCard.m let you match any number of cards and not only two or three. This is possible thanks to recursion, as you can see below.

Download Matchismo assignment 2 of CS193P

/// Recursive version of match. It lets you /// calculate the matching-score of two or /// more cards. /// /// Example: [1♥, 5♣, 7♣, 2♥] /// 1♥ match 5♣, 1♥ match 7♣, 1♥ match 2♥ /// 5♣ match 7♣, 5♣ match 2♥ /// 7♣ match 2♥ /// - (int)match:(NSArray *)otherCards { int score = 0; if ([otherCards count] != 0) { if ([otherCards count] == 1) { PlayingCard *otherCard = [otherCards firstObject]; if ([self.suit isEqualToString:otherCard.suit]) { return score = 1; } else if (self.rank == otherCard.rank) { return score = 4; } } else { for (Card *otherCard in otherCards) score += [self match:@[otherCard]]; PlayingCard *otherCard = [otherCards firstObject]; score += [otherCard match:[otherCards subarrayWithRange:NSMakeRange(1, [otherCards count] - 1)]]; } } return score; }

Any question? Please, leave a comment below! 🙂

Usually it doesn’t make much sense to repeat code from a lecture demo, but for the Dropit project I think it’s worth to post an article based on a slight variation using the new features of UIKit Dynamics available in iOS 9.

This is perhaps a good time to mention that all the CS193P assignment solutions so far created has been based on Swift 2.1 and iOS 9, whereas the Stanford U. lectures and demos are based on Swift 1.2 and iOS 8.

The motivation to create the variation was a result of attempting to implement assignment 5. Particularly the ball which is a UIView and ideally should be round, even though UIViews are obviously rectangles. The good news is that iOS 9 provides a collisionBoundsType property which can be easily overridden in UIView:

class SphereView: UIView { // iOS 9 specific override var collisionBoundsType: UIDynamicItemCollisionBoundsType { return .Ellipse } }

This sets the bounds of the view to be an ellipse and allows the right boundary collisions
as you might expect from a ball or sphere.

The UIFieldBehavior new in iOS 9, also seems very interesting with properties like: dragField, springField, electricField, magneticField, noiseFieldWithSmoothness and more, which opens the door to new possibilities of animation in your apps. The Dropit variation also adds the noiseFieldWithSmoothness behavior for some cool
visual effects.

With debugEnabled for UIDynamicAnimator (by adding a Swift bridging header *), you can see the noise field in action as it shifts by adding random noise. To enable UIView debugging, the following code needs to be added in the bridging header file:

@import UIKit; @interface UIDynamicAnimator (AAPLDebugInterfaceOnly) // Used in DropitViewController.swift file: // lazilyCreatedDynamicAnimator.debugEnabled = true @property (nonatomic, getter=isDebugEnabled) BOOL debugEnabled; @end

Full source code of the demo is available here at Github:
https://github.com/sanjibahmad/Variation-on-Dropit-Demo

Video Demo

For more on what’s new in UIKit Dynamics in iOS 9, please see the WWDC 2015 video: What’s New in UIKit Dynamics and Visual Effects

* Please see this article on how to add the Swift bridging header: Adding a Swift Bridging Header

Like this:

LikeLoading...

One thought on “Stanford Cs193p Assignment 2

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *