Archived: 4Score

One day, my mother was determined to make an iPhone app. She tells me of the multitude of math-based games she made as a math prof, and I of course roll my eyes being the good son that I am. Long story short: she convinces me, I procrastinate, she convinces me again, I write an iPhone app. Available here.

I wasnt quite sure how I wanted to package this math game, but I was sure about how not to package it.

First and foremost, if you’re developping for iOS, you need both an iOS device and an OS X environment on an actual OS X machine (No Hackintoshes, VMs). Heres a thread about it.

After trying several less than legal methods, I finally went out and bought an aluminum brick. Otherwise know as a Mac Mini. Another thing needed to code in iOS is Xcode, and the latest version of it. You’ll also need a developer license, renewed yearly, for the "low" cost of 99$/year.

If you find yourself not knowing diddly-squat about iOS, Objective-C, and MVC (Most Vicious Concept?) then take a look at these links:

//
//  SimpleGameViewController.h
//  Normal Game mode
//
//  Created by Default on 11-07-30.
//  Copyright 2011 Travectory Studios. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "GameMenuViewController.h"
#import "StatsViewController.h"
#import "Achievements.h"

@interface SimpleGameViewController : UIViewController <GameMenuDelegate, StatsViewDelegate>{
	UILabel IBOutlet *pointsLabel;		//Label to display points
	UILabel IBOutlet *secondaryLabel;	//Label to display win/lose
	NSInteger roll;				//numerical roll
	Boolean rolled;				//user has rolled
	NSInteger grid[16];			//numerical values in 4x4 grid
	UIButton *buttons[16];			//UIButton array of 4x4 grid
	UIView *views[16];			//UIImageView array of 4x4 grid
	UIButton *resetButton;			//game reset button
	Boolean eks[16];				//bool array of where Xs are placed
	NSString *skinPath;			//current game skin path
	NSString *skinName;			//current game skin name
	int x_scale;				//current game skin x scale
	int y_scale;				//current game skin y scale
	int x_off;				//current game skin x offset
	int y_off;				//current game skin y offset
	int button_scale;			//current game skin button size
	NSDate *startDate;			//start time
	int secondsPassed;			//elapsed seconds
	NSString* returnString;			//terrible way of messaging between views
	Achievements* achievements;		//achievements array
	int MAX_POINTS;				//max points for the game type
}
-(void)loadSkin;					//load current game skin
-(IBAction)openMenu:(id)sender;			
-(IBAction) onHit:(id) sender;			//button hit event
-(IBAction) onRoll:(id) sender;			//dice roll event
-(IBAction) onReset;				//reset event
-(BOOL) isAllGood;						//is possible to place die
-(BOOL) isGood:(int) roll XLocation:(int) x YLocation:(int) y;	//is (x,y) "good"?
-(int) getPoints;						
-(BOOL) rowIsComplete:(int) row;			//row is full?
-(BOOL) colIsComplete:(int) col;			//col is full?
-(NSString*) diceImage:(int) a;			//get current game skin dice image
@property (retain, nonatomic) IBOutlet UILabel *pointsLabel;
@property (retain, nonatomic) IBOutlet UILabel *secondaryLabel;
-(void) startTimer;
-(int) endTimer;

@end

Speaking of that code I just posted, that’s the header file for my game window. 4score has 3 game modes so far (Classic, Single Line, Double Line). Classic Mode simply uses the ViewController you see above. The other game mode inherit that class and instantiate their own ViewControllers.

My full code for SimpleGameViewController.m is here. It might not be entirely self explanatory but in making the game itself I only used a couple of techniques:

One problem I did have for awhile before learning about delegates was how to control another view directly. The pre-delegate solution was to do something like this:

- (IBAction)continueGame:(id)sender{
	NSArray* controllers = self.navigationController.viewControllers;
	GameViewController* parent = (GameViewController*)[controllers objectAtIndex:[controllers count]-1];
	[parent startTimer];
	[self dismissModalViewControllerAnimated:YES];
}

Which manhandled the navigation controller itself. Now, with delegates, we get this:

- (IBAction)continueGame:(id)sender{
    [self.delegate returnToGame:@"continue"];
}

//the other file
- (void) returnToGame: (NSString*) str{
    returnString = str;
    [self dismissViewControllerAnimated:YES completion:nil];
}

END OF POST

Evidently, I wasn’t very good at following through with my blog posts but I’ve brought this old article to my new site for nostalgia / personal history. I don’t think I’ll be updating this one as Objective-C isn’t in vogue anymore

#twolander