zoukankan      html  css  js  c++  java
  • 2048

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

  • 相关阅读:
    jsp转向
    什么是 XDoclet?
    tomcat中的几点配置说明
    mysql5问题
    POJ 3734 Blocks
    POJ 2409 Let it Bead
    HDU 1171 Big Event in HDU
    POJ 3046 Ant Counting
    HDU 2082 找单词
    POJ 1286 Necklace of Beads
  • 原文地址:https://www.cnblogs.com/huojiaoqingchun0123/p/6092732.html
Copyright © 2011-2022 走看看