关灯游戏

来源:互联网 发布:阿里云域名icp备案 编辑:程序博客网 时间:2024/04/27 17:21

#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    for (int i = 0; i < 6; i++) {

        for (int j = 0; j < 7; j++) {

            UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

            button.frame = CGRectMake(15 + 50 * i, 40 + 50 * j, 40, 40);

            button.backgroundColor = [UIColor whiteColor];

            [self.view addSubview:button];

            button.tag = 10 * i + j + 1;

            [button addTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

            

        }

    }

}

- (void)buttonAction:(UIButton *)button

{

    if (button.backgroundColor == [UIColor whiteColor]) {

        button.backgroundColor = [UIColor blackColor];

        NSInteger a = button.tag;

        UIButton *button0 = (UIButton *)[self.view viewWithTag:(a - 1)];

        if (button0.backgroundColor == [UIColor whiteColor]) {

            button0.backgroundColor = [UIColor blackColor];

        } else if (button0.backgroundColor == [UIColor blackColor]) {

            button0.backgroundColor = [UIColor whiteColor];

        }

        UIButton *button1 = (UIButton *)[self.view viewWithTag:(a + 1)];

        if (button1.backgroundColor == [UIColor whiteColor]) {

            button1.backgroundColor = [UIColor blackColor];

        } else if (button1.backgroundColor == [UIColor blackColor]) {

            button1.backgroundColor = [UIColor whiteColor];

        }

        UIButton *button2 = (UIButton *)[self.view viewWithTag:(a - 10)];

        if (button2.backgroundColor == [UIColor whiteColor]) {

            button2.backgroundColor = [UIColor blackColor];

        } else if (button2.backgroundColor == [UIColor blackColor]) {

            button2.backgroundColor = [UIColor whiteColor];

        }

        UIButton *button3 = (UIButton *)[self.view viewWithTag:(a + 10)];

        if (button3.backgroundColor == [UIColor whiteColor]) {

            button3.backgroundColor = [UIColor blackColor];

        } else if (button0.backgroundColor == [UIColor blackColor]) {

            button3.backgroundColor = [UIColor whiteColor];

        }

    } else if (button.backgroundColor == [UIColor blackColor]) {

        button.backgroundColor = [UIColor whiteColor];

        NSInteger a = button.tag;

        UIButton *button0 = (UIButton *)[self.view viewWithTag:(a - 1)];

        if (button0.backgroundColor == [UIColor whiteColor]) {

            button0.backgroundColor = [UIColor blackColor];

        } else if (button0.backgroundColor == [UIColor blackColor]) {

            button0.backgroundColor = [UIColor whiteColor];

        }

        UIButton *button1 = (UIButton *)[self.view viewWithTag:(a + 1)];

        if (button1.backgroundColor == [UIColor whiteColor]) {

            button1.backgroundColor = [UIColor blackColor];

        } else if (button1.backgroundColor == [UIColor blackColor]) {

            button1.backgroundColor = [UIColor whiteColor];

        }

        UIButton *button2 = (UIButton *)[self.view viewWithTag:(a - 10)];

        if (button2.backgroundColor == [UIColor whiteColor]) {

            button2.backgroundColor = [UIColor blackColor];

        } else if (button2.backgroundColor == [UIColor blackColor]) {

            button2.backgroundColor = [UIColor whiteColor];

        }

        UIButton *button3 = (UIButton *)[self.view viewWithTag:(a + 10)];

        if (button3.backgroundColor == [UIColor whiteColor]) {

            button3.backgroundColor = [UIColor blackColor];

        } else if (button3.backgroundColor == [UIColor blackColor]) {

            button3.backgroundColor = [UIColor whiteColor];

        }

    }

    NSLog(@"%d", button.tag);

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end


0 0
原创粉丝点击