ios扫雷
Posted 晚起的蚂蚁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 10 //格子边长个数
#define NUMRED 20 //雷的个数
#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{
imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, Height-Width-100, Width, Width)];
imageView.image=[UIImage imageNamed:@""];
imageView.userInteractionEnabled=YES;
imageView.backgroundColor=[UIColor greenColor];
[self.view addSubview:imageView];
UIImageView* imageViewHeader=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Width, Height-Width-100)];
imageViewHeader.image=[UIImage imageNamed:@"0fd7eeeee49905d8e4d7f1b02b937f40.png"];
[self.view addSubview:imageViewHeader];
UIImageView* imageViewFoot=[[UIImageView alloc]initWithFrame:CGRectMake(0, Height-100, Width, 100)];
imageViewFoot.image=[UIImage imageNamed:@"a10e9fc0ccdc64360c8a90f97918b0a3.png"];
[self.view addSubview:imageViewFoot];
}
//创建按钮
-(void)createButton{
CGFloat btnHeight=imageView.frame.size.width/NUM;
for (int i=0; i<NUM; i++) {
for (int j=0; j<NUM; j++) {
UIButton* btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(btnHeight*j, btnHeight*i, btnHeight-1, btnHeight-1);
btn.tag=NUM*i+j+1;
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[btn.layer setBorderWidth:3];
btn.layer.borderColor = [UIColor grayColor].CGColor;
btn.backgroundColor=[UIColor brownColor];
[imageView addSubview:btn];
}
}
}
//按钮点击事件
-(void)btnAction:(UIButton*)sender{
for (int i=0; i<dataSource.count; i++) {
if(sender.tag==[dataSource[i] intValue]){
NSArray* arr=[imageView subviews];
for (int k=0; k<arr.count; k++) {
UIButton* btn=[imageView viewWithTag:k+1];
if ([self tagIsEqulTag:k+1]) {
btn.backgroundColor=[UIColor redColor];
}else{
[btn setTitle:allArr[k] forState:UIControlStateNormal];
btn.backgroundColor=[UIColor blueColor];
}
}
return;
}
}
[sender setTitle:allArr[sender.tag-1] forState:UIControlStateNormal];
sender.backgroundColor=[UIColor blueColor];
}
//随机分布雷
-(void)createData{
dataSource=[NSMutableArray new];
int numB=NUM*NUM;
int number=0;
while (1) {
number=(arc4random()%numB)+1;
int time=0;
if (dataSource.count==0) {
[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];
for (int i=0; i<NUM*NUM; i++) {
[allArr addObject:@"0"];
}
//按钮的坐标
CGFloat coordinateX=0;
CGFloat coordinateY=0;
NSInteger number=0;
NSArray* btnArr=[imageView subviews];
for (UIButton* btn in btnArr) {
if ((btn.backgroundColor!=[UIColor redColor])) {
coordinateX=(btn.tag-1)%NUM;
coordinateY=(btn.tag-1)/NUM;
//左
if (coordinateX>0) {
if ([self tagIsEqulTag:btn.tag-1]) {
number++;
}
}else{
if (coordinateY>0&&coordinateY<9) {
if ([self tagIsEqulTag:(coordinateX+1)+(coordinateY+1)*NUM+1]) {
number++;
}
if ([self tagIsEqulTag:(coordinateX+1)+(coordinateY-1)*NUM+1]) {
number++;
}
}else{
if ([self tagIsEqulTag:(coordinateX+1)+(coordinateY+1)*NUM+1]) {
number++;
}
}
}
//右
if (coordinateX<9) {
if ([self tagIsEqulTag:btn.tag+1]) {
number++;
}
}else{
if (coordinateY>0&&coordinateY<9) {
if ([self tagIsEqulTag:(coordinateX-1)+(coordinateY+1)*NUM+1]) {
number++;
}
if ([self tagIsEqulTag:(coordinateX-1)+(coordinateY-1)*NUM+1]) {
number++;
}
}else{
if ([self tagIsEqulTag:(coordinateX-1)+(coordinateY-1)*NUM+1]) {
number++;
}
}
}
//上
if (coordinateY>0) {
if ([self tagIsEqulTag:(coordinateY-1)*NUM+coordinateX+1]) {
number++;
}
}else{
if (coordinateX>0&&coordinateX<9) {
if ([self tagIsEqulTag:(coordinateY+1)*NUM+(coordinateX-1)+1]) {
number++;
}
if ([self tagIsEqulTag:(coordinateY+1)*NUM+(coordinateX+1)+1]) {
number++;
}
}else{
if ([self tagIsEqulTag:(coordinateY+1)*NUM+(coordinateX+1)+1]) {
number++;
}
}
}
//下
if (coordinateY<9) {
if ([self tagIsEqulTag:(coordinateY+1)*NUM+coordinateX+1]) {
number++;
}
}else{
if (coordinateX>0&&coordinateX<9) {
if ([self tagIsEqulTag:(coordinateY-1)*NUM+(coordinateX-1)+1]) {
number++;
}
if ([self tagIsEqulTag:(coordinateY-1)*NUM+(coordinateX+1)+1]) {
number++;
}
}else{
if ([self tagIsEqulTag:(coordinateY-1)*NUM+(coordinateX-1)+1]) {
number++;
}
}
}
//中间按钮的斜角
if (coordinateY>0&&coordinateY<9&&coordinateX>0&&coordinateX<9) {
if ([self tagIsEqulTag:(coordinateY-1)*NUM+(coordinateX-1)+1]) {
number++;
}
if ([self tagIsEqulTag:(coordinateY-1)*NUM+(coordinateX+1)+1]) {
number++;
}
if ([self tagIsEqulTag:(coordinateY+1)*NUM+(coordinateX-1)+1]) {
number++;
}
if ([self tagIsEqulTag:(coordinateY+1)*NUM+(coordinateX+1)+1]) {
number++;
}
}
//将数据存入数组
NSString* numberStr=[NSString stringWithFormat:@"%ld",number];
//[btn setTitle:numberStr forState:UIControlStateNormal];
allArr[btn.tag-1]=numberStr;
number=0;
}
}
}
//判断是该按钮是否为雷
-(BOOL)tagIsEqulTag:(NSInteger)tag{
for (int i=0; i<dataSource.count; i++) {
if(tag==[dataSource[i] intValue]){
return YES;
}
}
return NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
以上是关于ios扫雷的主要内容,如果未能解决你的问题,请参考以下文章