Source

iPhone / solarwolf / Classes / solarwolfViewController.m

The default branch has multiple heads

Full commit
//
//  solarwolfViewController.m
//  solarwolf
//
//  Created by Tau Bernstorff Lehmann on 21/07/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "solarwolfViewController.h"

@implementation solarwolfViewController




- (void)viewDidLoad {
	myView=[[UIView alloc] initWithFrame:[[self view] bounds]];
	[myView setBackgroundColor:[UIColor whiteColor]];
	[[self view] addSubview:myView];
	shipView=[[UIView alloc] initWithFrame:[[self view] bounds]];
	[myView addSubview:shipView];
	vertStep=0;
	horzStep=0;
	fatArray=[[NSMutableArray alloc] init];

	for(int i=0; i<=11; i++){
		NSMutableArray *test=[[NSMutableArray alloc] init];
		for(int i=0; i<=17; i++){
			[test insertObject:[NSNumber numberWithInt:2] atIndex:i];
		}
		[fatArray insertObject:test atIndex:i];
	}
	[self colorbox2];
	
	UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];

	[shipView setBackgroundColor: color];
	
	CGRect frame=CGRectMake(0,5, 25, 25);
	ship=[[UIView alloc]initWithFrame:frame];
	ship.backgroundColor=[UIColor blackColor];
	[myView addSubview:ship];
	[ship release];
	[color release];
	[shipView release];
    [super viewDidLoad];
}
-(void)colorbox2{
	for(int i=0; i<=11; i++){
		NSMutableArray *rows=[fatArray objectAtIndex:i];
		for(int j=0; j<=17; j++){
			CGRect frame=CGRectMake(25*i,5+25*j, 25, 25);
			UIView *box=[[UIView alloc]initWithFrame:frame];
			int test=[[[rows objectAtIndex:j] retain] intValue];
			if(test==1){				
				box.backgroundColor=[UIColor greenColor];				
			}
			else if (test==2){
				box.backgroundColor=[UIColor redColor];
			}			
			else{
				box.backgroundColor=[UIColor whiteColor];				
			}
			[shipView addSubview:box];
			[box release];
		}
		[rows release];
	}	
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *) event{
	UITouch *touch=[[event allTouches] anyObject];
	CGPoint move= [touch locationInView:myView];
	[self moveShip:move];
	
	}
-(void)colorbox{
	NSMutableArray *rows=[fatArray objectAtIndex:horzStep];
		CGRect frame=CGRectMake(25*horzStep,5+25*vertStep, 25, 25);
		int test=[[rows objectAtIndex:vertStep] intValue];
		UIView *box=[[UIView alloc]initWithFrame:frame];
		if(test==1){			
			box.backgroundColor=[UIColor greenColor];			
		}
		else if (test==2){			
			box.backgroundColor=[UIColor redColor];			
		}
		else{
			 box.backgroundColor=[UIColor whiteColor];
			}
	[shipView addSubview:box];
	[box release];
	
	}
-(void)destroyBox{
	NSMutableArray *row=[fatArray objectAtIndex:horzStep];
	int k= [[row objectAtIndex:vertStep]intValue];
	int a=k-1;
	[row replaceObjectAtIndex:vertStep	withObject:[NSNumber numberWithInt:a]];
	[fatArray replaceObjectAtIndex:horzStep withObject:row];
	[self colorbox];
	[self gameWon];
	}
-(void)gameWon{
	int l=0;
	for(int i=0; i<=11; i++){
		NSMutableArray *row=[fatArray objectAtIndex:i];
		for(int j=0; j<=17; j++){
			int k=[[row	objectAtIndex:j]intValue];
			if(k>0){
				l++;
			}
		}
	}
	if(l>0)
	{
	}
	else{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"U Won" message:@"U won the game" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	
}
-(void)moveShip:(CGPoint)move{
	CGRect shipframe=ship.frame;
	CGPoint click;
	click.x=(shipframe.origin.x+12.5)-move.x;
	click.y=(shipframe.origin.y+12.5)-move.y;
	if(fabs(click.x)>fabs(click.y)){
		if(shipframe.origin.x>move.x){
		shipframe.origin.x-=25;
			horzStep--;
		}
		else{
			shipframe.origin.x+=25;
			horzStep++;
		}
	}
	else{
		
		if(shipframe.origin.y>move.y){
		shipframe.origin.y-=25;
			vertStep--;
		}
		else{
		shipframe.origin.y+=25;
			vertStep++;
		}
	}
	
	ship.frame=shipframe;
	[self destroyBox];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (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;
}


- (void)dealloc {
    [super dealloc];
}

@end