#import "RootViewController.h"
@interface RootViewController () <UIAlertViewDelegate>
@property (nonatomic, retain) NSArray *mapArray;
@end
@implementation RootViewController
- (void)dealloc
{
[_mapArray release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//第一关
//地图数据, 1为亮着的; 0为暗着的
NSArray *a1 = @[@0, @0, @1, @0, @0, @0, @0, @0, @0, @0];
NSArray *a2 = @[@0, @1, @1, @1, @0, @0, @0, @0, @0, @0];
NSArray *a3 = @[@0, @0, @1, @0, @0, @0, @0, @0, @0, @0];
NSArray *a4 = @[@0, @0, @0, @0, @0, @0, @0, @0, @0, @0];
NSArray *a5 = @[@0, @0, @0, @0, @0, @0, @0, @0, @0, @0];
NSArray *a6 = @[@0, @0, @0, @0, @0, @0, @0, @0, @0, @0];
NSArray *a7 = @[@0, @0, @0, @0, @0, @0, @0, @0, @0, @0];
NSArray *a8 = @[@0, @0, @0, @0, @0, @0, @0, @0, @0, @0];
NSArray *a9 = @[@0, @0, @0, @0, @0, @0, @0, @0, @0, @0];
NSArray *a10 = @[@0, @0, @0, @0, @0, @0, @0, @0, @0, @0];
self.mapArray = @[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10];
//10 * 10
for (NSInteger i = 0; i < 10; i++) {
for (NSInteger j = 0; j < 10; j++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//自定义
//每个button, 宽高均为37.5
button.frame = CGRectMake(37.5 * j, 20 + 37.5 * i, 37.5, 37.5);
button.backgroundColor = [UIColor yellowColor];
//边框的颜色
// button.layer.borderColor = [UIColor blueColor];
//圆角的圆形程度
// button.layer.cornerRadius = 10
button.layer.borderWidth = 1;
//左右相差1, 上下相差20倍数
button.tag = 100 + 20 * i + j;
//button的背景色,
//因为本身都是0 1, 如果是1, 黄色; 如果是0, 灰色
button.backgroundColor = [self.mapArray[i][j] integerValue] ? [UIColor yellowColor] : [UIColor grayColor];
//关联方法(按下按钮的方法)
[button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
//添加到父视图
[self.view addSubview:button];
}
}
}
- (void)next {
//地图数据
//第二关
NSArray *a1 = @[@0, @0, @1, @0, @0, @0, @0, @0, @0, @0];
NSArray *a2 = @[@0, @1, @1, @1, @0, @0, @0, @0, @0, @0];
NSArray *a3 = @[@0, @0, @1, @0, @0, @0, @1, @0, @0, @0];
NSArray *a4 = @[@0, @0, @0, @0, @0, @1, @1, @1, @0, @0];
NSArray *a5 = @[@0, @0, @0, @0, @0, @0, @1, @0, @0, @0];
NSArray *a6 = @[@0, @1, @0, @0, @0, @0, @0, @0, @0, @0];
NSArray *a7 = @[@1, @1, @1, @0, @0, @0, @0, @0, @0, @0];
NSArray *a8 = @[@0, @1, @0, @0, @0, @0, @0, @0, @0, @0];
NSArray *a9 = @[@0, @0, @0, @0, @0, @0, @0, @0, @0, @0];
NSArray *a10 = @[@0, @0, @0, @0, @0, @0, @0, @0, @0, @0];
//每行的情况加上去
self.mapArray = @[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10];
//删除上一关的视图
for (UIButton *button in self.view.subviews) {
[button removeFromSuperview];
}
for (NSInteger i = 0; i < 10; i++) {
for (NSInteger j = 0; j < 10; j++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//状态栏20
button.frame = CGRectMake(37.5 * j, 20 + 37.5 * i, 37.5, 37.5);
//全部背景色为黄色
button.backgroundColor = [UIColor yellowColor];
button.layer.borderWidth = 1;
button.tag = 100 + 20 * i + j;
//颜色分类变化
button.backgroundColor = [self.mapArray[i][j] integerValue] ? [UIColor yellowColor] : [UIColor grayColor];
//关联方法
[button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
}
- (void)pressButton:(UIButton *)button {
//按下一个按钮, 会关联到上下左右
//防止边上的按钮会影响到下一行按钮, 所以, 每行设置20间距
UIButton *topButton = [self.view viewWithTag:button.tag - 20];
UIButton *bottomButton = [self.view viewWithTag:button.tag + 20];
UIButton *leftButton = [self.view viewWithTag:button.tag - 1];
UIButton *rightButton = [self.view viewWithTag:button.tag + 1];
[self turnOff:button];
[self turnOff:topButton];
[self turnOff:bottomButton];
[self turnOff:leftButton];
[self turnOff:rightButton];
[self victory];
}
- (void)victory {
for (UIButton *button in self.view.subviews) {
if (button.backgroundColor == [UIColor yellowColor]) {
return;
}
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"恭喜过关!" delegate:self cancelButtonTitle:@"下一关" otherButtonTitles: nil];
[alertView show];
[alertView release];
}
- (void)turnOff:(UIButton *)button {
//根据颜色, 黄边灰; 灰变黄
if (button.backgroundColor == [UIColor yellowColor]) {
button.backgroundColor = [UIColor grayColor];
} else {
button.backgroundColor = [UIColor yellowColor];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UIAlertViewDelegate
//提醒框
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//进入下一关
[self next];
}
关灯游戏
最新推荐文章于 2022-05-17 22:20:11 发布