最近突然想写一个2048的小游戏,由于全部是自定义控件,所以程序看起来冗杂,但是核心的算法部分还是很不错的,大家感兴趣的可以仔细看看。

声明部分:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(nonatomic,strong) IBOutlet UILabel *label;

@property(nonatomic,strong) IBOutlet UILabel *label1;

@property(nonatomic,strong) IBOutlet UILabel *label2;

@property(nonatomic,strong) IBOutlet UILabel *label3;

@property(nonatomic,strong) IBOutlet UILabel *label4;

@property(nonatomic,strong) IBOutlet UILabel *label5;

@property(nonatomic,strong) IBOutlet UILabel *label6;

@property(nonatomic,strong) IBOutlet UILabel *label7;

@property(nonatomic,strong) IBOutlet UILabel *label8;

@property(nonatomic,strong) IBOutlet UILabel *label9;

@property(nonatomic,strong) IBOutlet UILabel *label10;

@property(nonatomic,strong) IBOutlet UILabel *label11;

@property(nonatomic,strong) IBOutlet UILabel *label12;

@property(nonatomic,strong) IBOutlet UILabel *label13;

@property(nonatomic,strong) IBOutlet UILabel *label14;

@property(nonatomic,strong) IBOutlet UILabel *label15;

@end

实现部分:

#import "ViewController.h"

@interface ViewController ()

{

NSArray *viewArray;

NSNumber *ret,*ret1,*ret2,*ret3,*ret4,*ret5,*ret6,*ret7,*ret8,*ret9,*ret10,*ret11,*ret12,*ret13,*ret14,*ret15;

NSArray *dataArray;

NSMutableArray *mutArr,*conutArr;

int x,y;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//[mutArr removeAllObjects];

[self appear];

[self begin];

ret = [NSNumber numberWithInt:[self.label.text intValue]];

ret1 = [NSNumber numberWithInt:[self.label1.text intValue]];

ret2 = [NSNumber numberWithInt:[self.label2.text intValue]];

ret3 = [NSNumber numberWithInt:[self.label3.text intValue]];

ret4 = [NSNumber numberWithInt:[self.label4.text intValue]];

ret5 = [NSNumber numberWithInt:[self.label5.text intValue]];

ret6 = [NSNumber numberWithInt:[self.label6.text intValue]];

ret7 = [NSNumber numberWithInt:[self.label7.text intValue]];

ret8 = [NSNumber numberWithInt:[self.label8.text intValue]];

ret9 = [NSNumber numberWithInt:[self.label9.text intValue]];

ret10 = [NSNumber numberWithInt:[self.label10.text intValue]];

ret11 = [NSNumber numberWithInt:[self.label11.text intValue]];

ret12 = [NSNumber numberWithInt:[self.label12.text intValue]];

ret13 = [NSNumber numberWithInt:[self.label13.text intValue]];

ret14 = [NSNumber numberWithInt:[self.label14.text intValue]];

ret15 = [NSNumber numberWithInt:[self.label15.text intValue]];

mutArr = [[NSMutableArray alloc]initWithObjects:ret,ret1,ret2,ret3,ret4,ret5,ret6,ret7,ret8,ret9,ret10,ret11,ret12,ret13,ret14,ret15, nil];

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];

swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;

[self.view addGestureRecognizer:swipeGesture];

UISwipeGestureRecognizer *swipeGesture1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe1:)];

swipeGesture1.direction = UISwipeGestureRecognizerDirectionLeft;

[self.view addGestureRecognizer:swipeGesture1];

UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe2:)];

swipeGesture2.direction = UISwipeGestureRecognizerDirectionUp;

[self.view addGestureRecognizer:swipeGesture2];

UISwipeGestureRecognizer *swipeGesture3 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe3:)];

swipeGesture3.direction = UISwipeGestureRecognizerDirectionDown;

[self.view addGestureRecognizer:swipeGesture3];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

-(void)swipe:(UISwipeGestureRecognizer *)swipe//向右

