关灯游戏:UI3-2

//

//  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];

                }

            }

        }

    }

    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值