Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CellButton.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 19 additions & 17 deletions CellButton.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#import "CellButton.h"

@implementation CellButton

@synthesize isMine;
@synthesize isOpened;

- (id) init
{
if( self = [super init]){
Expand All @@ -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];

}
Expand Down Expand Up @@ -65,32 +67,32 @@ - (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;
}

- (void) markFlag
{
}

@synthesize isMine;

@end
Binary file added [email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified GameController.h
100644 → 100755
Empty file.
136 changes: 89 additions & 47 deletions GameController.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Empty file modified HomeController.h
100644 → 100755
Empty file.
Empty file modified HomeController.m
100644 → 100755
Empty file.
Empty file modified Info.plist
100644 → 100755
Empty file.
27 changes: 19 additions & 8 deletions MineSweeper.xcodeproj/project.pbxproj
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 45;
objectVersion = 46;
objects = {

/* Begin PBXBuildFile section */
Expand All @@ -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 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = BC50F2C117A9844900C0A5BE /* [email protected] */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -35,6 +36,7 @@
31F757260F92292400B1F870 /* tile2.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = tile2.gif; sourceTree = "<group>"; };
32CA4F630368D1EE00C91783 /* MineSweeper_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MineSweeper_Prefix.pch; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
BC50F2C117A9844900C0A5BE /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -61,6 +63,7 @@
29B97314FDCFA39411CA2CEA /* CustomTemplate */ = {
isa = PBXGroup;
children = (
BC50F2C117A9844900C0A5BE /* [email protected] */,
31F756CE0F921F7500B1F870 /* Classes */,
29B97315FDCFA39411CA2CEA /* Other Sources */,
29B97317FDCFA39411CA2CEA /* Resources */,
Expand Down Expand Up @@ -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 = "";
Expand All @@ -157,6 +167,7 @@
files = (
31F757270F92292400B1F870 /* tile1.gif in Resources */,
31F757280F92292400B1F870 /* tile2.gif in Resources */,
BC50F2C217A9844900C0A5BE /* [email protected] in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges</key>
<true/>
<key>SnapshotAutomaticallyBeforeSignificantChanges</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "1.0">
</Bucket>
Loading