{

int temp;

NSNumber *temp1 = nil;

[self rightMoveSpace];

for (int i = 0; i<4; i++)

{

for (int j = 3; j>=1; j--)

{

if ([[mutArr objectAtIndex:j+4*i]intValue] == [[mutArr objectAtIndex:j-1+4*i]intValue])

{

[self rightMoveSpace];

temp = [[mutArr objectAtIndex:j+4*i]intValue]+[[mutArr objectAtIndex:j-1+4*i]intValue];

temp1 = [NSNumber numberWithInt:temp];

[mutArr replaceObjectAtIndex:j+4*i withObject:temp1];

[mutArr replaceObjectAtIndex:j-1+4*i withObject:[NSNumber numberWithInt:0]];

[self rightMoveSpace];

}

}

}

[self reAppear];

}

-(void)rightMoveSpace

{

NSNumber *temp = nil;

NSNumber *temp1 = nil;

for (int i = 0; i<4; i++) {

for (int j = 2; j>=0; j--) {

int k = j;

while (k<=2&&[[mutArr objectAtIndex:k+1+4*i]intValue]==0)

{

temp = [mutArr objectAtIndex:k+1+4*i];

temp1 = [mutArr objectAtIndex:k+4*i];

[mutArr replaceObjectAtIndex:k+1+4*i withObject:temp1];

[mutArr replaceObjectAtIndex:k+4*i withObject:temp];

k++;

}

}

}

}

-(void)swipe1:(UISwipeGestureRecognizer *)swipe//向左

{

int temp;

NSNumber *temp1 = nil;

[self leftMoveSpace];

for (int i = 0; i<4; i++) {

for (int j = 0; j<3; j++)

{

if ([[mutArr objectAtIndex:j+4*i]intValue] == [[mutArr objectAtIndex:j+1+4*i]intValue])

{

temp = [[mutArr objectAtIndex:j+4*i]intValue]+[[mutArr objectAtIndex:j+1+4*i]intValue];

temp1 = [NSNumber numberWithInt:temp];

[mutArr replaceObjectAtIndex:j+4*i withObject:temp1];

[mutArr replaceObjectAtIndex:j+1+4*i withObject:[NSNumber numberWithInt:0]];

[self leftMoveSpace];

}

}

}

[self reAppear];

}

-(void)leftMoveSpace

{

NSNumber *temp = nil;

NSNumber *temp1 = nil;

for ( int i = 0; i<4; i++) {

for (int j = 1; j<4; j++) {

int k = j;

while (k>=1&&[[mutArr objectAtIndex:k-1+4*i]intValue]==0)

{

temp = [mutArr objectAtIndex:k-1+4*i];

temp1 = [mutArr objectAtIndex:k+4*i];

[mutArr replaceObjectAtIndex:k-1+4*i withObject:temp1];

[mutArr replaceObjectAtIndex:k+4*i withObject:temp];

k--;

}

}

}

}

-(void)swipe2:(UISwipeGestureRecognizer *)swipes//向上

{

int temp;

NSNumber *temp1 = nil;

[self upMoveSpace];

for ( int j = 0; j<4; j++) {

for (int i = 0; i<3; i++)

{

if ([[mutArr objectAtIndex:j+4*i]intValue] == [[mutArr objectAtIndex:j+4*(i+1)]intValue])

{

temp = [[mutArr objectAtIndex:j+4*i]intValue]+[[mutArr objectAtIndex:j+4*(i+1)]intValue];

temp1 = [NSNumber numberWithInt:temp];

[mutArr replaceObjectAtIndex:j+4*i withObject:temp1];

[mutArr replaceObjectAtIndex:j+4*(i+1) withObject:[NSNumber numberWithInt:0]];

[self upMoveSpace];

}

}

}

[self reAppear];

}

-(void)upMoveSpace

{

int index;

for (index = 0; index<mutArr.count; index++)

{

NSNumber *temp = nil;

NSNumber *temp1 = nil;

int i = index/4;

int j = index%4;

for (j = 0; j<4; j++) {

for (i = 1; i<4; i++) {

int k = i;

while (k>=1&&[[mutArr objectAtIndex:j+4*(k-1)]intValue]==0)

{

temp = [mutArr objectAtIndex:j+4*(k-1)];

temp1 = [mutArr objectAtIndex:j+4*k];

[mutArr replaceObjectAtIndex:j+4*(k-1) withObject:temp1];

[mutArr replaceObjectAtIndex:j+4*k withObject:temp];

k--;

}

}

}

}

}

