diff --git a/CellButton.h b/CellButton.h old mode 100644 new mode 100755 index a5177b2..53b59d7 --- a/CellButton.h +++ b/CellButton.h @@ -15,10 +15,12 @@ BOOL isOpened; } +@property (readwrite) BOOL isMine; +@property (readonly) BOOL isOpened; + - (void) addAdjCell: (CellButton *) cell; - (void) addRoundCell: (CellButton *) cell; - (int) open; - (void) markFlag; -@property (readwrite) BOOL isMine; @end diff --git a/CellButton.m b/CellButton.m old mode 100644 new mode 100755 index 0f7636c..7fe2a05 --- a/CellButton.m +++ b/CellButton.m @@ -9,6 +9,10 @@ #import "CellButton.h" @implementation CellButton + +@synthesize isMine; +@synthesize isOpened; + - (id) init { if( self = [super init]){ @@ -17,12 +21,10 @@ - (id) init isMine = NO; isOpened = NO; - [self setBackgroundImage:[[UIImage imageNamed:@"tile1.gif"] - stretchableImageWithLeftCapWidth:18.0 topCapHeight: 0.0] + [self setBackgroundImage:[UIImage imageNamed:@"tile1.gif"] forState: UIControlStateNormal]; - [self setBackgroundImage:[[UIImage imageNamed:@"tile2.gif"] - stretchableImageWithLeftCapWidth:18.0 topCapHeight: 0.0] + [self setBackgroundImage:[UIImage imageNamed:@"tile2.gif"] forState: UIControlStateHighlighted]; } @@ -65,25 +67,27 @@ - (int) open int value = [self getValue]; if(isOpened) return value; isOpened = YES; - - if(value > 0){ + [self setBackgroundImage:[UIImage imageNamed:@"tile2.gif"] + forState: UIControlStateNormal]; + + if (value > 0) { + [self setTitle:[NSString stringWithFormat:@"%i", value] forState:UIControlStateNormal]; [self setTitle:[NSString stringWithFormat:@"%i", value] forState:UIControlStateHighlighted]; - }else if(value == -1){ + + } else if (value == -1) { + [self setTitle:@"X" forState:UIControlStateNormal]; [self setTitle:@"X" forState:UIControlStateHighlighted]; - } - [self setBackgroundImage:[[UIImage imageNamed:@"tile2.gif"] - stretchableImageWithLeftCapWidth:18.0 topCapHeight: 0.0] - forState: UIControlStateNormal]; - - if(value == 0){ - for(CellButton * cell in round_cells_){ + + } else if (value == 0) { + + for(CellButton * cell in round_cells_) { + [cell open]; } } - return value; } @@ -91,6 +95,4 @@ - (void) markFlag { } -@synthesize isMine; - @end \ No newline at end of file diff --git a/Default-568h@2x.png b/Default-568h@2x.png new file mode 100644 index 0000000..0891b7a Binary files /dev/null and b/Default-568h@2x.png differ diff --git a/GameController.h b/GameController.h old mode 100644 new mode 100755 diff --git a/GameController.m b/GameController.m old mode 100644 new mode 100755 index 0d1c1b7..e88dd34 --- a/GameController.m +++ b/GameController.m @@ -9,87 +9,129 @@ #import "GameController.h" #import "CellButton.h" +#define MINE_COUNT 10 +#define CELL_COUNT_COLUMN 8 +#define CELL_COUNT_ROW 8 + @implementation GameController - (id) init { - if( self = [super init]){ - cells_ = [NSMutableArray array]; - [cells_ retain]; - isInitedCells = NO; - } - return self; + if( self = [super init]){ + cells_ = [NSMutableArray array]; + [cells_ retain]; + isInitedCells = NO; + self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(actionRevealAllMinesButton:)] autorelease]; + } + return self; } - (void) dealloc { - [cells_ release]; - [super dealloc]; + [cells_ release]; + [super dealloc]; } - (void) initCells : (CellButton *) sender { - int MINE_COUNT = 16; - for(int i = 0; i < MINE_COUNT; i++){ - while(1){ - int rand = random() % [cells_ count]; - CellButton * cell = [cells_ objectAtIndex:rand]; - if(cell != sender && !cell.isMine){ - cell.isMine = YES; - break; - } + for(int i = 0; i < MINE_COUNT; i++){ + while(1){ + int rand = random() % [cells_ count]; + CellButton * cell = [cells_ objectAtIndex:rand]; + if(cell != sender && !cell.isMine){ + cell.isMine = YES; + break; + } + } } - } - isInitedCells = YES; + isInitedCells = YES; } - (void) clickCell : (CellButton *) sender { - if(! isInitedCells) [self initCells: sender]; - [sender open]; + if(! isInitedCells) [self initCells: sender]; + [sender open]; + + if (sender.isMine) + [self displayMessage:@":-("]; + + else if ([self isAllMinesDetected]) + [self displayMessage:@":-)"]; } -(void)loadView { - [super loadView]; + [super loadView]; - UIView * contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; - contentView.backgroundColor = [UIColor whiteColor]; - - int WIDTH = 16; - int HEIGHT = 16; + UIView * contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; + contentView.backgroundColor = [UIColor whiteColor]; + + int WIDTH = CELL_COUNT_COLUMN; + int HEIGHT = CELL_COUNT_ROW; + + CGSize canvasSize = self.view.frame.size; + canvasSize.height = self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height; + int cellWidth = canvasSize.width / WIDTH; + int cellHeight = canvasSize.height / HEIGHT; + for(int x = 0; x < WIDTH; x++) { for(int y = 0; y < HEIGHT; y++){ - CellButton * cell = [[CellButton alloc] init]; - [cell setFrame:CGRectMake(x * 18.0f, y * 18.0f, 18.0f, 18.0f)]; + CellButton * cell = [[CellButton alloc] init]; + [cell setFrame:CGRectMake(x * cellWidth, y * cellHeight, cellWidth, cellHeight)]; //[cell setCenter:CGPointMake(160.0f, 208.0f)]; [cell addTarget:self action:@selector(clickCell:) forControlEvents:UIControlEventTouchUpInside]; [contentView addSubview: cell]; - [cells_ addObject: cell]; - [cell release]; + [cells_ addObject: cell]; + [cell release]; } - } - + } + for(int x = 0; x < WIDTH; x++) { for(int y = 0; y < HEIGHT; y++){ - CellButton * cell = [cells_ objectAtIndex:(x + y * WIDTH)]; - for(int i = -1; i <= 1; i++){ - for(int j = -1; j <= 1; j++){ - if(x + i < 0 || x + i >= WIDTH) continue; - if(y + j < 0 || y + j >= HEIGHT) continue; - - CellButton * cell_to = [cells_ objectAtIndex:( (x+i) + (y+j) * WIDTH)]; - [cell addRoundCell:cell_to]; - if(i == 0 || y == 0){ - [cell addAdjCell:cell_to]; - } - } - } + CellButton * cell = [cells_ objectAtIndex:(x + y * WIDTH)]; + for(int i = -1; i <= 1; i++){ + for(int j = -1; j <= 1; j++){ + if(x + i < 0 || x + i >= WIDTH) continue; + if(y + j < 0 || y + j >= HEIGHT) continue; + + CellButton * cell_to = [cells_ objectAtIndex:( (x+i) + (y+j) * WIDTH)]; + [cell addRoundCell:cell_to]; + if(i == 0 || y == 0){ + [cell addAdjCell:cell_to]; + } + } + } } } - + self.view = contentView; [contentView release]; } +- (BOOL)isAllMinesDetected +{ + for (CellButton *cell in cells_) + if ((!cell.isMine && !cell.isOpened) || (cell.isMine && cell.isOpened)) + return NO; + + return YES; +} + +- (void)actionRevealAllMinesButton:(id)sender +{ + if(! isInitedCells) [self initCells: sender]; + + for (CellButton *cell in cells_) + if (cell.isMine) + [cell open]; +} + +- (void)displayMessage:(NSString *)message +{ + UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:message message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; + [alertView show]; + [alertView release]; + +} + @end diff --git a/HomeController.h b/HomeController.h old mode 100644 new mode 100755 diff --git a/HomeController.m b/HomeController.m old mode 100644 new mode 100755 diff --git a/Info.plist b/Info.plist old mode 100644 new mode 100755 diff --git a/MineSweeper.xcodeproj/project.pbxproj b/MineSweeper.xcodeproj/project.pbxproj old mode 100644 new mode 100755 index 0033c26..1108c74 --- a/MineSweeper.xcodeproj/project.pbxproj +++ b/MineSweeper.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 45; + objectVersion = 46; objects = { /* Begin PBXBuildFile section */ @@ -16,6 +16,7 @@ 31F756D40F9220F500B1F870 /* HomeController.m in Sources */ = {isa = PBXBuildFile; fileRef = 31F756D30F9220F500B1F870 /* HomeController.m */; }; 31F757270F92292400B1F870 /* tile1.gif in Resources */ = {isa = PBXBuildFile; fileRef = 31F757250F92292400B1F870 /* tile1.gif */; }; 31F757280F92292400B1F870 /* tile2.gif in Resources */ = {isa = PBXBuildFile; fileRef = 31F757260F92292400B1F870 /* tile2.gif */; }; + BC50F2C217A9844900C0A5BE /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = BC50F2C117A9844900C0A5BE /* Default-568h@2x.png */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -35,6 +36,7 @@ 31F757260F92292400B1F870 /* tile2.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = tile2.gif; sourceTree = ""; }; 32CA4F630368D1EE00C91783 /* MineSweeper_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MineSweeper_Prefix.pch; sourceTree = ""; }; 8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + BC50F2C117A9844900C0A5BE /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -61,6 +63,7 @@ 29B97314FDCFA39411CA2CEA /* CustomTemplate */ = { isa = PBXGroup; children = ( + BC50F2C117A9844900C0A5BE /* Default-568h@2x.png */, 31F756CE0F921F7500B1F870 /* Classes */, 29B97315FDCFA39411CA2CEA /* Other Sources */, 29B97317FDCFA39411CA2CEA /* Resources */, @@ -138,9 +141,16 @@ /* Begin PBXProject section */ 29B97313FDCFA39411CA2CEA /* Project object */ = { isa = PBXProject; + attributes = { + LastUpgradeCheck = 0450; + }; buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "MineSweeper" */; - compatibilityVersion = "Xcode 3.1"; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; hasScannedForEncodings = 1; + knownRegions = ( + en, + ); mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */; projectDirPath = ""; projectRoot = ""; @@ -157,6 +167,7 @@ files = ( 31F757270F92292400B1F870 /* tile1.gif in Resources */, 31F757280F92292400B1F870 /* tile2.gif in Resources */, + BC50F2C217A9844900C0A5BE /* Default-568h@2x.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -187,6 +198,7 @@ GCC_OPTIMIZATION_LEVEL = 0; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = MineSweeper_Prefix.pch; + GCC_VERSION = ""; INFOPLIST_FILE = Info.plist; PRODUCT_NAME = MineSweeper; }; @@ -214,11 +226,10 @@ GCC_VERSION = 4.0; GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 2.2.1; - ONLY_ACTIVE_ARCH = YES; - PREBINDING = NO; + IPHONEOS_DEPLOYMENT_TARGET = 4.3; + ONLY_ACTIVE_ARCH = NO; "PROVISIONING_PROFILE[sdk=iphoneos*]" = ""; - SDKROOT = iphoneos2.0; + SDKROOT = iphoneos; }; name = Debug; }; @@ -230,8 +241,8 @@ GCC_C_LANGUAGE_STANDARD = c99; GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNUSED_VARIABLE = YES; - PREBINDING = NO; - SDKROOT = iphoneos2.0; + IPHONEOS_DEPLOYMENT_TARGET = 4.3; + SDKROOT = iphoneos; }; name = Release; }; diff --git a/MineSweeper.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/MineSweeper.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..63a14da --- /dev/null +++ b/MineSweeper.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/MineSweeper.xcodeproj/project.xcworkspace/xcuserdata/warifakhandrishi.xcuserdatad/UserInterfaceState.xcuserstate b/MineSweeper.xcodeproj/project.xcworkspace/xcuserdata/warifakhandrishi.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..2a3eb57 Binary files /dev/null and b/MineSweeper.xcodeproj/project.xcworkspace/xcuserdata/warifakhandrishi.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/MineSweeper.xcodeproj/project.xcworkspace/xcuserdata/warifakhandrishi.xcuserdatad/WorkspaceSettings.xcsettings b/MineSweeper.xcodeproj/project.xcworkspace/xcuserdata/warifakhandrishi.xcuserdatad/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..659c876 --- /dev/null +++ b/MineSweeper.xcodeproj/project.xcworkspace/xcuserdata/warifakhandrishi.xcuserdatad/WorkspaceSettings.xcsettings @@ -0,0 +1,10 @@ + + + + + HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges + + SnapshotAutomaticallyBeforeSignificantChanges + + + diff --git a/MineSweeper.xcodeproj/xcuserdata/warifakhandrishi.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist b/MineSweeper.xcodeproj/xcuserdata/warifakhandrishi.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist new file mode 100644 index 0000000..05301bc --- /dev/null +++ b/MineSweeper.xcodeproj/xcuserdata/warifakhandrishi.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist @@ -0,0 +1,5 @@ + + + diff --git a/MineSweeper.xcodeproj/xcuserdata/warifakhandrishi.xcuserdatad/xcschemes/MineSweeper.xcscheme b/MineSweeper.xcodeproj/xcuserdata/warifakhandrishi.xcuserdatad/xcschemes/MineSweeper.xcscheme new file mode 100644 index 0000000..a15b6c4 --- /dev/null +++ b/MineSweeper.xcodeproj/xcuserdata/warifakhandrishi.xcuserdatad/xcschemes/MineSweeper.xcscheme @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MineSweeper.xcodeproj/xcuserdata/warifakhandrishi.xcuserdatad/xcschemes/xcschememanagement.plist b/MineSweeper.xcodeproj/xcuserdata/warifakhandrishi.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..7452df7 --- /dev/null +++ b/MineSweeper.xcodeproj/xcuserdata/warifakhandrishi.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,22 @@ + + + + + SchemeUserState + + MineSweeper.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 1D6058900D05DD3D006BFB54 + + primary + + + + + diff --git a/MineSweeperAppDelegate.h b/MineSweeperAppDelegate.h old mode 100644 new mode 100755 diff --git a/MineSweeperAppDelegate.m b/MineSweeperAppDelegate.m old mode 100644 new mode 100755 index 9192107..80a24ba --- a/MineSweeperAppDelegate.m +++ b/MineSweeperAppDelegate.m @@ -13,9 +13,9 @@ @implementation MineSweeperAppDelegate -(void) applicationDidFinishLaunching:(UIApplication *) application { UIWindow *window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; - UINavigationController * nav = - [[UINavigationController alloc] initWithRootViewController:[[HomeController alloc] init]]; - [window addSubview: nav.view]; + UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:[[HomeController alloc] init]]; + nav.navigationBar.barStyle = UIBarStyleBlack; + window.rootViewController = nav; [window makeKeyAndVisible]; } diff --git a/MineSweeper_Prefix.pch b/MineSweeper_Prefix.pch old mode 100644 new mode 100755 diff --git a/README.md b/README.md new file mode 100644 index 0000000..322fa1d --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +minesweeper-iphone +================== + +Minesweeper for iPhone + +1. The board is an 8x8 grid and by default 10 hidden mines are randomly placed into the board. +2. New Game: start a new, randomly generated game. +3. Validate: check that a user has correctly marked all the tiles and end the game in either victory or failure +4. Cheat: reveal the locations of the mines.' diff --git a/main.m b/main.m old mode 100644 new mode 100755 diff --git a/tile1.gif b/tile1.gif old mode 100644 new mode 100755 diff --git a/tile2.gif b/tile2.gif old mode 100644 new mode 100755