iOS 类似2048、4096小游戏-OC

大概思路(初步制作,粗工制造):demo

1、Collection 创建cell

2、cell上添加一个view,用来添加手势

3、字典用来存放数据-》每次执行StarGame数组接收没有数字的字典,原来字典对应的删除-》随机选中改变数组-》插回原来数据-》每次滑动 用选择排序分别相加相邻且相等的数据后,再用选择排序 进行排序(详情下文)-》刷新cell-》♻️

 

//数据    
NSMutableDictionary * dictChange;//放 数值为0(代表没有数值的位置) 的字典
 UISwipeGestureRecognizer * recognizer;//滑动手势
   NSMutableDictionary * dictData;//数据字典

 

// 先添加 初始数据
-(void)creaDATA{
    
    
    dictData = [NSMutableDictionary dictionary];
    for (int i=0; i<16; i++) {
        
        [dictData setObject:@"0" forKey:@(i)];
        
    }
    
}

 

-(void)StarGame{
    //筛选出 模块上可以添加数据的 位置字典。装到数组,随机分配
    dictChange = [NSMutableDictionary dictionary];
    for (int i=0; i<16; i++) {
        NSString * str = [dictData objectForKey:@(i)];
        if ([str isEqualToString:@"0"]) {
            
            [dictChange setObject:@"0" forKey:@(i)];
            
        }
       
    }
    
    //先判断有没有满足要求
    NSArray * value = [dictData allValues];
    if ([value containsObject:_GameNum]) {
        NSInteger haomiao,miao,fen;
        
        haomiao = timernumber%100;
        miao = timernumber/100;
        fen = miao/60;
        
        
        [UIView pushAlertTwoActionViewWithMessage:[NSString stringWithFormat:@"恭喜你,完成游戏\n用时:%02ld:%02ld:%02ld",fen,miao,haomiao] Target:self Title:@"OH耶!!!" oneAlertTitle:@"挑战时间" twoAlertTitle:@"休息一下" oneActionfunc:^{
            //继续
            [dictData removeAllObjects];
            [collectionvieww reloadData];
            timernumber = 0;
            [self StarGame];
            [self timerStar];
            
            
        } twoActionfunc:^{
            
            
            //返回上一页
            
        }];

    }
    
    //如果没有位置可以再添加数字了 游戏就结束了
    if (dictChange.count ==0) {
        [timerss invalidate];
        timerss = nil;
        
           [UIView pushAlertTwoActionViewWithMessage:@"游戏结束" Target:self Title:@"再挑战一局吧" oneAlertTitle:@"好的" twoAlertTitle:@"休息一下" oneActionfunc:^{
               //继续
               [dictData removeAllObjects];
               [collectionvieww reloadData];
               timernumber = 0;
               [self StarGame];
               [self timerStar];
               
               
           } twoActionfunc:^{
               
               
               //返回上一页
               
           }];
        return;
    }
    
    //随机添加数据。刷新表格
    int random = (int)( (arc4random() % (dictChange.count)));
    NSArray * dictKey = [dictChange allKeys];
    [dictChange setObject:@"2" forKey:dictKey[random]];
    //返回给数据字典
    for (NSString * key in dictKey) {
        
        [dictData setObject:dictChange[key] forKey:key];
        
    }
    [dictChange removeAllObjects];
    //刷新表
    [collectionvieww reloadData];
    
    
    
}
每次插入数字时
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(silidingTop)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [frontView addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(silidingLift)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [frontView addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(silidingDown)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [frontView addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(silidingRight)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [frontView addGestureRecognizer:recognizer];
上左下右 手势添加
-(void)silidingLift{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int h = 0; h<4; h++) {
            
            
            //选择 筛选相邻数字相加。只相加一次
            for (int i=0; i<4; i++) {
                
                if (![dictData[@(i+4*h)] isEqualToString:@"0"])
                    
                {
                    
                    for (int j=i+1; j<4; j++) {
                        
                        
                        if (![dictData[@(j+4*h)] isEqualToString:@"0"]){
                            
                            
                            if ([dictData[@(i+4*h)] isEqualToString: dictData[@(j+4*h)]]) {
                                
                                
                                
                                //后边的数是 相加后的值
                                [dictData setObject:[NSString stringWithFormat:@"%d",[dictData[@(j+4*h)] intValue] + [dictData[@(i+4*h)] intValue]] forKey:@(i+4*h)];
                                [dictData setObject:@"0" forKey:@(j+4*h)];//前边的 设置为0
                                
                                
                            }
                            break; // 有了 就不再循环了
                            
                        }
                    }
                    
                }
                
                if (i==3) {
                    [self Lift2:h];
                }
                
            }
            
            
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [collectionvieww reloadData];
            
            [self StarGame];
        });
    });
    
}

-(void)Lift2:(int)h{
    
    //选择排序  整理位置
    for(int i=0;i<4;i++){
        if ([dictData[@(i+4*h)] isEqualToString:@"0"])//i为0 的时候
        {
            
            for (int j=i+1; j<4; j++)
            {
                if (![dictData[@(j+4*h)] isEqualToString:@"0"]) //j 有值的时候
                    
                    //i 的值 为j 的。j设置为0
                {
                    
                    [dictData setObject:dictData[@(j+4*h)] forKey:@(i+4*h)];
                    [dictData setObject:@"0" forKey:@(j+4*h)];
                    
                    
                    break; // 继续下一次循环
                }
                
            }
            
        }
        
    }
    
}
👈向左滑动
-(void)silidingDown{
    
    
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int h = 0; h<4; h++) {
            
            //选择 筛选相邻数字相加。只相加一次
            for (int i=0; i<4; i++) {
                
                if (![dictData[@(12-i*4+h)] isEqualToString:@"0"])
                    
                {
                    
                    for (int j=i+1; j<4; j++) {
                        
                        
                        if (![dictData[@(12-j*4+h)] isEqualToString:@"0"]){
                            
                            
                            if ([dictData[@(12-i*4+h)] isEqualToString: dictData[@(12-j*4+h)]]) {
                                
                                
                                
                                //后边的数是 相加后的值
                                [dictData setObject:[NSString stringWithFormat:@"%d",[dictData[@(12-j*4+h)] intValue] + [dictData[@(12-i*4+h)] intValue]] forKey:@(12-i*4+h)];
                                [dictData setObject:@"0" forKey:@(12-j*4+h)];//前边的 设置为0
                                
                                
                            }
                            break; // 有了 就不再循环了
                            
                        }
                    }
                    
                }
                
                if (i==3) {
                    [self Down2:h];
                }
                
            }
            
            
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [collectionvieww reloadData];
            
            [self StarGame];
        });
    });
}