-(void)swipe3:(UISwipeGestureRecognizer *)swipe//向下

{

int temp;

NSNumber *temp1 = nil;

[self downMoveSpace];

for (int j = 0; j<4; j++) {

for (int  i = 3; i>=1; i--)

{

if ([[mutArr objectAtIndex:j+4*i]intValue] == [[mutArr objectAtIndex:j+4*(i-1)]intValue])

{

temp = [[mutArr objectAtIndex:j+4*i]intValue]+[[mutArr objectAtIndex:j+4*(i-1)]intValue];

temp1 = [NSNumber numberWithInt:temp];

[mutArr replaceObjectAtIndex:j+4*i withObject:temp1];

[mutArr replaceObjectAtIndex:j+4*(i-1) withObject:[NSNumber numberWithInt:0]];

[self downMoveSpace];

}

}

}

[self reAppear];

}

-(void)downMoveSpace

{

NSNumber *temp = nil;

NSNumber *temp1 = nil;

for (int j = 0; j<4; j++)

{

for (int i = 2; i>=0; i--)

{

int k = i;

while (k<=2&&[[mutArr objectAtIndex:j+4*(k+1)]intValue]==0)

{

temp = [mutArr objectAtIndex:j+4*(k+1)];

temp1 = [mutArr objectAtIndex:j+4*k];

[mutArr replaceObjectAtIndex:j+4*(k+1) withObject:temp1];

[mutArr replaceObjectAtIndex:j+4*k withObject:temp];

k++;

}

}

}

}

-(void)reAppear

{

int k=0;

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];

for (int i =0; i<mutArr.count; i++)

{

if ([[mutArr objectAtIndex:i]intValue] == 0)

{

k++;

[dictionary setObject:[NSNumber numberWithInt:i] forKey:[NSString stringWithFormat:@"%d",k-1]];

[mutArr replaceObjectAtIndex:i withObject:@""];

}

}

NSLog(@"%d",k-1);

conutArr = [[NSMutableArray alloc]init];

[conutArr addObject:[NSNumber numberWithInt:0]];

[conutArr addObject:[NSNumber numberWithInt:k]];

if ([[conutArr objectAtIndex:conutArr.count-1]intValue]!=[[conutArr objectAtIndex:conutArr.count-2]intValue])

{

int m = arc4random()%dictionary.count;

NSNumber *temp = [dictionary objectForKey:[NSString stringWithFormat:@"%d",m]];

[mutArr replaceObjectAtIndex:[temp intValue] withObject:[NSNumber numberWithInt:2]];

}

self.label.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:0]];

self.label1.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:1]];

self.label2.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:2]];

self.label3.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:3]];

self.label4.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:4]];

self.label5.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:5]];

self.label6.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:6]];

self.label7.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:7]];

self.label8.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:8]];

self.label9.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:9]];

self.label10.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:10]];

self.label11.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:11]];

self.label12.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:12]];

self.label13.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:13]];

self.label14.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:14]];

self.label15.text = [NSString stringWithFormat:@"%@",[mutArr objectAtIndex:15]];

}

-(void)begin

{

viewArray = [self.view subviews];

int  aaa = arc4random()%15;

int  bbb = arc4random()%15;

if (aaa != bbb)

{

UILabel *label = [viewArray objectAtIndex:aaa+2];

label.text = @"2";

label.textAlignment = UITextAlignmentCenter;

label.tintColor = [UIColor redColor];

UILabel *label1 = [viewArray objectAtIndex:bbb+2];

label1.text = @"2";

label1.textAlignment = UITextAlignmentCenter;

label1.tintColor = [UIColor redColor];

}else

{

UILabel *label = [viewArray objectAtIndex:aaa+2];

label.text = @"2";

label.textAlignment = UITextAlignmentCenter;

label.tintColor = [UIColor redColor];

UILabel *label1 = [viewArray objectAtIndex:bbb+3];

label1.text = @"2";

label1.textAlignment = UITextAlignmentCenter;

label1.tintColor = [UIColor redColor];

}

}

-(void)appear

{

self.label = [[UILabel alloc]init];

self.label.frame = CGRectMake(60, 150, 60, 60);

self.label.backgroundColor = [UIColor greenColor];

self.label.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label];

