-
Notifications
You must be signed in to change notification settings - Fork 8
/
GameController.m
95 lines (82 loc) · 2.15 KB
/
GameController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// GameController.m
// MineSweeper
//
// Created by t on 09/04/13.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "GameController.h"
#import "CellButton.h"
@implementation GameController
- (id) init
{
if( self = [super init]){
cells_ = [NSMutableArray array];
[cells_ retain];
isInitedCells = NO;
}
return self;
}
- (void) 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;
}
}
}
isInitedCells = YES;
}
- (void) clickCell : (CellButton *) sender
{
if(! isInitedCells) [self initCells: sender];
[sender open];
}
-(void)loadView
{
[super loadView];
UIView * contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
int WIDTH = 16;
int HEIGHT = 16;
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)];
//[cell setCenter:CGPointMake(160.0f, 208.0f)];
[cell addTarget:self action:@selector(clickCell:) forControlEvents:UIControlEventTouchUpInside];
[contentView addSubview: cell];
[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];
}
}
}
}
}
self.view = contentView;
[contentView release];
}
@end