Welcome to iD Princeton!

Get ready to learn some awesome iPhone programming this week!

I'm Scrabble and I will be your fearless tour guide, but this website is here for you to refer back to at any time!

Code for Friday morning!

1-JJ-Memory.zip

2-Jacob-SurfboardWeb.zip

4-Thomas-RPSv2.zip

5-Sanath-Hangman.zip

6-Logan-MilkCloud.zip

7-Annie-EggPet.zip

8-Tommy-PassGen.zip

Rock, Paper, Scissors!

Rock, Paper, Scissors!

Finally, you no longer have to bug your friends when you get your weekly RPS urge...now you can play against your phone!

Using our knowledge of random numbers, if statements, and the iPhone's amazing and lovable Buttons/IBActions and Labels/IBOutlets, we're going to make a Rock Paper Scissors app.

It's not easy, but you should know everything that you need to make it by now. Try to do as much as you can before asking me for help teeny tiny hints...it'll make it THAT much better!

Here are the minimum requirements: (feel free to have more, obviously!)

  1. 3 buttons: Rock, Paper, and Scissors. (AND YOU BETTER SPELL SCISSORS CORRECTLY NOW FOR THE REST OF YOUR LIFE...NO EXCUSES!)
  2. 3 text labels: one saying your choice, one saying computer's choice, and one saying the winner. (Paper beats rock, rock beats scissors, scissors beats paper. Just in case.)
  3. 2 image views: one representing your choice and one representing your phone's choice. (Images are here: hand-rock.png, hand-paper.png, hand-scissors.png — and they're all a perfect square, so no need for Photoshop. I made my Image Views 140x140 but see what works for you.)
  4. MAKE IT PRETTY! I'll show you mine so you know how it works, but mine is uuuuuugly. I know you can do better. If you're "done," make sure you have an icon and a loading screen, and an appropriate name under the icon!
  5. Bonuses:
    1. Keep track of the number of wins, losses, and ties, and show them.
    2. Add sound effects after the phone's turns so it feels like you can hear the computer playing.
    3. Let the user choose the number of matches in a round, and figure out when there needs to be tiebreakers to determine the winner of the round. A "best out of three" round is almost never exactly three matches, unless one person is really unlucky that day! I'll help you with this only if you're done with everything else!
    4. Can you think of any that I forgot? I like surprises!

I'll post some code right here at exactly 3:30 PM...but NO COPY AND PASTE!

Until then, try to only use your memory, your past projects, your neighbor, or the internet for research/help. I promise you'll thank me later when you all have a full working game and you can say you did it yourself!

Setting the image property of an IBOutlet UIImageView

imageComp.image = [UIImage imageNamed:@"hand-rock.png"];

Sample ViewController.h file

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

    int compChoice; // 0 = Rock, 1 = Paper, 2 = Scissors

    IBOutlet UIImageView *imageYou;
    IBOutlet UIImageView *imageComp;

    IBOutlet UILabel *labelYou;
    IBOutlet UILabel *labelComp;

    IBOutlet UILabel *labelWinner;

}

- (IBAction)pressRock:(id)sender;
- (IBAction)pressPaper:(id)sender;
- (IBAction)pressScissors:(id)sender;

@end

Sample pressRock method

- (IBAction)pressRock:(id)sender {

    // user chose rock, let's see what the computer chooses...

    compChoice = (arc4random() % 3); // generates number from 0 to 2, including 0 and 2 (in other words: 0, 1, or 2)

    labelYou.text = @"Rock";
    imageYou.image = [UIImage imageNamed:@"hand-rock.png"];

    if(compChoice == 0) {
        labelComp.text = @"Rock";
        imageComp.image = [UIImage imageNamed:@"hand-rock.png"];
        labelWinner.text = @"It's a tie!";
    } else if(compChoice == 1) {
        labelComp.text = @"Paper";
        imageComp.image = [UIImage imageNamed:@"hand-paper.png"];
        labelWinner.text = @"Computer wins!";
    } else if(compChoice == 2) {
        labelComp.text = @"Scissors";
        imageComp.image = [UIImage imageNamed:@"hand-scissors.png"];
        labelWinner.text = @"You win!!!";
    } else {
        NSLog(@"Oops, something went terribly wrong with the computer's choice!");
    }

}

Random

We have one more sample project before we get into making our first "REAL" game!

This app will give your user a random number on their screen (in a Label) when they press a button, so we can learn how to change a label's property (and any other item's property) through the code with something called delegates (which is also a cool noun to use when you go home...look it up in the dictionary!).

Random

ViewController.h — header file

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    int myNumber;                 // Tell our phone to expect us to set the "myNumber"
                                  // variable equal to some integer.
                                  // Math class refresher... ints = FULL numbers
                                  //                                (NO DECIMALS!)

    IBOutlet UILabel *myLabel;    // Tell our phone to expect us to control the
                                  // LABEL on our ViewController.xib which will
                                  // display our random number variable!
}

- (IBAction)pressGenerate:(id)sender;     // Tell our phone to expect a method that we'll
                                          // link to our button's "touch up inside" event.
                                          // This should look pretty familiar!

@end

ViewController.m — implementation method (after @implementation ViewController .... and before @end)