self.label1 = [[UILabel alloc]init];

self.label1.frame = CGRectMake(60+60+10, 150, 60, 60);

self.label1.backgroundColor = [UIColor greenColor];

self.label1.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label1];

self.label2 = [[UILabel alloc]init];

self.label2.frame = CGRectMake(60+120+20, 150, 60, 60);

self.label2.backgroundColor = [UIColor greenColor];

self.label2.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label2];

self.label3 = [[UILabel alloc]init];

self.label3.frame = CGRectMake(60+180+30, 150, 60, 60);

self.label3.backgroundColor = [UIColor greenColor];

self.label3.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label3];

self.label4 = [[UILabel alloc]init];

self.label4.frame = CGRectMake(60, 150+70, 60, 60);

self.label4.backgroundColor = [UIColor greenColor];

self.label4.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label4];

self.label5 = [[UILabel alloc]init];

self.label5.frame = CGRectMake(60+70, 150+70, 60, 60);

self.label5.backgroundColor = [UIColor greenColor];

self.label5.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label5];

self.label6 = [[UILabel alloc]init];

self.label6.frame = CGRectMake(60+140, 150+70, 60, 60);

self.label6.backgroundColor = [UIColor greenColor];

self.label6.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label6];

self.label7 = [[UILabel alloc]init];

self.label7.frame = CGRectMake(60+210, 150+70, 60, 60);

self.label7.backgroundColor = [UIColor greenColor];

self.label7.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label7];

self.label8 = [[UILabel alloc]init];

self.label8.frame = CGRectMake(60, 150+140, 60, 60);

self.label8.backgroundColor = [UIColor greenColor];

self.label8.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label8];

self.label9 = [[UILabel alloc]init];

self.label9.frame = CGRectMake(60+70, 150+140, 60, 60);

self.label9.backgroundColor = [UIColor greenColor];

self.label9.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label9];

self.label10 = [[UILabel alloc]init];

self.label10.frame = CGRectMake(60+140, 150+140, 60, 60);

self.label10.backgroundColor = [UIColor greenColor];

self.label10.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label10];

self.label11 = [[UILabel alloc]init];

self.label11.frame = CGRectMake(60+210, 150+140, 60, 60);

self.label11.backgroundColor = [UIColor greenColor];

self.label11.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label11];

self.label12 = [[UILabel alloc]init];

self.label12.frame = CGRectMake(60, 150+210, 60, 60);

self.label12.backgroundColor = [UIColor greenColor];

self.label12.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label12];

self.label13 = [[UILabel alloc]init];

self.label13.frame = CGRectMake(60+70, 150+210, 60, 60);

self.label13.backgroundColor = [UIColor greenColor];

self.label13.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label13];

self.label14 = [[UILabel alloc]init];

self.label14.frame = CGRectMake(60+140, 150+210, 60, 60);

self.label14.backgroundColor = [UIColor greenColor];

self.label14.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label14];

self.label15 = [[UILabel alloc]init];

self.label15.frame = CGRectMake(60+210, 150+210, 60, 60);

self.label15.backgroundColor = [UIColor greenColor];

self.label15.textAlignment = UITextAlignmentCenter;

[self.view addSubview:self.label15];

}

@end

