Objective-C 实现2048算法类

笑对人生起伏6

笑对人生起伏6

2016-02-19 11:35

今天图老师小编给大家介绍下Objective-C 实现2048算法类,平时喜欢Objective-C 实现2048算法类的朋友赶紧收藏起来吧!记得点赞哦~

参数model有一个二维数组data,及阶数matrix

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
// .h文件@class DataModel; @interface Algorithm : NSObject @property (nonatomic,assign) int addScore; // 加分 - (void)caculateTop:(DataModel *)model;  // 上滑规则- (void)caculateBottom:(DataModel *)model; // 下滑规则- (void)caculateLeft:(DataModel *)model;  // 左滑规则- (void)caculateRight:(DataModel *)model; // 右滑规则 - (BOOL)randNewOne:(DataModel *)data; - (int)getAddScore; @end  // .m文件@implementation Algorithm #pragma mark - 滑动规则- (void)caculateTop:(DataModel *)model {  [self up_remove_blank:model];  [self up:model];} - (void)caculateBottom:(DataModel *)model {  [self down_remove_blank:model];  [self down:model];} - (void)caculateLeft:(DataModel *)model {  [self left_remove_blank:model];  [self left:model];} - (void)caculateRight:(DataModel *)model {  [self right_remove_blank:model];  [self right:model];} - (int)getAddScore {  int temp = _addScore;  _addScore = 0;  return temp;} #pragma mark - 新一个- (BOOL)randNewOne:(DataModel *)model {  array data = [model getData];  int all = 0;  for (int i=0; imodel.matrix; i++) {for (int j=0; jmodel.matrix; j++) {  if (data[i][j] == 0) {all = all + 1;  }}  }  if (all == 0) {return NO;  }  int index = arc4random() % all;  all = 0;  for (int i=0; imodel.matrix; i++) {for (int j=0; jmodel.matrix; j++) {  if (data[i][j] == 0) {all = all + 1;if (all == index+1) {  data[i][j] = 2;  return YES;}  }}  }  return NO;} #pragma mark - 滑动算法- (void)up_remove_blank:(DataModel *)model {  array data = [model getData];  int i,j,k;  for(j=0;j  model.matrix;j++){for(i=1;i  model.matrix;i++){  k=i;  while(k-1=0&&data[k-1][j]==0){//上面的那个为空//swap(data[k][j],data[k-1][j]);int temp = data[k][j];data[k][j] = data[k-1][j];data[k-1][j] = temp;k--;  }}  }} - (void)down_remove_blank:(DataModel *)model {  array data = [model getData];  int i,j,k;  for(j=0; j  model.matrix; j++){for(i = model.matrix-2; i = 0; i--){  k=i;  while(k+1=model.matrix-1&&data[k+1][j]==0){//上面的那个为空//swap(a[k][j],a[k][j]);int temp = data[k][j];data[k][j] = data[k+1][j];data[k+1][j] = temp;k++;  }}  }} - (void)left_remove_blank:(DataModel *)model {  array data = [model getData];  int i,j,k;  for(i=0;i  model.matrix;i++){for(j=1;jmodel.matrix;j++){  k=j;  while(k-1=0&&data[i][k-1]==0){//上面的那个为空//swap(a[i][k],a[i][k-1]);int temp = data[i][k];data[i][k] = data[i][k-1];data[i][k-1] = temp;k--;  }}  }}- (void)right_remove_blank:(DataModel *)model {  array data = [model getData];  int i,j,k;  for(i=0;imodel.matrix;i++){for(j=model.matrix-2;j=0;j--){  k=j;  while(k+1=model.matrix-1&&data[i][k+1]==0){//上面的那个为空//swap(a[i][k],a[i][k+1]);int temp = data[i][k];data[i][k] = data[i][k+1];data[i][k+1] = temp;k++;  }}  }}- (void)left:(DataModel *)model {  array data = [model getData];  int i,j;  for(i=0;imodel.matrix;i++){for(j=0;jmodel.matrix-1;j++){  if(data[i][j]==data[i][j+1]){_addScore = _addScore + data[i][j];data[i][j]+=data[i][j+1];data[i][j+1]=0;[self left_remove_blank:model];  }}  }}- (void)right:(DataModel *)model {  array data = [model getData];  int i,j;  for(i=0;imodel.matrix;i++){for(j=model.matrix-1;j=1;j--){  if(data[i][j]==data[i][j-1]){_addScore = _addScore + data[i][j];data[i][j]+=data[i][j-1];data[i][j-1]=0;[self right_remove_blank:model];  }}  }}- (void)up:(DataModel *)model {  array data = [model getData];  int i,j;  for(j=0;jmodel.matrix;j++){//每一列for(i=0;imodel.matrix-1;i++){  if(data[i][j]==data[i+1][j]){_addScore = _addScore + data[i][j];data[i][j]=data[i][j]+data[i+1][j];data[i+1][j]=0;//移除空格[self up_remove_blank:model];  }}  }}- (void)down:(DataModel *)model {  array data = [model getData];  int i,j;  for(j=0;jmodel.matrix;j++){//每一列for(i=model.matrix-1;i=1;i--){  if(data[i][j]==data[i-1][j]){_addScore = _addScore + data[i][j];data[i][j]=data[i][j]+data[i-1][j];data[i-1][j]=0;//移除空格[self down_remove_blank:model];  }}  }} @end
展开更多 50%)
分享

猜你喜欢

Objective-C 实现2048算法类

编程语言 网络编程
Objective-C 实现2048算法类

Objective-C 消息传递机制详解

编程语言 网络编程
Objective-C 消息传递机制详解

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

Objective-C的MKNetworkKit开发框架解析

编程语言 网络编程
Objective-C的MKNetworkKit开发框架解析

Objective-C中NSArray的基本用法示例

编程语言 网络编程
Objective-C中NSArray的基本用法示例

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

Objective-C中使用NSString类操作字符串的方法小结

编程语言 网络编程
Objective-C中使用NSString类操作字符串的方法小结

Objective-C封装字符串存储操作示例

编程语言 网络编程
Objective-C封装字符串存储操作示例

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

java string 转date方法如何实现

java string 转date方法如何实现

PHP安全编程之加密功能

PHP安全编程之加密功能
下拉加载更多内容 ↓