-(void)Down2:(int)h{
    
    //选择排序  整理位置
    for(int i=0;i<4;i++){
        if ([dictData[@(12-i*4+h)] isEqualToString:@"0"])//i为0 的时候
        {
            
            for (int j=i+1; j<4; j++)
            {
                if (![dictData[@(12-j*4+h)] isEqualToString:@"0"]) //j 有值的时候
                    
                    //i 的值 为j 的。j设置为0
                {
                    
                    [dictData setObject:dictData[@(12-j*4+h)] forKey:@(12-i*4+h)];
                    [dictData setObject:@"0" forKey:@(12-j*4+h)];
                    
                    
                    break; // 继续下一次循环
                }
                
            }
            
        }
        
    }
    
}
👇向下滑动
-(void)silidingRight{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int h = 0; h<4; h++) {
            
            //选择 筛选相邻数字相加。只相加一次
            for (int i=0; i<4; i++) {
                
                if (![dictData[@(3-i+h*4)] isEqualToString:@"0"])
                    
                {
                    
                    for (int j=i+1; j<4; j++) {
                        
                        
                        if (![dictData[@(3-j+h*4)] isEqualToString:@"0"]){
                            
                            
                            if ([dictData[@(3-i+h*4)] isEqualToString: dictData[@(3-j+h*4)]]) {
                                
                                
                                
                                //后边的数是 相加后的值
                                [dictData setObject:[NSString stringWithFormat:@"%d",[dictData[@(3-j+h*4)] intValue] + [dictData[@(3-i+h*4)] intValue]] forKey:@(3-i+h*4)];
                                [dictData setObject:@"0" forKey:@(3-j+h*4)];//前边的 设置为0
                                
                                
                            }
                            break; // 有了 就不再循环了
                            
                        }
                    }
                    
                }
                
                if (i==3) {
                    [self right2:h];
                }
                
            }
            
            
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [collectionvieww reloadData];
            
            [self StarGame];
        });
    });
    
}

-(void)right2:(int)h{
    
    //选择排序  整理位置
    for(int i=0;i<4;i++){
        if ([dictData[@(3-i+h*4)] isEqualToString:@"0"])//i为0 的时候
        {
            
            for (int j=i+1; j<4; j++)
            {
                if (![dictData[@(3-j+h*4)] isEqualToString:@"0"]) //j 有值的时候
                    
                    //i 的值 为j 的。j设置为0
                {
                    
                    [dictData setObject:dictData[@(3-j+h*4)] forKey:@(3-i+h*4)];
                    [dictData setObject:@"0" forKey:@(3-j+h*4)];
                    
                    
                    break; // 继续下一次循环
                }
                
            }
            
        }
        
    }
    
}
👉向右滑动
-(void)silidingTop{
    
   
        
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int h = 0; h<4; h++) {
            
            //选择 筛选相邻数字相加。只相加一次
            for (int i=0; i<4; i++) {
                
                if (![dictData[@(i*4+h)] isEqualToString:@"0"])
                    
                {
                    
                    for (int j=i+1; j<4; j++) {
                        
                        if (![dictData[@(j*4+h)] isEqualToString:@"0"]){
                            
                            if ([dictData[@(i*4+h)] isEqualToString: dictData[@(j*4+h)]]) {
                                
                                //后边的数是 相加后的值
                                [dictData setObject:[NSString stringWithFormat:@"%d",[dictData[@(j*4+h)] intValue] + [dictData[@(i*4+h)] intValue]] forKey:@(i*4+h)];
                                [dictData setObject:@"0" forKey:@(j*4+h)];//前边的 设置为0
                                
                                
                            }
                            break; // 有了 就不再循环了
                            
                        }
                    }
                    
                }
                
                if (i==3) {
                    [self top2:h];
                }
                
            }
            
            
            
        }

        
        dispatch_async(dispatch_get_main_queue(), ^{
            [collectionvieww reloadData];
            
            [self StarGame];
        });
    });
}

-(void)top2:(int)h{
    
    //选择排序  整理位置
    for(int i=0;i<4;i++){
        if ([dictData[@(i*4+h)] isEqualToString:@"0"])//i为0 的时候
        {
            
            for (int j=i+1; j<4; j++)
            {
                if (![dictData[@(j*4+h)] isEqualToString:@"0"]) //j 有值的时候
                    
                    //i 的值 为j 的。j设置为0
                {
                    
                    [dictData setObject:dictData[@(j*4+h)] forKey:@(i*4+h)];
                    [dictData setObject:@"0" forKey:@(j*4+h)];
                    
                    
                    break; // 继续下一次循环
                }
                
            }
            
        }
        
    }
    
}
👆向上滑动

 

posted @ 2017-06-29 15:37  徐家汇  阅读(550)  评论(0编辑  收藏  举报