ios2048小游戏的更多相关文章

  1. jQuery实践-网页版2048小游戏

    ▓▓▓▓▓▓ 大致介绍 看了一个实现网页版2048小游戏的视频,觉得能做出自己以前喜欢玩的小游戏很有意思便自己动手试了试,真正的验证了这句话-不要以为你以为的就是你以为的,看视频时觉得看懂了,会写了, ...

  2. 拼图小游戏之计算后样式与CSS动画的冲突

    先说结论: 前几天写了几个非常简单的移动端小游戏,其中一个拼图游戏让我郁闷了一段时间.因为要获取每张图片的位置,用`<style>`标签写的样式,直接获取计算后样式再用来交换位置,结果就悲 ...

  3. 推荐10款超级有趣的HTML5小游戏

    HTML5的发展速度比任何人的都想像都要更快.更加强大有效的和专业的解决方案已经被开发......甚至在游戏世界中!这里跟大家分享有10款超级趣味的HTML5游戏,希望大家能够喜欢! Kern Typ ...

  4. 如何开发一个简单的HTML5 Canvas 小游戏

    原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...

  5. JavaScript版拼图小游戏

    慕课网上准备开个新的jQuery教程,花了3天空闲时间写了一个Javascript版的拼图小游戏,作为新教程配套的分析案例 拼图游戏网上有不少的实现案例了,但是此源码是我自己的实现,所以不做太多的比较 ...

  6. C语言-纸牌计算24点小游戏

    C语言实现纸牌计算24点小游戏 利用系统时间设定随机种子生成4个随机数,并对4个数字之间的运算次序以及运算符号进行枚举,从而计算判断是否能得出24,以达到程序目的.程序主要功能已完成,目前还有部分细节 ...

  7. Cocos2d-x 版本小游戏 《是男人就下100层》 项目开源

    这个是很久就开始动手写的一个小游戏了,直到最近才把它收尾了,拖拖拉拉的毛病总是很难改啊. 项目是基于 cocos2d-x v2.2 版本 ,目前只编译到了 Win8 平台上,并且已经上传到了商店,支持 ...

  8. Chrome 中的彩蛋,一款小游戏,你知道吗?

    今天看到一篇文章,介绍chrome中的彩蛋,带着好奇心进去看了一眼,没想到发现了一款小游戏,个人觉得还不错,偶尔可以玩一下,放松放松心情!^_^ 当 Chrome 无法连接到互联网时, 或者上着网突然 ...

  9. 软件工程:黄金G点小游戏1.0

    我们要做的是黄金G点小游戏: N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数),得到G值. ...

随机推荐

  1. PHP上传大文件配置

    使用PHP上传比较大的文件时,发现程序没反应,那是因为PHP的默认设置限制了上传文件的大小,所以要修改配置文件php.ini中的一些选项:(以下只是我个人的配置,大家可根据自己的实际需要配置) fil ...

  2. 使用spark-streaming实时读取Kafka数据统计结果存入MySQL

    在这篇文章里,我们模拟了一个场景,实时分析订单数据,统计实时收益. 场景模拟 我试图覆盖工程上最为常用的一个场景: 1)首先,向Kafka里实时的写入订单数据,JSON格式,包含订单ID-订单类型-订 ...

  3. 使用sklearn进行数据挖掘-房价预测(4)—数据预处理

    在使用机器算法之前,我们先把数据做下预处理,先把特征和标签拆分出来 housing = strat_train_set.drop("median_house_value",axis ...

  4. iPhone X 适配解决方案

    在head里添加<meta name='viewport' content='initial-scale=1, viewport-fit=cover'> 这将导致一个页面允分利用iPhon ...

  5. 数据分析与展示——NumPy库入门

    这是我学习北京理工大学嵩天老师的<Python数据分析与展示>课程的笔记.嵩老师的课程重点突出.层次分明,在这里特别感谢嵩老师的精彩讲解. NumPy库入门 数据的维度 维度是一组数据的组 ...

  6. 想成为一个高效的Web开发者吗?来看看大牛分享的经验吧~ #精选JAVASCRIPT前端开发

    想成为一个高效的Web开发者吗?来看看大牛分享的经验吧~ 作为一个软(ku)件(bi)工(de)程(ma)师(nong),你有没有觉得做什么事都没时间?没时间学习新东西,没时间去回顾.整理原来写的烂代 ...

  7. Python学习之一:Python2.7与opencv2.4安装配置

    安装前准备: 1.确定所安装的电脑是32位还是64位系统:(作者电脑是64bit win10) 2.下载对应的安装包: (1)下载最新Python安装包:https://www.python.org/ ...

  8. HDU 4118 Holiday's Accommodation(树形DP)

    Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 200000/200000 K (Jav ...

  9. sql脚本

    Windows下执行命令 \. d:\book.sql 这里使用了case when 这个小技巧来实现批量更新.//一个字段 UPDATE categories      SET display_or ...

  10. Kotlin实现LeetCode算法题之Two Sum

    LeetCode介绍 LeetCode是算法练习.交流等多功能网站,感兴趣的同学可以关注下(老司机请超车).页面顶部的Problems菜单对应算法题库,附带历史通过滤.难易程度等信息. 未来计划 打算 ...