ios扫雷

就这些代码敲了我两个小时。。。。。。

//  ViewController.m

//  扫雷

//

//  Created by 晚起的蚂蚁 on 2017/3/22.

//  Copyright © 2017年 晚起的蚂蚁. All rights reserved.

//

#define Width [UIScreen mainScreen].bounds.size.width

#define Height [UIScreen mainScreen].bounds.size.height

#define NUM           //格子边长个数

#define NUMRED       //雷的个数

#import "ViewController.h"

@interface ViewController ()

{

UIImageView* imageView;

NSMutableArray* dataSource;//雷的位置

NSMutableArray* allArr;    //所有按钮的数字

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor=[UIColor whiteColor];

[self createImageView];

[self createButton];

[self createData];

[self numberOfRedBtn];

}

//创建背景视图

-(void)createImageView{

, Width, Width)];

imageView.image=[UIImage imageNamed:@""];

imageView.userInteractionEnabled=YES;

imageView.backgroundColor=[UIColor greenColor];

[self.view addSubview:imageView];

, , )];

imageViewHeader.image=[UIImage imageNamed:@"0fd7eeeee49905d8e4d7f1b02b937f40.png"];

[self.view addSubview:imageViewHeader];

, , )];

imageViewFoot.image=[UIImage imageNamed:@"a10e9fc0ccdc64360c8a90f97918b0a3.png"];

[self.view addSubview:imageViewFoot];

}

//创建按钮

-(void)createButton{

CGFloat btnHeight=imageView.frame.size.width/NUM;

; i<NUM; i++) {

; j<NUM; j++) {

UIButton* btn=[UIButton buttonWithType:UIButtonTypeCustom];

btn., btnHeight-);

btn.;

[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

[btn.];

btn.layer.borderColor = [UIColor grayColor].CGColor;

btn.backgroundColor=[UIColor brownColor];

[imageView addSubview:btn];

}

}

}

//按钮点击事件

-(void)btnAction:(UIButton*)sender{

; i<dataSource.count; i++) {

if(sender.tag==[dataSource[i] intValue]){

NSArray* arr=[imageView subviews];

; k<arr.count; k++) {

];

]) {

btn.backgroundColor=[UIColor redColor];

}else{

[btn setTitle:allArr[k] forState:UIControlStateNormal];

btn.backgroundColor=[UIColor blueColor];

}

}

return;

}

}

] forState:UIControlStateNormal];

sender.backgroundColor=[UIColor blueColor];

}

//随机分布雷

-(void)createData{

dataSource=[NSMutableArray new];

int numB=NUM*NUM;

;

) {

number=(;

;

) {

[dataSource addObject:[NSString stringWithFormat:@"%d",number]];

}else if(dataSource.count<NUMRED){

for (NSString* str in dataSource) {

if ([str intValue]==number) break;

else time++;

if (time==dataSource.count) {

[dataSource addObject:[NSString stringWithFormat:@"%d",number]];

break;

}

}

}else{

break;

}

}

}

//非雷区的数字

-(void)numberOfRedBtn{

allArr=[NSMutableArray new];

; i<NUM*NUM; i++) {

[allArr addObject:@"0"];

}

//按钮的坐标

;

;

;

NSArray* btnArr=[imageView subviews];

for (UIButton* btn in btnArr) {

if ((btn.backgroundColor!=[UIColor redColor])) {

coordinateX=(btn.)%NUM;

coordinateY=(btn.)/NUM;

//左

) {

]) {

number++;

}

}else{

&&coordinateY<) {

)+(coordinateY+)*]) {

number++;

}

)+(coordinateY-)*]) {

number++;

}

}else{

)+(coordinateY+)*]) {

number++;

}

}

}

//右

) {

]) {

number++;

}

}else{

&&coordinateY<) {

)+(coordinateY+)*]) {

number++;

}

)+(coordinateY-)*]) {

number++;

}

}else{

)+(coordinateY-)*]) {

number++;

}

}

}

//上

) {

)*]) {

number++;

}

}else{

&&coordinateX<) {

)*)+]) {

number++;

}

)*)+]) {

number++;

}

}else{

)*)+]) {

number++;

}

}

}

//下

) {

)*]) {

number++;

}

}else{

&&coordinateX<) {

)*)+]) {

number++;

}

)*)+]) {

number++;

}

}else{

)*)+]) {

number++;

}

}

}

//中间按钮的斜角

&&coordinateY<&&coordinateX>&&coordinateX<) {

)*)+]) {

number++;

}

)*)+]) {

number++;

}

)*)+]) {

number++;

}

)*)+]) {

number++;

}

}

//将数据存入数组

NSString* numberStr=[NSString stringWithFormat:@"%ld",number];

//[btn setTitle:numberStr forState:UIControlStateNormal];

]=numberStr;

number=;

}

}

}

//判断是该按钮是否为雷

-(BOOL)tagIsEqulTag:(NSInteger)tag{

; i<dataSource.count; i++) {

if(tag==[dataSource[i] intValue]){

return YES;

}

}

return NO;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

@end

上一篇:博弈论揭示了深度学习的未来(译自:Game Theory Reveals the Future of Deep Learning)


下一篇:《MySQL必知必会》学习笔记整理