博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2048
阅读量:6551 次
发布时间:2019-06-24

本文共 7048 字,大约阅读时间需要 23 分钟。

//

//  ViewController.m
//  2048
//
//  Created by 晚起的蚂蚁 on 2016/11/3.
//  Copyright © 2016年 晚起的蚂蚁. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property(retain)NSString* str1;
@property(retain)NSString* str2;
@property bool isMove;
@property bool isAdd;
@property NSInteger addNumber;
@end
@implementation ViewController
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _addNumber = 0;
    _str1 = [NSString new];
    _str2 = [NSString new];
    [self Map];
   
    //手滑
    UISwipeGestureRecognizer *recognizer;
   
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
   
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [[self view] addGestureRecognizer:recognizer];
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
   
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [[self view] addGestureRecognizer:recognizer];
   
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
   
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [[self view] addGestureRecognizer:recognizer];
   
     recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
   
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [[self view] addGestureRecognizer:recognizer];
   
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    if(recognizer.direction==UISwipeGestureRecognizerDirectionDown)
        [self handleSForDown];
    if(recognizer.direction==UISwipeGestureRecognizerDirectionUp)
        [self handleSForUp];
    if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft)
        [self handleSForLeft];
    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight)
        [self handleSForRight];
    //判断是否移动过
    if (_isMove) {
        [self suijiKongbai];
        _isMove=false;
    }
    _addNumber = 0;
}
//右移
-(void)handleSForRight{
   
    for (NSInteger k= 0; k<3; k++)
        for (NSInteger j=2; j>=0; j--)
            for (NSInteger i=0; i<4; i++) {
               
                NSInteger a =j*4+i+1;//前
                NSInteger b =(j+1)*4+i+1;//后
                [self move:a and:b];
            }
}
//左移
-(void)handleSForLeft{
   
    for (NSInteger k= 0; k<3; k++)
        for (NSInteger j=1; j<4; j++)
            for (NSInteger i=0; i<4; i++) {
               
                NSInteger a =j*4+i+1;//前
                NSInteger b =(j-1)*4+i+1;//后
               [self move:a and:b];
            }
}
//下移
-(void)handleSForDown{
   
    for (NSInteger k= 0; k<3; k++)
        for (NSInteger i=2; i>=0; i--)
            for (NSInteger j=3; j>=0; j--) {
               
                NSInteger a =j*4+i+1;//前
                NSInteger b =j*4+i+2;//后
                [self move:a and:b];
            }
}
//上移
-(void)handleSForUp{
   
    for (NSInteger k= 0; k<3; k++)
        for (NSInteger i=1; i<4; i++)
            for (NSInteger j=0; j<4; j++) {
               
                NSInteger a =j*4+i+1;//前
                NSInteger b =j*4+i;//后
                [self move:a and:b];
            }
}
//移动
-(void)move:(NSInteger)a and:(NSInteger)b{
    _str1=((UILabel*)[self.view viewWithTag:a]).text;//前
    _str2=((UILabel*)[self.view viewWithTag:b]).text;//后
    if (_str1.length!=0) {
        int number = [_str1 intValue];
        if (_str2.length==0) {
            UILabel* label =(UILabel*)[self.view viewWithTag:b];
            label.text=_str1;
            [self ColorSet:label and:number];
           
            UILabel* label1 =(UILabel*)[self.view viewWithTag:a];
            label1.backgroundColor = [UIColor whiteColor];
            label1.text=@"";
            _isMove=true;
        }
        if (_addNumber<2) {
            if ([_str1 isEqualToString:_str2]) {
               
                UILabel* label =(UILabel*)[self.view viewWithTag:b];
                label.text=[NSString stringWithFormat:@"%d",2*number];
                [self ColorSet:label and:2*number];
                UILabel* label1 =(UILabel*)[self.view viewWithTag:a];
                label1.backgroundColor = [UIColor whiteColor];
                label1.text=@"";
               
                UILabel* labelFendshu =(UILabel*)[self.view viewWithTag:17];
                NSInteger k = [labelFendshu.text intValue];
                labelFendshu.text = [NSString stringWithFormat:@"%ld",k+=2*number];
                _isMove=true;
                _addNumber++;
            }
        }
    }
}
//移动后再随机生成一个新的数
-(void)suijiKongbai{
    UILabel* label;
    NSInteger number;
    while (1) {
        number = arc4random()%16+1;
        label=(UILabel*)[self.view viewWithTag:number];
        if (label.text.length==0) {
            [self suijishu:number];
            break;
        }
    }
}
//开始时列出地图
-(void)Map{
    CGFloat width=[UIScreen mainScreen].bounds.size.width;
    CGFloat height=[UIScreen mainScreen].bounds.size.height;
    UIView* backgroundColor=[[UIView alloc]initWithFrame:CGRectMake(2,140, width-4, width+10)];
    backgroundColor.backgroundColor=[UIColor brownColor];
    backgroundColor.layer.masksToBounds=YES;//设置圆角显示
    backgroundColor.layer.cornerRadius=10;//设置圆角的角度大小
    [self.view addSubview:backgroundColor];
    [self.view sendSubviewToBack:backgroundColor];//把backgroundColor放到最下面
   
    UIView* myView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 20)];
    myView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:myView];
   
    self.view.backgroundColor = [UIColor orangeColor];
   
    UILabel* labelName=[[UILabel alloc]initWithFrame:CGRectMake(8,50,width-16,80)];
    labelName.backgroundColor=[UIColor whiteColor];
    labelName.text=@"2048";
    labelName.textAlignment = UITextAlignmentCenter;//剧中
    labelName.font = [UIFont systemFontOfSize:60];//字体大小
    [self.view addSubview:labelName];
    //生成2048方格
    int tag = 0;
    for(NSInteger i=0;i<4;i++){
        for(NSInteger j=0;j<4;j++){
            UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(i*(width/4-2)+6,j*(width/4-2)+150,width/4-7,width/4-7)];
            label.backgroundColor=[UIColor whiteColor];
            label.tag = ++tag;
            label.textAlignment = UITextAlignmentCenter;//剧中
            label.font = [UIFont systemFontOfSize:30];//字体大小
            label.layer.masksToBounds=YES;//设置圆角显示
            label.layer.cornerRadius=5;//设置圆角的角度大小
            [self.view addSubview:label];
        }
    }
    //开始就产生三个随机数
    for (NSInteger i=0; i<3; i++) {
        [self suijiKongbai];
    }
    UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(8,height-90,width-16,80)];
    label.backgroundColor=[UIColor whiteColor];
    label.textAlignment = UITextAlignmentCenter;//剧中
    label.font = [UIFont systemFontOfSize:40];//字体大小
    label.tag=17;
    [self.view addSubview:label];
}
//随机数产生,并赋值
-(void)suijishu:(NSInteger)number{
    //给随机数分配数字与颜色
    int fu = arc4random()%100;
    if (fu>90) {
        UILabel* label=(UILabel*)[self.view viewWithTag:number];
        [self ColorSet:label and:4];
        label.text = @"4";
    }else{
        UILabel* label=(UILabel*)[self.view viewWithTag:number];
        [self ColorSet:label and:2];
        label.text = @"2";
    }
}
//设置颜色
-(void)ColorSet:(UILabel*)label and:(NSInteger)number{
    switch (number) {
        case 2:
            label.backgroundColor = [UIColor orangeColor];
            break;
        case 4:
            label.backgroundColor = [UIColor magentaColor];
            break;
        case 8:
            label.backgroundColor = [UIColor yellowColor];
            break;
        case 16:
            label.backgroundColor = [UIColor purpleColor];
            break;
        case 32:
            label.backgroundColor = [UIColor grayColor];
            break;
        case 64:
            label.backgroundColor = [UIColor greenColor];
            break;
        case 128:
            label.backgroundColor = [UIColor lightGrayColor];
            break;
        case 256:
            label.backgroundColor = [UIColor orangeColor];
            break;
        case 512:
            label.backgroundColor = [UIColor blueColor];
            break;
        case 1024:
            label.backgroundColor = [UIColor redColor];
               break;
        case 2048:
            label.backgroundColor = [UIColor redColor];
            break;
        default:
            break;
    }
}
@end

转载于:https://www.cnblogs.com/huojiaoqingchun0123/p/6092732.html

你可能感兴趣的文章
sql语句
查看>>
android 一步一步教你集成tinker(热修复)
查看>>
到底有多少内存
查看>>
centos7.3 安装ovirt-engine4.0 版本
查看>>
css入门教程资料(3)
查看>>
putty、xshell的密钥认证
查看>>
Jenkins+git+tomcat 自动化持续部署
查看>>
项目log日志打印
查看>>
vSphere 5 中的多网卡 vMotion
查看>>
Openstack的环境的Mitaka部署环境服务,实例(1)
查看>>
Oracle约束的状态及验证机制
查看>>
Redis总结(七)Redis运维常用命令
查看>>
linux命令:cpio命令 系统裁剪之四busybox 进行linux系统制作
查看>>
常用shell
查看>>
文档的压缩与打包
查看>>
interactive_timeout和wait_timeout的关系
查看>>
tftp+syslinux 6.x 搭建PXE系统(支持EFI模式)
查看>>
python3 在不同操作系统安装第三方库方法
查看>>
redhat5.8+mfs(提供软件包文档)
查看>>
python编写登录接口
查看>>