- (IBAction)pressGenerate:(id)sender {
    myNumber = (arc4random() % 100 - 1 + 1) + 1;  // Generates a random number from 1 to 100
                                                  //          (including 1 and 100).

                                                  // The formula is ... (arc4random() % (high - low + 1)) + low
                                                  // So if you wanted random numbers between 18 and 53, you
                                                  // would do:     number = (arc4random() % (53 - 18 + 1)) + 18

                                                  // ...Easy, right? :) We'll talk about this more today.


    // We told the phone to expect us to change the "myLabel" label on our view controller.
    // Now we're doing it!

    myLabel.text = [NSString stringWithFormat:@"%i", myNumber];

    // IMPORTANT NOTE: We can't just set myLabel.text equal to myNumber, our integer, because
    // labels always need a STRING for their text!
    // The second half of that line converts myNumber into a string that myLabel can understand.
}

Noisy

Let's fill the halls of iD Princeton with our beautiful music! Right click and "save" here to download the image ...and here for the best 8-second MP3 of your life.

Noisy

ViewController.h — header file

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController {
    // We keep track of the audio player here on the class
    AVAudioPlayer *audioPlayer;
}

// Our UIButton clicked methods
- (IBAction)playClicked:(id)sender;
- (IBAction)stopClicked:(id)sender;

ViewController.m — implementation file

- (IBAction) playClicked:(id)sender {
    // Open a music file that's been added to the project, called lady.mp3
    NSString * mp3Path = [NSString stringWithFormat:@"%@/lady.mp3", [[NSBundle mainBundle] resourcePath]];
    NSURL *url = [NSURL fileURLWithPath:mp3Path];

    // create an AudioPlayer with the file used above
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if (audioPlayer == nil)
        NSLog(@"Error playing sound %@", [error description]);
    else
        [audioPlayer play];
}

- (IBAction)stopClicked:(id)sender {
    [audioPlayer stop];
}

Beautiful

Our first button...the start of a beautiful relationship. Click to open the image.

Beautiful

AlertView snippet

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Welcome!"
                                                message:@"Enjoy the app!"
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Button1", @"Button2", nil];
[alert show];

ScrabbleFlip

This app requires NO CODE! Thanks, Xcode!

ScrabbleFlipScrabbleFlip

I'll start putting these up right after the first person finishes their Rock Paper Scissors game above. It's not a race, though...we have all week for these!

If the week has ended and you're bored in a town that's not called Princeton right now (booooo), these are some AMAAAAAAAAZING sites to keep your momentum going throughout the summer and beyond!

Or, if I'm really busy helping other people in class and you need something new for your project, I guarantee there's at least a dozen things here that you never thought of!

Scrabble also doesn't know everything...crazy, right?!?! I won't lie to you and tell you that I know something, but I will ABSOLUTELY help you learn how to understand these sites and the people who write them.

Without further ado...

  • Official Apple documentation — It kinda stinks, but sometimes it's best to hear stuff straight from the horse's mouth if you already know what you're looking for.
  • Ray Wenderlich's tutorials — Some AMAZING tutorials about both the basics and the latest and greatest parts of iOS, including Twitter integration, maps, and iCloud...save this for when you're "done!"
  • iOS 5 By Tutorials — ZIP download of Ray Wenderlich's great tutorials with examples
  • Learn Objective-C in 24 Days — Really good if it's been a while since you've done any type of programming. We all need a refresher sometimes!
  • Tuts+ "Beginning" iOS Tutorials — Great video tutorials that go sooooo in-depth that I'm not sure why they're all labeled "beginning" iOS! Perfect to sit back and watch if your hands need an Xcode break or your ears need a Scrabble break.
  • Videos of Stanford's iPhone course on iTunes U — Get a taste of college from the best of the best!

Are we coding in iOS? Java? C++? Nope!

iOS apps are powered by the same programming languages used to create Mac apps since the first version of OS X in the 90's.

It's called Objective-C and it looks a little scary at first...but really, it's not too different from the languages you already know.

Once you know one programming language, the hard part of learning your next programming language is already out of the way.

So while the code might look different and scary, don't be afraid to try and relate everything I say to things you already know, and please remind me if I forget to do that. Objective-C is still pretty new to me (and most iPhone developers) and I still love Java and C++, so we'll be talking a lot about those too, even if we're not coding in them!

So, what do we use to write Objective-C? Thanks, Apple!

We'll be using a shiny Apple app called Xcode!

Even though it's already in its fourth version, it definitely still has bugs.

As beautiful as it is, you'll love to hate it, and at some point you won't believe me when I tell you it was made by Apple.

Fortunately, Xcode saves all your code for you every time you run your app. So if you haven't tested in a while...

  1. You should be testing.
  2. SAVE!

Can we use the iOS 6 beta? Pleeeeeeeease?

No, no, NO!

We'll be using iOS 5.1.x for ALL of our apps.

Trust me — I just put iOS 6 on my iPhone and it's definitely not ready yet. And there's no going back.

I'll let you play with mine all week, but if you want your iPhone to still make phone calls or your iPod to still play music, wait for the suckers like me to finish dealing with all the bugs first!

Download Xcode
Download Xcode at Home