-
-
Save sandersnewmedia/84f1e1026d4201fa6bc1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation GameViewController | |
@synthesize board, oponentsViewController, instructionsViewController, imgPicker, gameView, computerScore, computerLabel, humanScore, humanPortrait, computerPortrait, changeLevelBtn, instructionsBtn, boardMatrix, pieceMatrix; | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// create loading screen with red background | |
UIView *loader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]; | |
loader.backgroundColor = [UIColor redColor]; | |
// add loading screen to view | |
[self.view addSubview:loader]; | |
// construct operation queue | |
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; | |
NSInvocationOperation *initOponentsViewController = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(initOponentsViewController) object:nil]; | |
NSInvocationOperation *initImgPicker = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(initImgPicker) object:nil]; | |
NSInvocationOperation *initInstructionsViewController = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(initInstructionsViewController) object:nil]; | |
NSInvocationOperation *buildBoardMatrix = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(buildBoardMatrix) object:nil]; | |
NSInvocationOperation *buildPieceMatrix = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(buildPieceMatrix) object:nil]; | |
NSInvocationOperation *buildGameState = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(buildGameState) object:nil]; | |
[buildPieceMatrix addDependency:buildBoardMatrix]; | |
[buildGameState addDependency:initOponentsViewController]; | |
[buildGameState addDependency:buildBoardMatrix]; | |
[buildGameState addDependency:buildPieceMatrix]; | |
[queue addOperation:initOponentsViewController]; | |
[queue addOperation:buildBoardMatrix]; | |
[queue addOperation:buildPieceMatrix]; | |
[queue addOperation:buildGameState]; | |
[queue addOperation:initImgPicker]; | |
[queue addOperation:initInstructionsViewController]; | |
[initOponentsViewController release]; | |
[buildBoardMatrix release]; | |
[buildPieceMatrix release]; | |
[buildGameState release]; | |
[initImgPicker release]; | |
[initInstructionsViewController release]; | |
// pause main thread until all operations finish | |
[queue waitUntilAllOperationsAreFinished]; | |
[queue release]; | |
// remove loading screen | |
[loader removeFromSuperview]; | |
[loader release]; | |
} | |
- (void)initImgPicker | |
{ | |
imgPicker = [[UIImagePickerController alloc] init]; | |
imgPicker.allowsEditing = YES; | |
imgPicker.delegate = self; | |
} | |
- (void)initOponentsViewController | |
{ | |
oponentsViewController = [[OponentsViewController alloc] init]; | |
oponentsViewController.delegate = self; | |
} | |
- (void)initInstructionsViewController | |
{ | |
instructionsViewController = [[InstructionsViewController alloc] init]; | |
instructionsViewController.delegate = self; | |
} | |
- (void)buildBoardMatrix | |
{ | |
boardMatrix = [[NSMutableArray alloc] init]; | |
// build BOARD_SIZE * BOARD_SIZE array of cells for the board | |
for (int i = 0; i < BOARD_SIZE; i++) { | |
NSMutableArray *boardRow = [[NSMutableArray alloc] init]; | |
for (int j = 0; j < BOARD_SIZE; j++) { | |
Cell* boardCell = [[Cell alloc] initWithRow:i Column:j]; | |
[gameView addSubview:boardCell]; | |
[boardRow addObject:boardCell]; | |
[boardCell release]; | |
} | |
[boardMatrix addObject:boardRow]; | |
[boardRow release]; | |
} | |
} | |
- (void)buildPieceMatrix | |
{ | |
pieceMatrix = [[NSMutableArray alloc] init]; | |
// build BOARD_SIZE * BOARD_SIZE array of cells for the pieces | |
for (int i = 0; i < BOARD_SIZE; i++) { | |
NSMutableArray *pieceRow = [[NSMutableArray alloc] init]; | |
for (int j = 0; j < BOARD_SIZE; j++) { | |
Cell* pieceCell = [[Cell alloc] initWithRow:i Column:j]; | |
[gameView addSubview:pieceCell]; | |
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userMove:)]; | |
tap.numberOfTapsRequired = 1; | |
[pieceCell addGestureRecognizer:tap]; | |
[tap release]; | |
pieceCell.userInteractionEnabled = YES; | |
[pieceRow addObject:pieceCell]; | |
[pieceCell release]; | |
} | |
[pieceMatrix addObject:pieceRow]; | |
[pieceRow release]; | |
} | |
} | |
- (void)buildGameState | |
{ | |
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [path objectAtIndex:0]; | |
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Game.plist"]; | |
// check for game data in app's documents | |
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { | |
// resume game from file | |
NSData *data; | |
NSKeyedUnarchiver *unarchiver; | |
data = [NSData dataWithContentsOfFile:filePath]; | |
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; | |
board = [[MainBoard alloc] initWithCoder:unarchiver andBoardMatrix:boardMatrix andPieceMatrix:pieceMatrix]; | |
[unarchiver finishDecoding]; | |
[unarchiver release]; | |
[self updateScores]; | |
} | |
else { | |
// first game ever, sets difficulty to level 3 in Mainboard.m | |
board = [[MainBoard alloc] initWithBoardMatrix:boardMatrix andPieceMatrix:pieceMatrix]; | |
} | |
[self changeLevel:[board difficultyLevel]]; | |
//board.delegate = self; | |
} | |
- (IBAction)startOver | |
{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Are You Sure?" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil]; | |
[alert show]; | |
[alert release]; | |
} | |
- (void)updateScores | |
{ | |
humanScore.text = [NSString stringWithFormat:@"%i", board.scoreHuman]; | |
computerScore.text = [NSString stringWithFormat:@"%i", [board getScoreComputerAtIndex:board.difficultyLevel]]; | |
} | |
- (void)saveGame { | |
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [path objectAtIndex:0]; | |
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Game.plist"]; | |
NSMutableData *data; | |
NSKeyedArchiver *archiver; | |
BOOL result; | |
data = [NSMutableData data]; | |
archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; | |
[archiver setOutputFormat:NSPropertyListXMLFormat_v1_0]; | |
[board encodeWithCoder:archiver]; | |
[archiver finishEncoding]; | |
result = [data writeToFile:filePath atomically:YES]; | |
[archiver release]; | |
} | |
- (void)userMove:(UIGestureRecognizer*) sender | |
{ | |
if ([sender.view isKindOfClass: [Cell class]]) | |
{ | |
Cell* c = (Cell*)(sender.view); | |
// make sure user won't double-tap when we release the tap object | |
c.userInteractionEnabled = NO; | |
sender.view.userInteractionEnabled = NO; | |
[board userMoveInRow: c.row column: c.col]; | |
sender.view.userInteractionEnabled = YES; | |
} | |
[self updateScores]; | |
[self saveGame]; | |
} | |
- (IBAction)choosePhoto | |
{ | |
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Nevermind" otherButtonTitles:@"Browse Library", nil]; | |
// if device has camera | |
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { | |
[popupQuery addButtonWithTitle:@"Take Photo"]; | |
} | |
popupQuery.actionSheetStyle = UIActionSheetStyleDefault; | |
[popupQuery showInView:self.view]; | |
[popupQuery release]; | |
} | |
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex | |
{ | |
switch(buttonIndex) | |
{ | |
case 0: | |
return; | |
case 1: | |
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; | |
break; | |
case 2: | |
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera; | |
break; | |
} | |
[self presentModalViewController:self.imgPicker animated:YES]; | |
} | |
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo { | |
humanPortrait.image = img; | |
[[picker parentViewController] dismissModalViewControllerAnimated:YES]; | |
} | |
- (IBAction)toggleInstructionsModal | |
{ | |
[self presentModalViewController:instructionsViewController animated:YES]; | |
} | |
- (void)didTapToCloseInstructions | |
{ | |
[self dismissModalViewControllerAnimated:YES]; | |
} | |
- (IBAction)toggleOponentsModal | |
{ | |
[self presentModalViewController:oponentsViewController animated:YES]; | |
} | |
- (void)didTapToChangeLevel:(int)level | |
{ | |
if ([self.modalViewController isMemberOfClass:[OponentsViewController class]]) { | |
[self dismissModalViewControllerAnimated:YES]; | |
} | |
[self changeLevel:level]; | |
[board newGame]; | |
[board drawBoard]; | |
} | |
- (void)changeLevel:(int)level { | |
[board setDifficultyLevel:level]; | |
computerLabel.text = [[oponentsViewController getOponentByLevel:level] name]; | |
UIImage *image = [UIImage imageNamed:[[oponentsViewController getOponentByLevel:level] imageName]]; | |
[computerPortrait setImage:image]; | |
[self updateScores]; | |
NSLog(@"level: %i", [board difficultyLevel]); | |
} | |
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { | |
if ([@"Cancel" isEqual:[alertView buttonTitleAtIndex:buttonIndex]]) { | |
return; | |
} | |
if ([@"Yes" isEqual:[alertView buttonTitleAtIndex:buttonIndex]] || [@"Play again" isEqual:[alertView buttonTitleAtIndex:buttonIndex]]) { | |
[board newGame]; | |
} | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
// Releases the view if it doesn't have a superview. | |
[super didReceiveMemoryWarning]; | |
// Release any cached data, images, etc that aren't in use. | |
} | |
- (void)viewDidUnload { | |
// Release any retained subviews of the main view. | |
// e.g. self.myOutlet = nil; | |
gameView = nil; | |
} | |
- (void)dealloc | |
{ | |
[board release]; | |
[oponentsViewController release]; | |
[instructionsViewController release]; | |
[imgPicker release]; | |
[gameView release]; | |
[computerScore release]; | |
[computerLabel release]; | |
[humanScore release]; | |
[humanPortrait release]; | |
[computerPortrait release]; | |
[changeLevelBtn release]; | |
[instructionsBtn release]; | |
[boardMatrix release]; | |
[pieceMatrix release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment