//
// MyViewController.m
// Demo-UI3-2
//
// Created by dllo on 15/3/6.
// Copyright (c) 2015年 luojin. All rights reserved.
//
#import "MyViewController.h"
#import <stdlib.h>
#define LIGHTWIDTH 40
#define LIGHTHEIGHT 40
@interface MyViewController ()
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//相当于导演
[self.view setBackgroundColor:[UIColor grayColor]];
/*
// UIButton *aButton = [UIButton buttonWithType:UIButtonTypeSystem];
// [aButton setFrame:CGRectMake(40, 40, 40, 40)];
// [aButton setBackgroundColor:[UIColor blackColor]];
// [self.view addSubview:aButton];
[aButton setTag:100];
// [aButton addTarget:self action:@selector(turnLight:) forControlEvents:UIControlEventTouchUpInside];
//
//
// CGRect rect = [aButton frame];
// rect.origin.x += 40;
// UIButton *bButton = [UIButton buttonWithType:UIButtonTypeSystem];
// [bButton setFrame:rect];
// [bButton setBackgroundColor:[UIColor yellowColor]];
// [self.view addSubview:bButton];
// [bButton addTarget:self action:@selector(turnLight:) forControlEvents:UIControlEventTouchUpInside];
//
// [aButton setTag:1];
// [bButton setTag:2];
*/
CGRect rect = CGRectMake(0, 0, LIGHTWIDTH, LIGHTHEIGHT);
for (int i = 0; i < 8; i++) {
rect.origin.y = i * LIGHTHEIGHT + 40;
for (int j = 0; j < 8; j++) {
rect.origin.x = j * LIGHTWIDTH + 30;
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeSystem];
[aButton setFrame:rect];
[aButton setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:aButton];
[aButton addTarget:self action:@selector(turnLight:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTag:(i + 1) * 10 + (j + 1)];
//加边框
[aButton.layer setBorderColor:[[UIColor grayColor] CGColor]];
[aButton.layer setBorderWidth:0.5];
}
}
//程序启动时随机点亮几盏灯
for (int i = 0; i < 50; i++) {
NSInteger tag1 = arc4random() % (8 - 1 + 1) + 1;
NSInteger tag2 = arc4random() % (8 - 1 + 1) + 1;
UIButton *yy = (UIButton *)[self.view viewWithTag:tag1 * 10 + tag2];
[self turnLight:yy];
// NSLog(@"%ld", tag1 * 10 + tag2);
}
}
- (void)showUIAlertView
{
UIAlertView *aView = [[[UIAlertView alloc] initWithTitle:@"提示" message:@"恭喜游戏通关" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] autorelease];
[aView setBackgroundColor:[UIColor redColor]];
[aView setTintColor:[UIColor blueColor]];
[self.view addSubview:aView];
[aView show];
}
- (void)turnLight:(id)sender
{
// sender作用:找到调用该方法的按钮的指针
// NSLog(@"%@", sender);
//不是新建一个按钮,是把sender的类型强制转化成UIButton类型
UIButton *aButton = (UIButton *)sender;
//判断aButton的背景色是否为黑,为黑变黄,否则变黑
if (aButton.backgroundColor == [UIColor blackColor]) {
[aButton setBackgroundColor:[UIColor yellowColor]];
} else {
[aButton setBackgroundColor:[UIColor blackColor]];
}
// // 得到下一个灯得tag值
// NSInteger nextTag = 1;
// if (aButton.tag == 1) {
// nextTag = 2;
// }
// UIButton *bButton = (UIButton *)[self.view viewWithTag:nextTag];
// if (bButton.backgroundColor == [UIColor blackColor]) {
// [bButton setBackgroundColor:[UIColor yellowColor]];
// } else {
// [bButton setBackgroundColor:[UIColor blackColor]];
// }
NSInteger topTag = aButton.tag - 10;
NSInteger downTag = aButton.tag + 10;
NSInteger liftTag = aButton.tag - 1;
NSInteger rightTag = aButton.tag + 1;
UIButton *topButton = (UIButton *)[self.view viewWithTag:topTag];
if (topButton.backgroundColor == [UIColor blackColor]) {
[topButton setBackgroundColor:[UIColor yellowColor]];
} else {
[topButton setBackgroundColor:[UIColor blackColor]];
}
UIButton *downButton = (UIButton *)[self.view viewWithTag:downTag];
if (downButton.backgroundColor == [UIColor blackColor]) {
[downButton setBackgroundColor:[UIColor yellowColor]];
} else {
[downButton setBackgroundColor:[UIColor blackColor]];
}
UIButton *liftButton = (UIButton *)[self.view viewWithTag:liftTag];
if (liftButton.backgroundColor == [UIColor blackColor]) {
[liftButton setBackgroundColor:[UIColor yellowColor]];
} else {
[liftButton setBackgroundColor:[UIColor blackColor]];
}
UIButton *rightButton = (UIButton *)[self.view viewWithTag:rightTag];
if (rightButton.backgroundColor == [UIColor blackColor]) {
[rightButton setBackgroundColor:[UIColor yellowColor]];
} else {
[rightButton setBackgroundColor:[UIColor blackColor]];
}
// int su = 0; // 0代表没有黄色, 1代表有黄色
// for (int i = 0; i < 8; i++) {
// for (int j = 0; j < 8; j++) {
// NSInteger tag = (i + 1) * 10 + (j + 1);
// _button = (UIButton *)[self.view viewWithTag:tag];
// if (_button.backgroundColor == [UIColor yellowColor]) {
// su = 1;
// }
// }
// }
// if (0 == su) {
// [self showUIAlertView];
// }
int sum = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
NSInteger tag = (i + 1) * 10 + (j + 1);
_button = (UIButton *)[self.view viewWithTag:tag];
if (_button.backgroundColor == [UIColor blackColor]) {
sum += 1;
if (64 == sum) {
[self showUIAlertView];
}
}
}
}
}